From 95be5f752fe2228d4b37febdbf3c9eaef7425534 Mon Sep 17 00:00:00 2001 From: vcaesar Date: Sun, 15 Jan 2023 09:12:40 -0800 Subject: [PATCH] Fixed: fixed mac key release and remove the toggle defaut sleep --- README.md | 31 ++++++++++++++++++++----------- examples/README.md | 2 +- examples/scale/main.go | 2 +- key.go | 6 ++++-- key/keypress_c.h | 12 ++++-------- mouse/mouse_c.h | 8 ++++---- robotgo.go | 22 ++++++++++++---------- robotgo_test.go | 10 +++++----- 8 files changed, 51 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 9223dc8..b7319a9 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ + [![Build Status](https://github.com/go-vgo/robotgo/workflows/Go/badge.svg)](https://github.com/go-vgo/robotgo/commits/master) [![CircleCI Status](https://circleci.com/gh/go-vgo/robotgo.svg?style=shield)](https://circleci.com/gh/go-vgo/robotgo) [![Build Status](https://travis-ci.org/go-vgo/robotgo.svg)](https://travis-ci.org/go-vgo/robotgo) @@ -12,6 +13,7 @@ [![GoDoc](https://godoc.org/github.com/go-vgo/robotgo?status.svg)](https://godoc.org/github.com/go-vgo/robotgo) [![GitHub release](https://img.shields.io/github/release/go-vgo/robotgo.svg)](https://github.com/go-vgo/robotgo/releases/latest) [![Join the chat at https://gitter.im/go-vgo/robotgo](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/go-vgo/robotgo?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) + @@ -20,6 +22,7 @@ RobotGo supports Mac, Windows, and Linux(X11); and robotgo supports arm64 and x86-amd64. ## Contents + - [Docs](#docs) - [Binding](#binding) - [Requirements](#requirements) @@ -34,10 +37,12 @@ RobotGo supports Mac, Windows, and Linux(X11); and robotgo supports arm64 and x8 - [License](#license) ## Docs - - [GoDoc](https://godoc.org/github.com/go-vgo/robotgo)
- - [API Docs](https://github.com/go-vgo/robotgo/blob/master/docs/doc.md) (Deprecated, no updated) + +- [GoDoc](https://godoc.org/github.com/go-vgo/robotgo)
+- [API Docs](https://github.com/go-vgo/robotgo/blob/master/docs/doc.md) (Deprecated, no updated) ## Binding: + [ADB](https://github.com/vcaesar/adb), packaging android adb API. [Robotn](https://github.com/vcaesar/robotn), binding JavaScript and other, support more language. @@ -47,6 +52,7 @@ RobotGo supports Mac, Windows, and Linux(X11); and robotgo supports arm64 and x8 Now, Please make sure `Golang, GCC` is installed correctly before installing RobotGo. ### ALL: + ``` Golang @@ -63,14 +69,12 @@ xcode-select --install #### For Windows: -[MinGW-w64](https://sourceforge.net/projects/mingw-w64/files) (Use recommended) +[MinGW-w64](https://sourceforge.net/projects/mingw-w64/files) (Use recommended) Download the Mingw, then set system environment variables `C:\mingw64\bin` to the Path. [Set environment variables to run GCC from command line](https://www.youtube.com/results?search_query=Set+environment+variables+to+run+GCC+from+command+line). -` -Or the other GCC (But you should compile the "libpng" with yourself when use the bitmap.) -` +`Or the other GCC (But you should compile the "libpng" with yourself when use the bitmap.)` #### For everything else: @@ -115,7 +119,7 @@ sudo apt install xsel xclip ##### Fedora: ```yml -sudo dnf install libXtst-devel +sudo dnf install libXtst-devel # Bitmap sudo dnf install libpng-devel @@ -144,6 +148,7 @@ go get github.com/go-vgo/robotgo png.h: No such file or directory? Please see [issues/47](https://github.com/go-vgo/robotgo/issues/47). ## Update: + ``` go get -u github.com/go-vgo/robotgo ``` @@ -151,7 +156,6 @@ go get -u github.com/go-vgo/robotgo Note go1.10.x C file compilation cache problem, [golang #24355](https://github.com/golang/go/issues/24355). `go mod vendor` problem, [golang #26366](https://github.com/golang/go/issues/26366). - ## [Examples:](https://github.com/go-vgo/robotgo/blob/master/examples) #### [Mouse](https://github.com/go-vgo/robotgo/blob/master/examples/mouse/main.go) @@ -486,6 +490,7 @@ func main() { ## CrossCompiling ##### Windows64 to windows32 + ```Go SET CGO_ENABLED=1 SET GOARCH=386 @@ -495,6 +500,7 @@ go build main.go #### Other to windows Install Requirements (Ubuntu, Just used by bitmap.): + ```bash sudo apt install gcc-multilib sudo apt install gcc-mingw-w64 @@ -503,6 +509,7 @@ sudo apt install libz-mingw-w64-dev ``` Build the binary: + ```Go GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ go build -x ./ ``` @@ -515,11 +522,13 @@ GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64 Some discussions and questions, please see [issues/228](https://github.com/go-vgo/robotgo/issues/228), [issues/143](https://github.com/go-vgo/robotgo/issues/143). ## Authors -* [The author is vz](https://github.com/vcaesar) -* [Maintainers](https://github.com/orgs/go-vgo/people) -* [Contributors](https://github.com/go-vgo/robotgo/graphs/contributors) + +- [The author is vz](https://github.com/vcaesar) +- [Maintainers](https://github.com/orgs/go-vgo/people) +- [Contributors](https://github.com/go-vgo/robotgo/graphs/contributors) ## Plans + - Refactor some C code to Go (such as x11, windows) - Better multiscreen support - Wayland support diff --git a/examples/README.md b/examples/README.md index af5c8ab..e7188e8 100644 --- a/examples/README.md +++ b/examples/README.md @@ -68,7 +68,7 @@ import ( ) func main() { - x, y := robotgo.GetMousePos() + x, y := robotgo.Location() fmt.Println("pos:", x, y) color := robotgo.GetPixelColor(100, 200) fmt.Println("color----", color) diff --git a/examples/scale/main.go b/examples/scale/main.go index 71e7371..d2baae4 100644 --- a/examples/scale/main.go +++ b/examples/scale/main.go @@ -22,7 +22,7 @@ func main() { robotx, roboty := 35*s/100, 25*s/100 fmt.Println("scale: ", sx, s, " pos: ", robotx, roboty) - mx, my := robotgo.GetMousePos() + mx, my := robotgo.Location() sx, sy := mx*s/100, my*s/100 rx, ry, rw, rh := sx, sy, robotx, roboty diff --git a/key.go b/key.go index 998452e..4f9b6dc 100644 --- a/key.go +++ b/key.go @@ -417,7 +417,7 @@ func keyTaps(k string, keyArr []string, pid int) error { return nil } -func keyToggles(k string, keyArr []string, pid int) error { +func keyToggles(k string, keyArr []string, pid int, args ...interface{}) error { if len(keyArr) <= 0 { keyArr = append(keyArr, "down") } @@ -438,7 +438,9 @@ func keyToggles(k string, keyArr []string, pid int) error { } C.toggleKeyCode(key, C.bool(down), flags, C.uintptr(pid)) - MilliSleep(KeySleep) + if len(args) > 0 { + MilliSleep(KeySleep) + } return nil } diff --git a/key/keypress_c.h b/key/keypress_c.h index 35c6d54..6064b4a 100644 --- a/key/keypress_c.h +++ b/key/keypress_c.h @@ -60,6 +60,8 @@ } else { CGEventPost(kCGSessionEventTap, event); } + + CFRelease(event); return 0; } @@ -187,7 +189,6 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, uintptr pi } SendTo(pid, keyEvent); - CFRelease(keyEvent); } #elif defined(IS_WINDOWS) const DWORD dwFlags = down ? 0 : KEYEVENTF_KEYUP; @@ -239,11 +240,6 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, uintptr pi void toggleKey(char c, const bool down, MMKeyFlags flags, uintptr pid) { MMKeyCode keyCode = keyCodeForChar(c); - //Prevent unused variable warning for Mac and Linux. - #if defined(IS_WINDOWS) - int modifiers; - #endif - #if defined(USE_X11) if (toUpper(c) && !(flags & MOD_SHIFT)) { flags |= MOD_SHIFT; /* Not sure if this is safe for all layouts. */ @@ -255,7 +251,8 @@ void toggleKey(char c, const bool down, MMKeyFlags flags, uintptr pid) { #endif #if defined(IS_WINDOWS) - modifiers = keyCode >> 8; // Pull out modifers. + int modifiers = keyCode >> 8; // Pull out modifers. + if ((modifiers & 1) != 0) { flags |= MOD_SHIFT; } // Uptdate flags from keycode modifiers. if ((modifiers & 2) != 0) { flags |= MOD_CONTROL; } if ((modifiers & 4) != 0) { flags |= MOD_ALT; } @@ -286,7 +283,6 @@ void toggleUnicode(UniChar ch, const bool down, uintptr pid) { CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch); SendTo(pid, keyEvent); - CFRelease(keyEvent); } #endif diff --git a/mouse/mouse_c.h b/mouse/mouse_c.h index a53bdf5..f273fbd 100644 --- a/mouse/mouse_c.h +++ b/mouse/mouse_c.h @@ -133,7 +133,7 @@ void dragMouse(MMPointInt32 point, const MMMouseButton button){ #endif } -MMPointInt32 getMousePos() { +MMPointInt32 location() { #if defined(IS_MACOSX) CGEventRef event = CGEventCreate(NULL); CGPoint point = CGEventGetLocation(event); @@ -161,7 +161,7 @@ MMPointInt32 getMousePos() { /* Press down a button, or release it. */ void toggleMouse(bool down, MMMouseButton button) { #if defined(IS_MACOSX) - const CGPoint currentPos = CGPointFromMMPointInt32(getMousePos()); + const CGPoint currentPos = CGPointFromMMPointInt32(location()); const CGEventType mouseType = MMMouseToCGEventType(down, button); CGEventRef event = CGEventCreateMouseEvent(NULL, mouseType, currentPos, (CGMouseButton)button); @@ -196,7 +196,7 @@ void clickMouse(MMMouseButton button){ void doubleClick(MMMouseButton button){ #if defined(IS_MACOSX) /* Double click for Mac. */ - const CGPoint currentPos = CGPointFromMMPointInt32(getMousePos()); + const CGPoint currentPos = CGPointFromMMPointInt32(location()); const CGEventType mouseTypeDown = MMMouseToCGEventType(true, button); const CGEventType mouseTypeUP = MMMouseToCGEventType(false, button); @@ -298,7 +298,7 @@ static double crude_hypot(double x, double y){ } bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed){ - MMPointInt32 pos = getMousePos(); + MMPointInt32 pos = location(); MMSizeInt32 screenSize = getMainDisplaySize(); double velo_x = 0.0, velo_y = 0.0; double distance; diff --git a/robotgo.go b/robotgo.go index dd902a3..0632996 100644 --- a/robotgo.go +++ b/robotgo.go @@ -211,9 +211,9 @@ func GetPixelColor(x, y int, displayId ...int) string { return PadHex(GetPxColor(x, y, displayId...)) } -// GetMouseColor get the mouse pos's color -func GetMouseColor(displayId ...int) string { - x, y := GetMousePos() +// GetLocationColor get the location pos's color +func GetLocationColor(displayId ...int) string { + x, y := Location() return GetPixelColor(x, y, displayId...) } @@ -651,7 +651,7 @@ func GetMousePos() (int, int) { // Location get the mouse location position return x, y func Location() (int, int) { - pos := C.getMousePos() + pos := C.location() x := int(pos.x) y := int(pos.y) @@ -731,29 +731,31 @@ func MovesClick(x, y int, args ...interface{}) { // // robotgo.Toggle("left") // default is down // robotgo.Toggle("left", "up") -func Toggle(key ...string) error { +func Toggle(key ...interface{}) error { var button C.MMMouseButton = C.LEFT_BUTTON if len(key) > 0 { - button = CheckMouse(key[0]) + button = CheckMouse(key[0].(string)) } down := true - if len(key) > 1 && key[1] == "up" { + if len(key) > 1 && key[1].(string) == "up" { down = false } C.toggleMouse(C.bool(down), button) - MilliSleep(MouseSleep) + if len(key) > 2 { + MilliSleep(MouseSleep) + } return nil } // MouseDown send mouse down event -func MouseDown(key ...string) error { +func MouseDown(key ...interface{}) error { return Toggle(key...) } // MouseUp send mouse up event -func MouseUp(key ...string) error { +func MouseUp(key ...interface{}) error { if len(key) <= 0 { key = append(key, "left") } diff --git a/robotgo_test.go b/robotgo_test.go index f5ff861..82d0e9e 100644 --- a/robotgo_test.go +++ b/robotgo_test.go @@ -42,7 +42,7 @@ func TestSize(t *testing.T) { func TestMoveMouse(t *testing.T) { Move(20, 20) MilliSleep(50) - x, y := GetMousePos() + x, y := Location() tt.Equal(t, 20, x) tt.Equal(t, 20, y) @@ -51,7 +51,7 @@ func TestMoveMouse(t *testing.T) { func TestMoveMouseSmooth(t *testing.T) { b := MoveSmooth(100, 100) MilliSleep(50) - x, y := GetMousePos() + x, y := Location() tt.True(t, b) tt.Equal(t, 100, x) @@ -61,7 +61,7 @@ func TestMoveMouseSmooth(t *testing.T) { func TestDragMouse(t *testing.T) { DragSmooth(500, 500) MilliSleep(50) - x, y := GetMousePos() + x, y := Location() tt.Equal(t, 500, x) tt.Equal(t, 500, y) @@ -85,7 +85,7 @@ func TestMoveRelative(t *testing.T) { MoveRelative(10, -10) MilliSleep(50) - x, y := GetMousePos() + x, y := Location() tt.Equal(t, 210, x) tt.Equal(t, 190, y) } @@ -97,7 +97,7 @@ func TestMoveSmoothRelative(t *testing.T) { MoveSmoothRelative(10, -10) MilliSleep(50) - x, y := GetMousePos() + x, y := Location() tt.Equal(t, 210, x) tt.Equal(t, 190, y) }