Skip to content

Commit 5cbaede

Browse files
authored
(nut-tree/nut.js#152) Switched to using a private event source and kCGHIDEventTap consistently to not lose input (#99)
1 parent 0486048 commit 5cbaede

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

Diff for: src/macos/keypress.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags)
4848
}
4949
else
5050
{
51-
CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, (CGKeyCode)code, down);
51+
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStatePrivate);
52+
CGEventRef keyEvent = CGEventCreateKeyboardEvent(src, (CGKeyCode)code, down);
5253
assert(keyEvent != NULL);
5354

54-
CGEventSetType(keyEvent, down ? kCGEventKeyDown : kCGEventKeyUp);
5555
CGEventSetFlags(keyEvent, flags);
56-
CGEventPost(kCGSessionEventTap, keyEvent);
56+
CGEventPost(kCGHIDEventTap, keyEvent);
5757
CFRelease(keyEvent);
58+
CFRelease(src);
5859
}
5960
}
6061

@@ -89,7 +90,8 @@ void toggleUnicodeKey(unsigned long ch, const bool down)
8990
* convert characters to a keycode, but does not support adding modifier
9091
* flags. It is therefore only used in typeString() and typeStringDelayed()
9192
* -- if you need modifier keys, use the above functions instead. */
92-
CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, 0, down);
93+
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStatePrivate);
94+
CGEventRef keyEvent = CGEventCreateKeyboardEvent(src, 0, down);
9395
if (keyEvent == NULL)
9496
{
9597
fputs("Could not create keyboard event.\n", stderr);
@@ -110,8 +112,9 @@ void toggleUnicodeKey(unsigned long ch, const bool down)
110112
CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch);
111113
}
112114

113-
CGEventPost(kCGSessionEventTap, keyEvent);
115+
CGEventPost(kCGHIDEventTap, keyEvent);
114116
CFRelease(keyEvent);
117+
CFRelease(src);
115118
}
116119

117120
void toggleUniKey(char c, const bool down)

0 commit comments

Comments
 (0)