Skip to content

Commit

Permalink
feat(NSEvent) add support for addLocalMonitorForEventsMatchingMask (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob620 authored Jun 12, 2022
1 parent 0c111bf commit aeb6eb8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
21 changes: 17 additions & 4 deletions cocoa/NSEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,23 @@ package cocoa
#include <Foundation/Foundation.h>
#include <AppKit/AppKit.h>
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 (
Expand Down Expand Up @@ -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) {
Expand Down
15 changes: 13 additions & 2 deletions cocoa/NSEvent_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,23 @@ import (

var monitorCh chan NSEvent

//export monitorReentry
func monitorReentry(event unsafe.Pointer) {
//export monitorGlobalReentry
func monitorGlobalReentry(event unsafe.Pointer) {
if event == nil {
return
}
obj := objc.ObjectPtr(uintptr(event))
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
}

0 comments on commit aeb6eb8

Please sign in to comment.