Skip to content

Commit a6bd56e

Browse files
authored
Merge pull request #758 from PekingSpades/pr-macos-desktop-drag
Fix: use drag event for macOS smooth drag
2 parents cec382b + 10deb4d commit a6bd56e

File tree

3 files changed

+38
-23
lines changed

3 files changed

+38
-23
lines changed

mouse/mouse_c.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ static double crude_hypot(double x, double y){
310310
return ((M_SQRT2 - 1.0) * small) + big;
311311
}
312312

313-
bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed){
313+
static bool smoothlyMoveMouseImpl(MMPointInt32 endPoint, double lowSpeed, double highSpeed,
314+
bool drag, MMMouseButton button){
314315
MMPointInt32 pos = location();
315316
// MMSizeInt32 screenSize = getMainDisplaySize();
316317
double velo_x = 0.0, velo_y = 0.0;
@@ -335,11 +336,24 @@ bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed)
335336
// if (pos.x >= screenSize.w || pos.y >= screenSize.h) {
336337
// return false;
337338
// }
338-
moveMouse(pos);
339+
if (drag) {
340+
dragMouse(pos, button);
341+
} else {
342+
moveMouse(pos);
343+
}
339344

340345
/* Wait 1 - 3 milliseconds. */
341346
microsleep(DEADBEEF_UNIFORM(lowSpeed, highSpeed));
342347
// microsleep(DEADBEEF_UNIFORM(1.0, 3.0));
343348
}
344349
return true;
345350
}
351+
352+
bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed){
353+
return smoothlyMoveMouseImpl(endPoint, lowSpeed, highSpeed, false, LEFT_BUTTON);
354+
}
355+
356+
bool smoothlyDragMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed,
357+
MMMouseButton button){
358+
return smoothlyMoveMouseImpl(endPoint, lowSpeed, highSpeed, true, button);
359+
}

robotgo.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -578,24 +578,11 @@ func Drag(x, y int, args ...string) {
578578
func DragSmooth(x, y int, args ...interface{}) {
579579
Toggle("left")
580580
MilliSleep(50)
581-
MoveSmooth(x, y, args...)
581+
smoothMove(x, y, true, args...)
582582
Toggle("left", "up")
583583
}
584584

585-
// MoveSmooth move the mouse smooth,
586-
// moves mouse to x, y human like, with the mouse button up.
587-
//
588-
// robotgo.MoveSmooth(x, y int, low, high float64, mouseDelay int)
589-
//
590-
// Examples:
591-
//
592-
// robotgo.MoveSmooth(10, 10)
593-
// robotgo.MoveSmooth(10, 10, 1.0, 2.0)
594-
func MoveSmooth(x, y int, args ...interface{}) bool {
595-
// if runtime.GOOS == "windows" {
596-
// f := ScaleF()
597-
// x, y = Scaled0(x, f), Scaled0(y, f)
598-
// }
585+
func smoothMove(x, y int, drag bool, args ...interface{}) bool {
599586
x, y = MoveScale(x, y)
600587

601588
cx := C.int32_t(x)
@@ -619,12 +606,30 @@ func MoveSmooth(x, y int, args ...interface{}) bool {
619606
high = 3.0
620607
}
621608

622-
cbool := C.smoothlyMoveMouse(C.MMPointInt32Make(cx, cy), low, high)
609+
var cbool C.bool
610+
if drag {
611+
cbool = C.smoothlyDragMouse(C.MMPointInt32Make(cx, cy), low, high, C.LEFT_BUTTON)
612+
} else {
613+
cbool = C.smoothlyMoveMouse(C.MMPointInt32Make(cx, cy), low, high)
614+
}
623615
MilliSleep(MouseSleep + mouseDelay)
624616

625617
return bool(cbool)
626618
}
627619

620+
// MoveSmooth move the mouse smooth,
621+
// moves mouse to x, y human like, with the mouse button up.
622+
//
623+
// robotgo.MoveSmooth(x, y int, low, high float64, mouseDelay int)
624+
//
625+
// Examples:
626+
//
627+
// robotgo.MoveSmooth(10, 10)
628+
// robotgo.MoveSmooth(10, 10, 1.0, 2.0)
629+
func MoveSmooth(x, y int, args ...interface{}) bool {
630+
return smoothMove(x, y, false, args...)
631+
}
632+
628633
// MoveArgs get the mouse relative args
629634
func MoveArgs(x, y int) (int, int) {
630635
mx, my := Location()

robotgo_fn_v1.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@ func MoveMouse(x, y int) {
2525
// DragMouse drag the mouse to (x, y),
2626
// It's same with the DragSmooth() now
2727
func DragMouse(x, y int, args ...interface{}) {
28-
Toggle("left")
29-
MilliSleep(50)
30-
// Drag(x, y, args...)
31-
MoveSmooth(x, y, args...)
32-
Toggle("left", "up")
28+
DragSmooth(x, y, args...)
3329
}
3430

3531
// Deprecated: use the MoveSmooth(),

0 commit comments

Comments
 (0)