我试图有一个处理程序在我的Mac OS X应用程序中用Swift编写的全局(系统范围)热键组合,但我只是无法找到适当的文档。我读过,我不得不在一些遗留的Carbon API为它,是没有更好的方法?你能给我一些概念证明Swift代码吗?提前致谢!
自从Swift 2.0以来,你现在可以传递一个函数指针到C API。
原文链接:https://www.f2er.com/swift/320930.htmlvar gMyHotKeyID = EventHotKeyID() gMyHotKeyID.signature = OSType("swat".fourCharCodeValue) gMyHotKeyID.id = UInt32(keyCode) var eventType = EventTypeSpec() eventType.eventClass = OSType(kEventClassKeyboard) eventType.eventKind = OSType(kEventHotKeyPressed) // Install handler. InstallEventHandler(GetApplicationEventTarget(),{(nextHanlder,theEvent,userData) -> OSStatus in var hkCom = EventHotKeyID() GetEventParameter(theEvent,EventParamName(kEventParamDirectObject),EventParamType(typeEventHotKeyID),nil,sizeof(EventHotKeyID),&hkCom) /// Check that hkCom in indeed your hotkey ID and handle it. },1,&eventType,nil) // Register hotkey. let status = RegisterEventHotKey(UInt32(keyCode),UInt32(modifierKeys),gMyHotKeyID,GetApplicationEventTarget(),&hotKeyRef)