Skip to content

Commit a8c387a

Browse files
authored
Merge pull request #725 from go-vgo/bitmap-pr
Update: use hid event in macos
2 parents f101b10 + 3eef3b5 commit a8c387a

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
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: 7 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);
@@ -188,6 +189,7 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, uintptr pi
188189
}
189190

190191
SendTo(pid, keyEvent);
192+
CFRelease(source);
191193
}
192194
#elif defined(IS_WINDOWS)
193195
const DWORD dwFlags = down ? 0 : KEYEVENTF_KEYUP;
@@ -273,7 +275,8 @@ void toggleKey(char c, const bool down, MMKeyFlags flags, uintptr pid) {
273275
convert characters to a keycode, but does not support adding modifier flags.
274276
It is only used in typeString().
275277
-- if you need modifier keys, use the above functions instead. */
276-
CGEventRef keyEvent = CGEventCreateKeyboardEvent(NULL, 0, down);
278+
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
279+
CGEventRef keyEvent = CGEventCreateKeyboardEvent(source, 0, down);
277280
if (keyEvent == NULL) {
278281
fputs("Could not create keyboard event.\n", stderr);
279282
return;
@@ -282,6 +285,7 @@ void toggleKey(char c, const bool down, MMKeyFlags flags, uintptr pid) {
282285
CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch);
283286

284287
SendTo(pid, keyEvent);
288+
CFRelease(source);
285289
}
286290
#else
287291
#define toggleUniKey(c, down) toggleKey(c, down, MOD_NONE, 0)

mouse/mouse_c.h

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,15 @@
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);
94+
CFRelease(source);
9395
#elif defined(USE_X11)
9496
Display *display = XGetMainDisplay();
9597
XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, point.x, point.y);
@@ -103,13 +105,15 @@ void moveMouse(MMPointInt32 point){
103105
void dragMouse(MMPointInt32 point, const MMMouseButton button){
104106
#if defined(IS_MACOSX)
105107
const CGEventType dragType = MMMouseDragToCGEventType(button);
106-
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
108+
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
109+
CGEventRef drag = CGEventCreateMouseEvent(source, dragType,
107110
CGPointFromMMPointInt32(point), (CGMouseButton)button);
108111

109112
calculateDeltas(&drag, point);
110113

111-
CGEventPost(kCGSessionEventTap, drag);
114+
CGEventPost(kCGHIDEventTap, drag);
112115
CFRelease(drag);
116+
CFRelease(source);
113117
#else
114118
moveMouse(point);
115119
#endif
@@ -145,10 +149,12 @@ void toggleMouse(bool down, MMMouseButton button) {
145149
#if defined(IS_MACOSX)
146150
const CGPoint currentPos = CGPointFromMMPointInt32(location());
147151
const CGEventType mouseType = MMMouseToCGEventType(down, button);
148-
CGEventRef event = CGEventCreateMouseEvent(NULL, mouseType, currentPos, (CGMouseButton)button);
152+
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
153+
CGEventRef event = CGEventCreateMouseEvent(source, mouseType, currentPos, (CGMouseButton)button);
149154

150-
CGEventPost(kCGSessionEventTap, event);
155+
CGEventPost(kCGHIDEventTap, event);
151156
CFRelease(event);
157+
CFRelease(source);
152158
#elif defined(USE_X11)
153159
Display *display = XGetMainDisplay();
154160
XTestFakeButtonEvent(display, button, down ? True : False, CurrentTime);
@@ -182,7 +188,8 @@ void doubleClick(MMMouseButton button){
182188
const CGEventType mouseTypeDown = MMMouseToCGEventType(true, button);
183189
const CGEventType mouseTypeUP = MMMouseToCGEventType(false, button);
184190

185-
CGEventRef event = CGEventCreateMouseEvent(NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft);
191+
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
192+
CGEventRef event = CGEventCreateMouseEvent(source, mouseTypeDown, currentPos, kCGMouseButtonLeft);
186193

187194
/* Set event to double click. */
188195
CGEventSetIntegerValueField(event, kCGMouseEventClickState, 2);
@@ -192,6 +199,7 @@ void doubleClick(MMMouseButton button){
192199
CGEventPost(kCGHIDEventTap, event);
193200

194201
CFRelease(event);
202+
CFRelease(source);
195203
#else
196204
/* Double click for everything else. */
197205
clickMouse(button);
@@ -208,14 +216,13 @@ void scrollMouseXY(int x, int y) {
208216
INPUT mouseScrollInputV;
209217
#endif
210218

211-
/* Direction should only be considered based on the scrollDirection. This Should not interfere. */
212-
/* Set up the OS specific solution */
213-
#if defined(__APPLE__)
214-
CGEventRef event;
215-
event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x);
219+
#if defined(IS_MACOSX)
220+
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
221+
CGEventRef event = CGEventCreateScrollWheelEvent(source, kCGScrollEventUnitPixel, 2, y, x);
216222
CGEventPost(kCGHIDEventTap, event);
217223

218224
CFRelease(event);
225+
CFRelease(source);
219226
#elif defined(USE_X11)
220227
int ydir = 4; /* Button 4 is up, 5 is down. */
221228
int xdir = 6;

0 commit comments

Comments
 (0)