从Swift 2.0开始,您现在可以将函数指针传递给C API。
var 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), nil, &hkCom) // Check that hkCom in indeed your hotkey ID and handle it.}, 1, &eventType, nil, nil)// Register hotkey.let status = RegisterEventHotKey(UInt32(keyCode), UInt32(modifierKeys), gMyHotKeyID, GetApplicationEventTarget(), 0, &hotKeyRef)


