我试图在 Swift 中使用 CGEventTapCreate 等捕获按键。我想这样做,以便当我的 child 使用 Mac 程序时,我可以限制哪些键执行某些操作。我意识到我需要启用辅助技术才能使 CGEventTap 正常工作
我无法理解我需要在 Swift 中做什么才能获取调用中的最终参数。本质上,我不理解quartz event documentation 的部分文档。
具体来说是由
描述的最终参数refcon: A pointer to user-defined data. This pointer is passed into the callback function specified in the callback parameter.
这是我的代码,缺少调用中的最终参数
@IBAction func interceptKeyboard(sender: NSButton) {
CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, kCGEventTapOptionDefault, CGEventMask((kCGEventKeyDown) | (kCGEventKeyUp)), MyCallBack: kCGHIDEventTap, )
}
func MyCallBack(myLocation:CGEventTapLocation) -> Void{
//do Nowt
}
最佳答案
不久前,但我想当你查看 CGEventTapCallback 的类型时,你会发现它是一个 CFunctionPointer:
typealias CGEventTapCallBack = CFunctionPointer<((CGEventTapProxy,
CGEventType, CGEvent!, UnsafeMutablePointer<Void>) -> Unmanaged<CGEvent>!)>
目前您无法在 Swift 中创建这些,因此您必须深入研究 Objective-C,在那里创建一个函数,然后该函数可以在 Swift 中调用等效函数,请参见此处:
Objective-C Wrapper for CFunctionPointer to a Swift Closure
我最终用 Objective-C 编写了所有这些东西......
关于macos - 使用 CGEventTapCreate Swift 中的参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26673329/