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