1

I'm trying to convert CGEvent to NSEvent using NSEvent(cgEvent: event) in order to get the characters of key press, but I get the error saying it's an "Invalid event." My code and the error message are below. Any help would b eappreciated. Thanks!

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
)
jl303
  • 1,461
  • 15
  • 27
  • 1
    Which key did you press? Error log shows the problem isn't at `NSEvent(cgEvent: event)`, but `other.characters`. Check if **other** is nil or it's `character` array is nil. – x4h1d Jun 11 '17 at 04:52
  • It doesn't matter what key I press. Wouldn't if let statement make sure other and chars are not nil though? – jl303 Jun 11 '17 at 05:11
  • So the first key press always throws an exception? Which keyboard do you use (System Preferences)? I tried your code and it works. – Willeke Jun 11 '17 at 09:24
  • Thanks for your help. I discovered that my actual code was missing if type == .keyDown. It seems to fail if type was .flagsChanged. I guess there's no character to report for .flagsChanged. Once I made sure type is .keyDown, it worked! – jl303 Jun 11 '17 at 12:14

0 Answers0