我正在尝试在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麻烦和参数
发布评论