From 754ad38523a36b4204400422ddc143132eb01ecc Mon Sep 17 00:00:00 2001 From: mia Date: Sat, 4 Jun 2022 16:38:10 -0500 Subject: [PATCH] feat(NSEvent) add support for addLocalMonitorForEventsMatchingMask --- cocoa/NSEvent.go | 21 +++++++++++++++++---- cocoa/NSEvent_monitor.go | 15 +++++++++++++-- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/cocoa/NSEvent.go b/cocoa/NSEvent.go index 48a3f294..4a79f217 100644 --- a/cocoa/NSEvent.go +++ b/cocoa/NSEvent.go @@ -7,15 +7,23 @@ package cocoa #include #include -void monitor(unsigned long long int mask) { +void monitorGlobal(unsigned long long int mask) { [NSEvent addGlobalMonitorForEventsMatchingMask:mask handler:^(NSEvent *incomingEvent) { - void monitorReentry(NSEvent *event); - monitorReentry(incomingEvent); + void monitorGlobalReentry(NSEvent *event); + monitorGlobalReentry(incomingEvent); return; }]; } +void monitorLocal(unsigned long long int mask) { + [NSEvent addLocalMonitorForEventsMatchingMask:mask + handler:^(NSEvent *incomingEvent) { + NSEvent* monitorLocalReentry(NSEvent *event); + return monitorLocalReentry(incomingEvent); + }]; +} + */ import "C" import ( @@ -102,7 +110,12 @@ type NSEvent struct { func NSEvent_GlobalMonitorMatchingMask(mask uint64, ch chan NSEvent) { monitorCh = ch - C.monitor(C.ulonglong(mask)) + C.monitorGlobal(C.ulonglong(mask)) +} + +func NSEvent_LocalMonitorMatchingMask(mask uint64, ch chan NSEvent) { + monitorCh = ch + C.monitorLocal(C.ulonglong(mask)) } func (e NSEvent) KeyCode() (int64, error) { diff --git a/cocoa/NSEvent_monitor.go b/cocoa/NSEvent_monitor.go index b8a87284..6d18d401 100644 --- a/cocoa/NSEvent_monitor.go +++ b/cocoa/NSEvent_monitor.go @@ -15,8 +15,8 @@ import ( var monitorCh chan NSEvent -//export monitorReentry -func monitorReentry(event unsafe.Pointer) { +//export monitorGlobalReentry +func monitorGlobalReentry(event unsafe.Pointer) { if event == nil { return } @@ -24,3 +24,14 @@ func monitorReentry(event unsafe.Pointer) { obj.Retain() monitorCh <- NSEvent_fromRef(obj) } + +//export monitorLocalReentry +func monitorLocalReentry(event unsafe.Pointer) unsafe.Pointer { + if event == nil { + return nil + } + obj := objc.ObjectPtr(uintptr(event)) + obj.Retain() + monitorCh <- NSEvent_fromRef(obj) + return event +}