在Swift中使用CGEventTapCreate麻烦和参数

编程入门 行业动态 更新时间:2024-10-27 08:25:21
本文介绍了在Swift中使用CGEventTapCreate麻烦和参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在Swift中使用CGEventTapCreate等来捕获按键.我想这样做,以便当我的孩子使用Mac程序时,我可以限制哪些键可以执行某些操作.我非常感谢我需要启用辅助技术才能使CGEventTap正常工作

I am trying to trap keypresses using a CGEventTapCreate etc in Swift. I want to do this so that when my toddler is using a program the Mac, I can limit which keys do something. I appreciate that I need to enable assistive technology to get CGEventTap to work

我无法理解在Swift中需要做些什么才能在调用中获取最终参数.从本质上讲,我不了解石英事件文档

I am having trouble understanding what I need to do in Swift to get the final parameter in the call. Essentially, I do not understand part of the documentation at quartz event documentation

特别是

refcon:指向用户定义数据的指针.该指针被传递到 在callback参数中指定的回调函数.

refcon: A pointer to user-defined data. This pointer is passed into the callback function specified in the callback parameter.

这是我的代码缺少通话中的最后一个参数

Here is my code missing the final parameter from the call

@IBAction func interceptKeyboard(sender: NSButton) { CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, CGEventMask((kCGEventKeyDown) | (kCGEventKeyUp)), MyCallBack: kCGHIDEventTap, ) } func MyCallBack(myLocation:CGEventTapLocation) -> Void{ //do Nowt }

推荐答案

前一阵子,但是我认为当您查看CGEventTapCallback的类型时,您会看到它是一个CFunctionPointer:

Bit of a while ago, but I think when you look at the type of CGEventTapCallback, you see that it is a CFunctionPointer:

typealias CGEventTapCallBack = CFunctionPointer<((CGEventTapProxy, CGEventType, CGEvent!, UnsafeMutablePointer<Void>) -> Unmanaged<CGEvent>!)>

目前您无法在Swift中创建这些函数,因此您必须使用Objective-C,在其中创建一个函数,然后可以在Swift中调用等效函数,请参见此处:

At the moment you can't create these in Swift, so you have to dip into objective-c, create a function there, which can then call an equivalent function in Swift, see here:

用于CFunctionPointer到Swift闭包的Objective-C包装器

我最终用Objective-C编写了所有这些东西.

I ended up writing the whole of this stuff in Objective-C...

更多推荐

在Swift中使用CGEventTapCreate麻烦和参数

本文发布于:2023-11-08 16:30:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569885.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:麻烦   参数   Swift   CGEventTapCreate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!