Skip to content

Commit bb76af1

Browse files
committed
Add: add mouse of scale support and optimize the multi screen
1 parent df1617d commit bb76af1

File tree

4 files changed

+43
-31
lines changed

4 files changed

+43
-31
lines changed

mouse/mouse_c.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void moveMouse(MMPointInt32 point){
101101
#define MOUSE_COORD_TO_ABS(coord, width_or_height) ( \
102102
((65536 * coord) / width_or_height) + (coord < 0 ? -1 : 1))
103103

104-
MMRectInt32 rect = getScreenRect(-1);
104+
MMRectInt32 rect = getScreenRect(1);
105105
int32_t x = MOUSE_COORD_TO_ABS(point.x - rect.origin.x, rect.size.w);
106106
int32_t y = MOUSE_COORD_TO_ABS(point.y - rect.origin.y, rect.size.h);
107107

@@ -239,12 +239,8 @@ void scrollMouseXY(int x, int y) {
239239
int xdir = 6;
240240
Display *display = XGetMainDisplay();
241241

242-
if (y < 0) {
243-
ydir = 5;
244-
}
245-
if (x < 0) {
246-
xdir = 7;
247-
}
242+
if (y < 0) { ydir = 5; }
243+
if (x < 0) { xdir = 7; }
248244

249245
int xi; int yi;
250246
for (xi = 0; xi < abs(x); xi++) {
@@ -299,7 +295,7 @@ static double crude_hypot(double x, double y){
299295

300296
bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed){
301297
MMPointInt32 pos = location();
302-
MMSizeInt32 screenSize = getMainDisplaySize();
298+
// MMSizeInt32 screenSize = getMainDisplaySize();
303299
double velo_x = 0.0, velo_y = 0.0;
304300
double distance;
305301

@@ -315,13 +311,13 @@ bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed)
315311
velo_x /= veloDistance;
316312
velo_y /= veloDistance;
317313

318-
pos.x += floor(velo_x + 0.5);
319-
pos.y += floor(velo_y + 0.5);
314+
pos.x += floor(velo_x + 0.8);
315+
pos.y += floor(velo_y + 0.8);
320316

321317
/* Make sure we are in the screen boundaries! (Strange things will happen if we are not.) */
322-
if (pos.x >= screenSize.w || pos.y >= screenSize.h) {
323-
return false;
324-
}
318+
// if (pos.x >= screenSize.w || pos.y >= screenSize.h) {
319+
// return false;
320+
// }
325321
moveMouse(pos);
326322

327323
/* Wait 1 - 3 milliseconds. */

robotgo.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ var (
7575

7676
// NotPid used the hwnd not pid in windows
7777
NotPid bool
78+
// Scale option the os screen scale
79+
Scale bool
7880
)
7981

8082
type (
@@ -253,7 +255,7 @@ func SysScale(displayId ...int) float64 {
253255
return float64(s)
254256
}
255257

256-
// Scaled get the screen scaled size
258+
// Scaled get the screen scaled return scale size
257259
func Scaled(x int, displayId ...int) int {
258260
f := ScaleF(displayId...)
259261
return Scaled0(x, f)
@@ -282,7 +284,8 @@ func GetScreenRect(displayId ...int) Rect {
282284
int(rect.size.w), int(rect.size.h)
283285

284286
if runtime.GOOS == "windows" {
285-
f := ScaleF(displayId...)
287+
// f := ScaleF(displayId...)
288+
f := ScaleF()
286289
x, y, w, h = Scaled0(x, f), Scaled0(y, f), Scaled0(w, f), Scaled0(h, f)
287290
}
288291
return Rect{
@@ -474,17 +477,24 @@ func CheckMouse(btn string) C.MMMouseButton {
474477
return C.LEFT_BUTTON
475478
}
476479

480+
// MoveScale calculate the os scale factor x, y
481+
func MoveScale(x, y int, displayId ...int) (int, int) {
482+
if Scale && runtime.GOOS == "windows" {
483+
f := ScaleF()
484+
x, y = Scaled0(x, f), Scaled0(y, f)
485+
}
486+
487+
return x, y
488+
}
489+
477490
// Move move the mouse to (x, y)
478491
//
479492
// Examples:
480493
//
481494
// robotgo.MouseSleep = 100 // 100 millisecond
482495
// robotgo.Move(10, 10)
483-
func Move(x, y int) {
484-
// if runtime.GOOS == "windows" {
485-
// f := ScaleF()
486-
// x, y = Scaled0(x, f), Scaled0(y, f)
487-
// }
496+
func Move(x, y int, displayId ...int) {
497+
x, y = MoveScale(x, y, displayId...)
488498

489499
cx := C.int32_t(x)
490500
cy := C.int32_t(y)
@@ -498,6 +508,8 @@ func Move(x, y int) {
498508
// Drag drag the mouse to (x, y),
499509
// It's not valid now, use the DragSmooth()
500510
func Drag(x, y int, args ...string) {
511+
x, y = MoveScale(x, y)
512+
501513
var button C.MMMouseButton = C.LEFT_BUTTON
502514
cx := C.int32_t(x)
503515
cy := C.int32_t(y)
@@ -516,6 +528,8 @@ func Drag(x, y int, args ...string) {
516528
//
517529
// robotgo.DragSmooth(10, 10)
518530
func DragSmooth(x, y int, args ...interface{}) {
531+
x, y = MoveScale(x, y)
532+
519533
Toggle("left")
520534
MilliSleep(50)
521535
MoveSmooth(x, y, args...)
@@ -536,6 +550,7 @@ func MoveSmooth(x, y int, args ...interface{}) bool {
536550
// f := ScaleF()
537551
// x, y = Scaled0(x, f), Scaled0(y, f)
538552
// }
553+
x, y = MoveScale(x, y)
539554

540555
cx := C.int32_t(x)
541556
cy := C.int32_t(y)

robotgo_fn_v1.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ func TypeStringDelayed(str string, delay int) {
5757

5858
// Deprecated: use the ScaledF(),
5959
//
60-
// Scale get the screen scale (only windows old), drop
61-
func Scale() int {
60+
// Scale1 get the screen scale (only windows old), drop
61+
func Scale1() int {
6262
dpi := map[int]int{
6363
0: 100,
6464
// DPI Scaling Level
@@ -90,6 +90,6 @@ func Scale0() int {
9090
//
9191
// Mul mul the scale, drop
9292
func Mul(x int) int {
93-
s := Scale()
93+
s := Scale1()
9494
return x * s / 100
9595
}

screen/screen_c.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,20 @@ MMRectInt32 getScreenRect(int32_t display_id) {
5050
(int32_t)DisplayWidth(display, screen),
5151
(int32_t)DisplayHeight(display, screen));
5252
#elif defined(IS_WINDOWS)
53-
// if (GetSystemMetrics(SM_CMONITORS) == 1) {
53+
if (GetSystemMetrics(SM_CMONITORS) == 1
54+
|| display_id == -1 || display_id == 0) {
5455
return MMRectInt32Make(
5556
(int32_t)0,
5657
(int32_t)0,
5758
(int32_t)GetSystemMetrics(SM_CXSCREEN),
5859
(int32_t)GetSystemMetrics(SM_CYSCREEN));
59-
// } else {
60-
// return MMRectInt32Make(
61-
// (int32_t)GetSystemMetrics(SM_XVIRTUALSCREEN),
62-
// (int32_t)GetSystemMetrics(SM_YVIRTUALSCREEN),
63-
// (int32_t)GetSystemMetrics(SM_CXVIRTUALSCREEN),
64-
// (int32_t)GetSystemMetrics(SM_CYVIRTUALSCREEN));
65-
// }
60+
} else {
61+
return MMRectInt32Make(
62+
(int32_t)GetSystemMetrics(SM_XVIRTUALSCREEN),
63+
(int32_t)GetSystemMetrics(SM_YVIRTUALSCREEN),
64+
(int32_t)GetSystemMetrics(SM_CXVIRTUALSCREEN),
65+
(int32_t)GetSystemMetrics(SM_CYVIRTUALSCREEN));
66+
}
6667
#endif
6768
}
6869

0 commit comments

Comments
 (0)