如何在Swift中将CGEvent转换为NSEvent?

时间:2017-06-11 03:32:22

标签: swift nsevent cgeventtap

我试图使用NSEvent(cgEvent:event)将CGEvent转换为NSEvent以获取按键的字符,但是我收到错误,说它是"无效的事件。 "我的代码和错误消息如下。任何帮助,将不胜感激。谢谢!

func myCGEventCallback(proxy : CGEventTapProxy, type : CGEventType, event : CGEvent, refcon : UnsafeMutableRawPointer?) -> Unmanaged<CGEvent>? {
    if type == .keyDown {
        if let other = NSEvent(cgEvent: event), let chars = other.characters {
            debugPrint(chars)
        }
    }
    return Unmanaged.passRetained(event)
}

2017-06-10 23:18:14.233831-0400 RemapEvent[54431:3082409] [default] Invalid event.
2017-06-10 23:18:14.233938-0400 RemapEvent[54431:3082409] *** Assertion failure in -[NSEvent characters], /Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1504.83.101/AppKit.subproj/NSEvent.m:2414
2017-06-10 23:18:14.234153-0400 RemapEvent[54431:3082409] [General] Invalid message sent to event "NSEvent: type=FlagsChanged loc=(580,464.336) time=116091.6 flags=0x40101 win=0x0 winNum=0 ctxt=0x0 keyCode=59"
2017-06-10 23:18:14.234965-0400 RemapEvent[54431:3082409] [General] (
    0   CoreFoundation                      0x00007fffa16c62cb __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffb64d148d objc_exception_throw + 48
    2   CoreFoundation                      0x00007fffa16cb042 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x00007fffa3113c80 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   AppKit                              0x00007fff9f2caa64 -[NSEvent characters] + 174
    5   RemapEvent                          0x0000000100003da9 _TFFC10RemapEvent14ViewController11viewDidLoadFT_T_L_17myCGEventCallbackFT5proxyVs13OpaquePointer4typeOSC11CGEventType5eventCSo7CGEvent6refconGSqSv__GSqGVs9UnmanagedS3___ + 473
    6   RemapEvent                          0x0000000100004877 _TToFFC10RemapEvent14ViewController11viewDidLoadFT_T_L_17myCGEventCallbackFT5proxyVs13OpaquePointer4typeOSC11CGEventType5eventCSo7CGEvent6refconGSqSv__GSqGVs9UnmanagedS3___ + 55
    7   SkyLight                            0x00007fffb32c0dca processDecodedEventRef + 204
    8   SkyLight                            0x00007fffb32c05b4 processEventTapData + 544
    9   SkyLight                            0x00007fffb319e6db _XPostEventTapData + 280
    10  SkyLight                            0x00007fffb32c033e eventTapMessageHandler + 137
    11  CoreFoundation                      0x00007fffa16451ed __CFMachPortPerform + 253
    12  CoreFoundation                      0x00007fffa16450d9 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    13  CoreFoundation                      0x00007fffa1645051 __CFRunLoopDoSource1 + 465
    14  CoreFoundation                      0x00007fffa163ccc5 __CFRunLoopRun + 2389
    15  CoreFoundation                      0x00007fffa163c114 CFRunLoopRunSpecific + 420
    16  HIToolbox                           0x00007fffa0b9debc RunCurrentEventLoopInMode + 240
    17  HIToolbox                           0x00007fffa0b9dcf1 ReceiveNextEventCommon + 432
    18  HIToolbox                           0x00007fffa0b9db26 _BlockUntilNextEventMatchingListInModeWithFilter + 71
    19  AppKit                              0x00007fff9f136a54 _DPSNextEvent + 1120
    20  AppKit                              0x00007fff9f8b27ee -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 2796
    21  AppKit                              0x00007fff9f12b3db -[NSApplication run] + 926
    22  AppKit                              0x00007fff9f0f5e0e NSApplicationMain + 1237
    23  RemapEvent                          0x000000010000648d main + 13
    24  libdyld.dylib                       0x00007fffb6db6235 start + 1
)

0 个答案:

没有答案