mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-05-29 21:43:55 +00:00
Add: add mouse of scale support and optimize the multi screen
This commit is contained in:
parent
df1617d894
commit
bb76af18f0
@ -101,7 +101,7 @@ void moveMouse(MMPointInt32 point){
|
|||||||
#define MOUSE_COORD_TO_ABS(coord, width_or_height) ( \
|
#define MOUSE_COORD_TO_ABS(coord, width_or_height) ( \
|
||||||
((65536 * coord) / width_or_height) + (coord < 0 ? -1 : 1))
|
((65536 * coord) / width_or_height) + (coord < 0 ? -1 : 1))
|
||||||
|
|
||||||
MMRectInt32 rect = getScreenRect(-1);
|
MMRectInt32 rect = getScreenRect(1);
|
||||||
int32_t x = MOUSE_COORD_TO_ABS(point.x - rect.origin.x, rect.size.w);
|
int32_t x = MOUSE_COORD_TO_ABS(point.x - rect.origin.x, rect.size.w);
|
||||||
int32_t y = MOUSE_COORD_TO_ABS(point.y - rect.origin.y, rect.size.h);
|
int32_t y = MOUSE_COORD_TO_ABS(point.y - rect.origin.y, rect.size.h);
|
||||||
|
|
||||||
@ -239,12 +239,8 @@ void scrollMouseXY(int x, int y) {
|
|||||||
int xdir = 6;
|
int xdir = 6;
|
||||||
Display *display = XGetMainDisplay();
|
Display *display = XGetMainDisplay();
|
||||||
|
|
||||||
if (y < 0) {
|
if (y < 0) { ydir = 5; }
|
||||||
ydir = 5;
|
if (x < 0) { xdir = 7; }
|
||||||
}
|
|
||||||
if (x < 0) {
|
|
||||||
xdir = 7;
|
|
||||||
}
|
|
||||||
|
|
||||||
int xi; int yi;
|
int xi; int yi;
|
||||||
for (xi = 0; xi < abs(x); xi++) {
|
for (xi = 0; xi < abs(x); xi++) {
|
||||||
@ -299,7 +295,7 @@ static double crude_hypot(double x, double y){
|
|||||||
|
|
||||||
bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed){
|
bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed){
|
||||||
MMPointInt32 pos = location();
|
MMPointInt32 pos = location();
|
||||||
MMSizeInt32 screenSize = getMainDisplaySize();
|
// MMSizeInt32 screenSize = getMainDisplaySize();
|
||||||
double velo_x = 0.0, velo_y = 0.0;
|
double velo_x = 0.0, velo_y = 0.0;
|
||||||
double distance;
|
double distance;
|
||||||
|
|
||||||
@ -315,13 +311,13 @@ bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed)
|
|||||||
velo_x /= veloDistance;
|
velo_x /= veloDistance;
|
||||||
velo_y /= veloDistance;
|
velo_y /= veloDistance;
|
||||||
|
|
||||||
pos.x += floor(velo_x + 0.5);
|
pos.x += floor(velo_x + 0.8);
|
||||||
pos.y += floor(velo_y + 0.5);
|
pos.y += floor(velo_y + 0.8);
|
||||||
|
|
||||||
/* Make sure we are in the screen boundaries! (Strange things will happen if we are not.) */
|
/* Make sure we are in the screen boundaries! (Strange things will happen if we are not.) */
|
||||||
if (pos.x >= screenSize.w || pos.y >= screenSize.h) {
|
// if (pos.x >= screenSize.w || pos.y >= screenSize.h) {
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
moveMouse(pos);
|
moveMouse(pos);
|
||||||
|
|
||||||
/* Wait 1 - 3 milliseconds. */
|
/* Wait 1 - 3 milliseconds. */
|
||||||
|
29
robotgo.go
29
robotgo.go
@ -75,6 +75,8 @@ var (
|
|||||||
|
|
||||||
// NotPid used the hwnd not pid in windows
|
// NotPid used the hwnd not pid in windows
|
||||||
NotPid bool
|
NotPid bool
|
||||||
|
// Scale option the os screen scale
|
||||||
|
Scale bool
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -253,7 +255,7 @@ func SysScale(displayId ...int) float64 {
|
|||||||
return float64(s)
|
return float64(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scaled get the screen scaled size
|
// Scaled get the screen scaled return scale size
|
||||||
func Scaled(x int, displayId ...int) int {
|
func Scaled(x int, displayId ...int) int {
|
||||||
f := ScaleF(displayId...)
|
f := ScaleF(displayId...)
|
||||||
return Scaled0(x, f)
|
return Scaled0(x, f)
|
||||||
@ -282,7 +284,8 @@ func GetScreenRect(displayId ...int) Rect {
|
|||||||
int(rect.size.w), int(rect.size.h)
|
int(rect.size.w), int(rect.size.h)
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
f := ScaleF(displayId...)
|
// f := ScaleF(displayId...)
|
||||||
|
f := ScaleF()
|
||||||
x, y, w, h = Scaled0(x, f), Scaled0(y, f), Scaled0(w, f), Scaled0(h, f)
|
x, y, w, h = Scaled0(x, f), Scaled0(y, f), Scaled0(w, f), Scaled0(h, f)
|
||||||
}
|
}
|
||||||
return Rect{
|
return Rect{
|
||||||
@ -474,17 +477,24 @@ func CheckMouse(btn string) C.MMMouseButton {
|
|||||||
return C.LEFT_BUTTON
|
return C.LEFT_BUTTON
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MoveScale calculate the os scale factor x, y
|
||||||
|
func MoveScale(x, y int, displayId ...int) (int, int) {
|
||||||
|
if Scale && runtime.GOOS == "windows" {
|
||||||
|
f := ScaleF()
|
||||||
|
x, y = Scaled0(x, f), Scaled0(y, f)
|
||||||
|
}
|
||||||
|
|
||||||
|
return x, y
|
||||||
|
}
|
||||||
|
|
||||||
// Move move the mouse to (x, y)
|
// Move move the mouse to (x, y)
|
||||||
//
|
//
|
||||||
// Examples:
|
// Examples:
|
||||||
//
|
//
|
||||||
// robotgo.MouseSleep = 100 // 100 millisecond
|
// robotgo.MouseSleep = 100 // 100 millisecond
|
||||||
// robotgo.Move(10, 10)
|
// robotgo.Move(10, 10)
|
||||||
func Move(x, y int) {
|
func Move(x, y int, displayId ...int) {
|
||||||
// if runtime.GOOS == "windows" {
|
x, y = MoveScale(x, y, displayId...)
|
||||||
// f := ScaleF()
|
|
||||||
// x, y = Scaled0(x, f), Scaled0(y, f)
|
|
||||||
// }
|
|
||||||
|
|
||||||
cx := C.int32_t(x)
|
cx := C.int32_t(x)
|
||||||
cy := C.int32_t(y)
|
cy := C.int32_t(y)
|
||||||
@ -498,6 +508,8 @@ func Move(x, y int) {
|
|||||||
// Drag drag the mouse to (x, y),
|
// Drag drag the mouse to (x, y),
|
||||||
// It's not valid now, use the DragSmooth()
|
// It's not valid now, use the DragSmooth()
|
||||||
func Drag(x, y int, args ...string) {
|
func Drag(x, y int, args ...string) {
|
||||||
|
x, y = MoveScale(x, y)
|
||||||
|
|
||||||
var button C.MMMouseButton = C.LEFT_BUTTON
|
var button C.MMMouseButton = C.LEFT_BUTTON
|
||||||
cx := C.int32_t(x)
|
cx := C.int32_t(x)
|
||||||
cy := C.int32_t(y)
|
cy := C.int32_t(y)
|
||||||
@ -516,6 +528,8 @@ func Drag(x, y int, args ...string) {
|
|||||||
//
|
//
|
||||||
// robotgo.DragSmooth(10, 10)
|
// robotgo.DragSmooth(10, 10)
|
||||||
func DragSmooth(x, y int, args ...interface{}) {
|
func DragSmooth(x, y int, args ...interface{}) {
|
||||||
|
x, y = MoveScale(x, y)
|
||||||
|
|
||||||
Toggle("left")
|
Toggle("left")
|
||||||
MilliSleep(50)
|
MilliSleep(50)
|
||||||
MoveSmooth(x, y, args...)
|
MoveSmooth(x, y, args...)
|
||||||
@ -536,6 +550,7 @@ func MoveSmooth(x, y int, args ...interface{}) bool {
|
|||||||
// f := ScaleF()
|
// f := ScaleF()
|
||||||
// x, y = Scaled0(x, f), Scaled0(y, f)
|
// x, y = Scaled0(x, f), Scaled0(y, f)
|
||||||
// }
|
// }
|
||||||
|
x, y = MoveScale(x, y)
|
||||||
|
|
||||||
cx := C.int32_t(x)
|
cx := C.int32_t(x)
|
||||||
cy := C.int32_t(y)
|
cy := C.int32_t(y)
|
||||||
|
@ -57,8 +57,8 @@ func TypeStringDelayed(str string, delay int) {
|
|||||||
|
|
||||||
// Deprecated: use the ScaledF(),
|
// Deprecated: use the ScaledF(),
|
||||||
//
|
//
|
||||||
// Scale get the screen scale (only windows old), drop
|
// Scale1 get the screen scale (only windows old), drop
|
||||||
func Scale() int {
|
func Scale1() int {
|
||||||
dpi := map[int]int{
|
dpi := map[int]int{
|
||||||
0: 100,
|
0: 100,
|
||||||
// DPI Scaling Level
|
// DPI Scaling Level
|
||||||
@ -90,6 +90,6 @@ func Scale0() int {
|
|||||||
//
|
//
|
||||||
// Mul mul the scale, drop
|
// Mul mul the scale, drop
|
||||||
func Mul(x int) int {
|
func Mul(x int) int {
|
||||||
s := Scale()
|
s := Scale1()
|
||||||
return x * s / 100
|
return x * s / 100
|
||||||
}
|
}
|
||||||
|
@ -50,19 +50,20 @@ MMRectInt32 getScreenRect(int32_t display_id) {
|
|||||||
(int32_t)DisplayWidth(display, screen),
|
(int32_t)DisplayWidth(display, screen),
|
||||||
(int32_t)DisplayHeight(display, screen));
|
(int32_t)DisplayHeight(display, screen));
|
||||||
#elif defined(IS_WINDOWS)
|
#elif defined(IS_WINDOWS)
|
||||||
// if (GetSystemMetrics(SM_CMONITORS) == 1) {
|
if (GetSystemMetrics(SM_CMONITORS) == 1
|
||||||
|
|| display_id == -1 || display_id == 0) {
|
||||||
return MMRectInt32Make(
|
return MMRectInt32Make(
|
||||||
(int32_t)0,
|
(int32_t)0,
|
||||||
(int32_t)0,
|
(int32_t)0,
|
||||||
(int32_t)GetSystemMetrics(SM_CXSCREEN),
|
(int32_t)GetSystemMetrics(SM_CXSCREEN),
|
||||||
(int32_t)GetSystemMetrics(SM_CYSCREEN));
|
(int32_t)GetSystemMetrics(SM_CYSCREEN));
|
||||||
// } else {
|
} else {
|
||||||
// return MMRectInt32Make(
|
return MMRectInt32Make(
|
||||||
// (int32_t)GetSystemMetrics(SM_XVIRTUALSCREEN),
|
(int32_t)GetSystemMetrics(SM_XVIRTUALSCREEN),
|
||||||
// (int32_t)GetSystemMetrics(SM_YVIRTUALSCREEN),
|
(int32_t)GetSystemMetrics(SM_YVIRTUALSCREEN),
|
||||||
// (int32_t)GetSystemMetrics(SM_CXVIRTUALSCREEN),
|
(int32_t)GetSystemMetrics(SM_CXVIRTUALSCREEN),
|
||||||
// (int32_t)GetSystemMetrics(SM_CYVIRTUALSCREEN));
|
(int32_t)GetSystemMetrics(SM_CYVIRTUALSCREEN));
|
||||||
// }
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user