Skip to content

Commit 5c28644

Browse files
committed
Update: use hid event in macos
1 parent 73c07dc commit 5c28644

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

key/keycode_c.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ MMKeyCode keyCodeForChar(const char c) {
9090

9191
#if defined(IS_MACOSX)
9292
CFStringRef createStringForKey(CGKeyCode keyCode){
93-
TISInputSourceRef currentKeyboard = TISCopyCurrentASCIICapableKeyboardInputSource();
93+
// TISInputSourceRef currentKeyboard = TISCopyCurrentASCIICapableKeyboardInputSource();
94+
TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardLayoutInputSource();
9495
CFDataRef layoutData = (CFDataRef) TISGetInputSourceProperty(
9596
currentKeyboard, kTISPropertyUnicodeKeyLayoutData);
9697

key/keypress_c.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
if (pid != 0) {
5959
CGEventPostToPid(pid, event);
6060
} else {
61-
CGEventPost(kCGSessionEventTap, event);
61+
CGEventPost(kCGHIDEventTap, event);
6262
}
6363

6464
CFRelease(event);
@@ -179,7 +179,8 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, uintptr pi
179179
NX_SYSDEFINED, loc, &event, kNXEventDataVersion, 0, FALSE);
180180
assert(KERN_SUCCESS == kr);
181181
} else {
182-
CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)code, down);
182+
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
183+
CGEventRef keyEvent = CGEventCreateKeyboardEvent(source, (CGKeyCode)code, down);
183184
assert(keyEvent != NULL);
184185

185186
CGEventSetType(keyEvent, down ? kCGEventKeyDown : kCGEventKeyUp);
@@ -273,7 +274,8 @@ void toggleKey(char c, const bool down, MMKeyFlags flags, uintptr pid) {
273274
convert characters to a keycode, but does not support adding modifier flags.
274275
It is only used in typeString().
275276
-- if you need modifier keys, use the above functions instead. */
276-
CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, 0, down);
277+
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
278+
CGEventRef keyEvent = CGEventCreateKeyboardEvent(source, 0, down);
277279
if (keyEvent == NULL) {
278280
fputs("Could not create keyboard event.\n", stderr);
279281
return;

mouse/mouse_c.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,13 @@
8383
/* Move the mouse to a specific point. */
8484
void moveMouse(MMPointInt32 point){
8585
#if defined(IS_MACOSX)
86-
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
86+
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
87+
CGEventRef move = CGEventCreateMouseEvent(source, kCGEventMouseMoved,
8788
CGPointFromMMPointInt32(point), kCGMouseButtonLeft);
8889

8990
calculateDeltas(&move, point);
9091

91-
CGEventPost(kCGSessionEventTap, move);
92+
CGEventPost(kCGHIDEventTap, move);
9293
CFRelease(move);
9394
#elif defined(USE_X11)
9495
Display *display = XGetMainDisplay();
@@ -103,12 +104,13 @@ void moveMouse(MMPointInt32 point){
103104
void dragMouse(MMPointInt32 point, const MMMouseButton button){
104105
#if defined(IS_MACOSX)
105106
const CGEventType dragType = MMMouseDragToCGEventType(button);
106-
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
107+
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
108+
CGEventRef drag = CGEventCreateMouseEvent(source, dragType,
107109
CGPointFromMMPointInt32(point), (CGMouseButton)button);
108110

109111
calculateDeltas(&drag, point);
110112

111-
CGEventPost(kCGSessionEventTap, drag);
113+
CGEventPost(kCGHIDEventTap, drag);
112114
CFRelease(drag);
113115
#else
114116
moveMouse(point);
@@ -145,9 +147,10 @@ void toggleMouse(bool down, MMMouseButton button) {
145147
#if defined(IS_MACOSX)
146148
const CGPoint currentPos = CGPointFromMMPointInt32(location());
147149
const CGEventType mouseType = MMMouseToCGEventType(down, button);
148-
CGEventRef event = CGEventCreateMouseEvent(NULL, mouseType, currentPos, (CGMouseButton)button);
150+
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
151+
CGEventRef event = CGEventCreateMouseEvent(source, mouseType, currentPos, (CGMouseButton)button);
149152

150-
CGEventPost(kCGSessionEventTap, event);
153+
CGEventPost(kCGHIDEventTap, event);
151154
CFRelease(event);
152155
#elif defined(USE_X11)
153156
Display *display = XGetMainDisplay();

0 commit comments

Comments
 (0)