Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NSEvent) add support for addLocalMonitorForEventsMatchingMask #92

Merged
merged 1 commit into from
Jun 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}