Skip to content
This repository was archived by the owner on Jun 19, 2021. It is now read-only.

Commit 8b9bdd0

Browse files
author
Simon Hofmann
committed
Fixed mouse drag on macOS
1 parent 42c3d6f commit 8b9bdd0

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/mouse.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,19 @@ void moveMouse(MMPoint point)
9999
#ifdef IS_MACOSX
100100
CGPoint position = CGPointMake (point.x, point.y);
101101
// Create an HID hardware event source
102-
CGEventSourceRef src = CGEventSourceCreate
103-
(kCGEventSourceStateHIDSystemState);
102+
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
104103

105104
CGEventRef evt = NULL;
106105
if (CGEventSourceButtonState (kCGEventSourceStateHIDSystemState, kCGMouseButtonLeft)) {
107106
// Create a left button drag
107+
printf("left button pressed, drag");
108108
evt = CGEventCreateMouseEvent
109109
(src, kCGEventLeftMouseDragged,
110110
position, kCGMouseButtonLeft);
111111
} else {
112112
if (CGEventSourceButtonState (kCGEventSourceStateHIDSystemState, kCGMouseButtonRight)) {
113113
// Create a right button drag
114+
printf("right button pressed, drag");
114115
evt = CGEventCreateMouseEvent
115116
(src, kCGEventRightMouseDragged,
116117
position, kCGMouseButtonLeft);
@@ -137,14 +138,16 @@ void moveMouse(MMPoint point)
137138
void dragMouse(MMPoint point, const MMMouseButton button)
138139
{
139140
#if defined(IS_MACOSX)
141+
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
140142
const CGEventType dragType = MMMouseDragToCGEventType(button);
141-
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
143+
CGEventRef drag = CGEventCreateMouseEvent(src, dragType,
142144
CGPointFromMMPoint(point),
143145
(CGMouseButton)button);
144146
calculateDeltas(&drag, point);
145147

146-
CGEventPost(kCGSessionEventTap, drag);
148+
CGEventPost(kCGHIDEventTap, drag);
147149
CFRelease(drag);
150+
CFRelease(src);
148151
#else
149152
moveMouse(point);
150153
#endif
@@ -153,9 +156,11 @@ void dragMouse(MMPoint point, const MMMouseButton button)
153156
MMPoint getMousePos()
154157
{
155158
#if defined(IS_MACOSX)
156-
CGEventRef event = CGEventCreate(NULL);
159+
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
160+
CGEventRef event = CGEventCreate(src);
157161
CGPoint point = CGEventGetLocation(event);
158162
CFRelease(event);
163+
CFRelease(src);
159164

160165
return MMPointFromCGPoint(point);
161166
#elif defined(USE_X11)
@@ -192,7 +197,9 @@ void toggleMouse(bool down, MMMouseButton button)
192197
mouseType,
193198
currentPos,
194199
(CGMouseButton)button);
195-
CGEventPost(kCGSessionEventTap, event);
200+
CGEventPost(kCGHIDEventTap, event);
201+
printf("mouse state left: %d", CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonLeft));
202+
printf("mouse state right: %d", CGEventSourceButtonState(kCGEventSourceStateHIDSystemState, kCGMouseButtonRight));
196203
CFRelease(event);
197204
CFRelease(src);
198205
#elif defined(USE_X11)
@@ -224,7 +231,8 @@ void doubleClick(MMMouseButton button)
224231
const CGEventType mouseTypeDown = MMMouseToCGEventType(true, button);
225232
const CGEventType mouseTypeUP = MMMouseToCGEventType(false, button);
226233

227-
CGEventRef event = CGEventCreateMouseEvent(NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft);
234+
CGEventSourceRef src = CGEventSourceCreate(kCGEventSourceStateHIDSystemState);
235+
CGEventRef event = CGEventCreateMouseEvent(src, mouseTypeDown, currentPos, kCGMouseButtonLeft);
228236

229237
/* Set event to double click. */
230238
CGEventSetIntegerValueField(event, kCGMouseEventClickState, 2);
@@ -235,6 +243,7 @@ void doubleClick(MMMouseButton button)
235243
CGEventPost(kCGHIDEventTap, event);
236244

237245
CFRelease(event);
246+
CFRelease(src);
238247

239248
#else
240249

0 commit comments

Comments
 (0)