I'm coding a macOS app with Xcode (XCode 11.5 under MacOS Catalina), the goal being to send through UDP the coordinates of fingers in contact with my Macbook Pro's trackpad, and trigger an haptic feedback when a UDP message is received.
Being a novice in Xcode, I created my app with this code, and SwiftOSC for the UDP communication. I succeeded to do what I wanted when the app is in the foreground.
Code: exactly the one found here.
Problem: the app is not updated while being in the background.
Update: Following @zrzka advices:
- I use
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.any) {event in
self.backMouseMovements(with: event)
}
to monitor events in the background, with the following function:
func backMouseMovements(with event: NSEvent) {
print("Event: \(event)")
let touches = event.touches(matching: .moved, in: self)
delegate?.touchesView(self, didUpdateTouchingTouches: touches)
}
I put this code in override init(frame frameRect: NSRect)
of the AppKitTouchesView
class of this code.
Accessibility:
AXIsProcessTrusted()
returnstrue
I don't have access to
touches
with these events, and even less tonormalizedPosition
, which is my ultimate goal.
Instead, I have an error:
2020-08-13 09:40:53.538119+0200 TrackPad[1263:34805] *** Assertion failure in -[NSEvent touchesMatchingPhase:inView:], /AppleInternal/BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1894.50.103/AppKit.subproj/NSEvent.m:4874
2020-08-13 09:40:53.538198+0200 TrackPad[1263:34805] [General] An uncaught exception was raised
2020-08-13 09:40:53.538229+0200 TrackPad[1263:34805] [General] Invalid message sent to event "NSEvent: type=MouseMoved loc=(1197.92,994.461) time=3138.3 flags=0 win=0x0 winNum=150 ctxt=0x0 evNum=307 click=1 buttonNumber=0 pressure=0 deltaX=1.000000 deltaY=0.000000 deviceID:0x300000080500000 subtype=NSEventSubtypeTouch"
2020-08-13 09:40:53.539294+0200 TrackPad[1263:34805] [General] (
0 CoreFoundation 0x00007fff318c5be7 __exceptionPreprocess + 250
1 libobjc.A.dylib 0x00007fff6a69d5bf objc_exception_throw + 48
2 CoreFoundation 0x00007fff318eed98 +[NSException raise:format:arguments:] + 88
3 Foundation 0x00007fff33fdde9d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 191
4 AppKit 0x00007fff2ee7fc36 -[NSEvent touchesMatchingPhase:inView:] + 449
5 TrackPad 0x0000000100002d0d $s8TrackPad17AppKitTouchesViewC18backMouseMovements4withySo7NSEventC_tF + 813
6 TrackPad 0x0000000100002986 $s8TrackPad17AppKitTouchesViewC5frameACSo6CGRectV_tcfcySo7NSEventCcfU_ + 214
7 TrackPad 0x0000000100002f09 $sSo7NSEventCIegg_ABIeyBy_TR + 73
8 AppKit 0x00007fff2f0a14e9 GlobalObserverHandler + 89
9 HIToolbox 0x00007fff3044f8ff _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 1254
10 HIToolbox 0x00007fff3044ed8d _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 329
11 HIToolbox 0x00007fff3044ec3d SendEventToEventTargetWithOptions + 45
12 HIToolbox 0x00007fff3048eb8c _ZL29ToolboxEventDispatcherHandlerP25OpaqueEventHandlerCallRefP14OpaqueEventRefPv + 1329
13 HIToolbox 0x00007fff3044fe8a _ZL23DispatchEventToHandlersP14EventTargetRecP14OpaqueEventRefP14HandlerCallRec + 2673
14 HIToolbox 0x00007fff3044ed8d _ZL30SendEventToEventTargetInternalP14OpaqueEventRefP20OpaqueEventTargetRefP14HandlerCallRec + 329
15 HIToolbox 0x00007fff3046447e SendEventToEventTarget + 39
16 AppKit 0x00007fff2eabe9aa _DPSNextEvent + 1268
17 AppKit 0x00007fff2eabd070 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1352
18 AppKit 0x00007fff2eaaed7e -[NSApplication run] + 658
19 AppKit 0x00007fff2ea80b86 NSApplicationMain + 777
20 TrackPad 0x000000010000eead main + 13
21 libdyld.dylib 0x00007fff6b844cc9 start + 1
I guess this error is thrown because I want to access to touches
, which doesn't exist for these events. However, I don't understand how to make a link between the NSEvent
of my addGlobalMonitorForEvents
and NSTouch
, where normalizedPosition
is available.
Thanks!