From 5c2864485d16b8bf3eb1a3baaf1dfcddda3d263e Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sat, 17 May 2025 12:33:16 -0700 Subject: [PATCH 1/4] Update: use hid event in macos --- key/keycode_c.h | 3 ++- key/keypress_c.h | 8 +++++--- mouse/mouse_c.h | 15 +++++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/key/keycode_c.h b/key/keycode_c.h index 01f1bdb..efc4f6b 100644 --- a/key/keycode_c.h +++ b/key/keycode_c.h @@ -90,7 +90,8 @@ MMKeyCode keyCodeForChar(const char c) { #if defined(IS_MACOSX) CFStringRef createStringForKey(CGKeyCode keyCode){ - TISInputSourceRef currentKeyboard = TISCopyCurrentASCIICapableKeyboardInputSource(); + // TISInputSourceRef currentKeyboard = TISCopyCurrentASCIICapableKeyboardInputSource(); + TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardLayoutInputSource(); CFDataRef layoutData = (CFDataRef) TISGetInputSourceProperty( currentKeyboard, kTISPropertyUnicodeKeyLayoutData); diff --git a/key/keypress_c.h b/key/keypress_c.h index a606f04..48de33a 100644 --- a/key/keypress_c.h +++ b/key/keypress_c.h @@ -58,7 +58,7 @@ if (pid != 0) { CGEventPostToPid(pid, event); } else { - CGEventPost(kCGSessionEventTap, event); + CGEventPost(kCGHIDEventTap, event); } CFRelease(event); @@ -179,7 +179,8 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, uintptr pi NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE); assert(KERN_SUCCESS == kr); } else { - CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)code, down); + CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); + CGEventRef keyEvent = CGEventCreateKeyboardEvent(source, (CGKeyCode)code, down); assert(keyEvent != NULL); CGEventSetType(keyEvent, down ? kCGEventKeyDown : kCGEventKeyUp); @@ -273,7 +274,8 @@ void toggleKey(char c, const bool down, MMKeyFlags flags, uintptr pid) { convert characters to a keycode, but does not support adding modifier flags. It is only used in typeString(). -- if you need modifier keys, use the above functions instead. */ - CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, 0, down); + CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); + CGEventRef keyEvent = CGEventCreateKeyboardEvent(source, 0, down); if (keyEvent == NULL) { fputs("Could not create keyboard event.\n", stderr); return; diff --git a/mouse/mouse_c.h b/mouse/mouse_c.h index adf8b7d..26d5e98 100644 --- a/mouse/mouse_c.h +++ b/mouse/mouse_c.h @@ -83,12 +83,13 @@ /* Move the mouse to a specific point. */ void moveMouse(MMPointInt32 point){ #if defined(IS_MACOSX) - CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved, + CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); + CGEventRef move = CGEventCreateMouseEvent(source, kCGEventMouseMoved, CGPointFromMMPointInt32(point), kCGMouseButtonLeft); calculateDeltas(&move, point); - CGEventPost(kCGSessionEventTap, move); + CGEventPost(kCGHIDEventTap, move); CFRelease(move); #elif defined(USE_X11) Display *display = XGetMainDisplay(); @@ -103,12 +104,13 @@ void moveMouse(MMPointInt32 point){ void dragMouse(MMPointInt32 point, const MMMouseButton button){ #if defined(IS_MACOSX) const CGEventType dragType = MMMouseDragToCGEventType(button); - CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType, + CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); + CGEventRef drag = CGEventCreateMouseEvent(source, dragType, CGPointFromMMPointInt32(point), (CGMouseButton)button); calculateDeltas(&drag, point); - CGEventPost(kCGSessionEventTap, drag); + CGEventPost(kCGHIDEventTap, drag); CFRelease(drag); #else moveMouse(point); @@ -145,9 +147,10 @@ void toggleMouse(bool down, MMMouseButton button) { #if defined(IS_MACOSX) const CGPoint currentPos = CGPointFromMMPointInt32(location()); const CGEventType mouseType = MMMouseToCGEventType(down, button); - CGEventRef event = CGEventCreateMouseEvent(NULL, mouseType, currentPos, (CGMouseButton)button); + CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); + CGEventRef event = CGEventCreateMouseEvent(source, mouseType, currentPos, (CGMouseButton)button); - CGEventPost(kCGSessionEventTap, event); + CGEventPost(kCGHIDEventTap, event); CFRelease(event); #elif defined(USE_X11) Display *display = XGetMainDisplay(); From 02c668a94689cc1d17ace13e2f35bef61a4f2988 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sat, 17 May 2025 12:37:38 -0700 Subject: [PATCH 2/4] Update: add CFRelease source --- key/keypress_c.h | 2 ++ mouse/mouse_c.h | 3 +++ 2 files changed, 5 insertions(+) diff --git a/key/keypress_c.h b/key/keypress_c.h index 48de33a..81bad31 100644 --- a/key/keypress_c.h +++ b/key/keypress_c.h @@ -189,6 +189,7 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, uintptr pi } SendTo(pid, keyEvent); + CFRelease(source); } #elif defined(IS_WINDOWS) const DWORD dwFlags = down ? 0 : KEYEVENTF_KEYUP; @@ -284,6 +285,7 @@ void toggleKey(char c, const bool down, MMKeyFlags flags, uintptr pid) { CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch); SendTo(pid, keyEvent); + CFRelease(source); } #else #define toggleUniKey(c, down) toggleKey(c, down, MOD_NONE, 0) diff --git a/mouse/mouse_c.h b/mouse/mouse_c.h index 26d5e98..cc2127d 100644 --- a/mouse/mouse_c.h +++ b/mouse/mouse_c.h @@ -91,6 +91,7 @@ void moveMouse(MMPointInt32 point){ CGEventPost(kCGHIDEventTap, move); CFRelease(move); + CFRelease(source); #elif defined(USE_X11) Display *display = XGetMainDisplay(); XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, point.x, point.y); @@ -112,6 +113,7 @@ void dragMouse(MMPointInt32 point, const MMMouseButton button){ CGEventPost(kCGHIDEventTap, drag); CFRelease(drag); + CFRelease(source); #else moveMouse(point); #endif @@ -152,6 +154,7 @@ void toggleMouse(bool down, MMMouseButton button) { CGEventPost(kCGHIDEventTap, event); CFRelease(event); + CFRelease(source); #elif defined(USE_X11) Display *display = XGetMainDisplay(); XTestFakeButtonEvent(display, button, down ? True : False, CurrentTime); From b731094f615b1a3fbb34318649500f1809cac348 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sat, 17 May 2025 12:54:35 -0700 Subject: [PATCH 3/4] Update: update if defined --- mouse/mouse_c.h | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/mouse/mouse_c.h b/mouse/mouse_c.h index cc2127d..11655d8 100644 --- a/mouse/mouse_c.h +++ b/mouse/mouse_c.h @@ -214,11 +214,8 @@ void scrollMouseXY(int x, int y) { INPUT mouseScrollInputV; #endif - /* Direction should only be considered based on the scrollDirection. This Should not interfere. */ - /* Set up the OS specific solution */ - #if defined(__APPLE__) - CGEventRef event; - event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x); + #if defined(IS_MACOSX) + CGEventRef event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x); CGEventPost(kCGHIDEventTap, event); CFRelease(event); From 3eef3b5879a6839daed5638a060617bd3f440632 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sat, 17 May 2025 13:01:03 -0700 Subject: [PATCH 4/4] Add: add CGEventSourceRef source --- mouse/mouse_c.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/mouse/mouse_c.h b/mouse/mouse_c.h index 11655d8..f9697a9 100644 --- a/mouse/mouse_c.h +++ b/mouse/mouse_c.h @@ -188,7 +188,8 @@ void doubleClick(MMMouseButton button){ const CGEventType mouseTypeDown = MMMouseToCGEventType(true, button); const CGEventType mouseTypeUP = MMMouseToCGEventType(false, button); - CGEventRef event = CGEventCreateMouseEvent(NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft); + CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); + CGEventRef event = CGEventCreateMouseEvent(source, mouseTypeDown, currentPos, kCGMouseButtonLeft); /* Set event to double click. */ CGEventSetIntegerValueField(event, kCGMouseEventClickState, 2); @@ -198,6 +199,7 @@ void doubleClick(MMMouseButton button){ CGEventPost(kCGHIDEventTap, event); CFRelease(event); + CFRelease(source); #else /* Double click for everything else. */ clickMouse(button); @@ -215,10 +217,12 @@ void scrollMouseXY(int x, int y) { #endif #if defined(IS_MACOSX) - CGEventRef event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x); + CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); + CGEventRef event = CGEventCreateScrollWheelEvent(source, kCGScrollEventUnitPixel, 2, y, x); CGEventPost(kCGHIDEventTap, event); CFRelease(event); + CFRelease(source); #elif defined(USE_X11) int ydir = 4; /* Button 4 is up, 5 is down. */ int xdir = 6;