Fixed: fixed mac key release and remove the toggle defaut sleep

This commit is contained in:
vcaesar 2023-01-15 09:12:40 -08:00
parent bdae1a7759
commit 95be5f752f
8 changed files with 51 additions and 42 deletions

View File

@ -4,6 +4,7 @@
<!--[![Build Status](https://travis-ci.org/go-vgo/robotgo.svg)](https://travis-ci.org/go-vgo/robotgo) <!--[![Build Status](https://travis-ci.org/go-vgo/robotgo.svg)](https://travis-ci.org/go-vgo/robotgo)
[![codecov](https://codecov.io/gh/go-vgo/robotgo/branch/master/graph/badge.svg)](https://codecov.io/gh/go-vgo/robotgo)--> [![codecov](https://codecov.io/gh/go-vgo/robotgo/branch/master/graph/badge.svg)](https://codecov.io/gh/go-vgo/robotgo)-->
<!--<a href="https://circleci.com/gh/go-vgo/robotgo/tree/dev"><img src="https://img.shields.io/circleci/project/go-vgo/robotgo/dev.svg" alt="Build Status"></a>--> <!--<a href="https://circleci.com/gh/go-vgo/robotgo/tree/dev"><img src="https://img.shields.io/circleci/project/go-vgo/robotgo/dev.svg" alt="Build Status"></a>-->
[![Build Status](https://github.com/go-vgo/robotgo/workflows/Go/badge.svg)](https://github.com/go-vgo/robotgo/commits/master) [![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) [![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) [![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) [![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) [![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) [![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)
<!-- [![Release](https://github-release-version.herokuapp.com/github/go-vgo/robotgo/release.svg?style=flat)](https://github.com/go-vgo/robotgo/releases/latest) --> <!-- [![Release](https://github-release-version.herokuapp.com/github/go-vgo/robotgo/release.svg?style=flat)](https://github.com/go-vgo/robotgo/releases/latest) -->
<!-- <a href="https://github.com/go-vgo/robotgo/releases"><img src="https://img.shields.io/badge/%20version%20-%206.0.0%20-blue.svg?style=flat-square" alt="Releases"></a> --> <!-- <a href="https://github.com/go-vgo/robotgo/releases"><img src="https://img.shields.io/badge/%20version%20-%206.0.0%20-blue.svg?style=flat-square" alt="Releases"></a> -->
@ -20,6 +22,7 @@
RobotGo supports Mac, Windows, and Linux(X11); and robotgo supports arm64 and x86-amd64. RobotGo supports Mac, Windows, and Linux(X11); and robotgo supports arm64 and x86-amd64.
## Contents ## Contents
- [Docs](#docs) - [Docs](#docs)
- [Binding](#binding) - [Binding](#binding)
- [Requirements](#requirements) - [Requirements](#requirements)
@ -34,10 +37,12 @@ RobotGo supports Mac, Windows, and Linux(X11); and robotgo supports arm64 and x8
- [License](#license) - [License](#license)
## Docs ## Docs
- [GoDoc](https://godoc.org/github.com/go-vgo/robotgo) <br>
- [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) <br>
- [API Docs](https://github.com/go-vgo/robotgo/blob/master/docs/doc.md) (Deprecated, no updated)
## Binding: ## Binding:
[ADB](https://github.com/vcaesar/adb), packaging android adb API. [ADB](https://github.com/vcaesar/adb), packaging android adb API.
[Robotn](https://github.com/vcaesar/robotn), binding JavaScript and other, support more language. [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. Now, Please make sure `Golang, GCC` is installed correctly before installing RobotGo.
### ALL: ### ALL:
``` ```
Golang Golang
@ -63,14 +69,12 @@ xcode-select --install
#### For Windows: #### 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. 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). [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: #### For everything else:
@ -115,7 +119,7 @@ sudo apt install xsel xclip
##### Fedora: ##### Fedora:
```yml ```yml
sudo dnf install libXtst-devel sudo dnf install libXtst-devel
# Bitmap # Bitmap
sudo dnf install libpng-devel 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). png.h: No such file or directory? Please see [issues/47](https://github.com/go-vgo/robotgo/issues/47).
## Update: ## Update:
``` ```
go get -u github.com/go-vgo/robotgo 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). 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). `go mod vendor` problem, [golang #26366](https://github.com/golang/go/issues/26366).
## [Examples:](https://github.com/go-vgo/robotgo/blob/master/examples) ## [Examples:](https://github.com/go-vgo/robotgo/blob/master/examples)
#### [Mouse](https://github.com/go-vgo/robotgo/blob/master/examples/mouse/main.go) #### [Mouse](https://github.com/go-vgo/robotgo/blob/master/examples/mouse/main.go)
@ -486,6 +490,7 @@ func main() {
## CrossCompiling ## CrossCompiling
##### Windows64 to windows32 ##### Windows64 to windows32
```Go ```Go
SET CGO_ENABLED=1 SET CGO_ENABLED=1
SET GOARCH=386 SET GOARCH=386
@ -495,6 +500,7 @@ go build main.go
#### Other to windows #### Other to windows
Install Requirements (Ubuntu, Just used by bitmap.): Install Requirements (Ubuntu, Just used by bitmap.):
```bash ```bash
sudo apt install gcc-multilib sudo apt install gcc-multilib
sudo apt install gcc-mingw-w64 sudo apt install gcc-mingw-w64
@ -503,6 +509,7 @@ sudo apt install libz-mingw-w64-dev
``` ```
Build the binary: Build the binary:
```Go ```Go
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ go build -x ./ 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). 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 ## Authors
* [The author is vz](https://github.com/vcaesar)
* [Maintainers](https://github.com/orgs/go-vgo/people) - [The author is vz](https://github.com/vcaesar)
* [Contributors](https://github.com/go-vgo/robotgo/graphs/contributors) - [Maintainers](https://github.com/orgs/go-vgo/people)
- [Contributors](https://github.com/go-vgo/robotgo/graphs/contributors)
## Plans ## Plans
- Refactor some C code to Go (such as x11, windows) - Refactor some C code to Go (such as x11, windows)
- Better multiscreen support - Better multiscreen support
- Wayland support - Wayland support

View File

@ -68,7 +68,7 @@ import (
) )
func main() { func main() {
x, y := robotgo.GetMousePos() x, y := robotgo.Location()
fmt.Println("pos:", x, y) fmt.Println("pos:", x, y)
color := robotgo.GetPixelColor(100, 200) color := robotgo.GetPixelColor(100, 200)
fmt.Println("color----", color) fmt.Println("color----", color)

View File

@ -22,7 +22,7 @@ func main() {
robotx, roboty := 35*s/100, 25*s/100 robotx, roboty := 35*s/100, 25*s/100
fmt.Println("scale: ", sx, s, " pos: ", robotx, roboty) fmt.Println("scale: ", sx, s, " pos: ", robotx, roboty)
mx, my := robotgo.GetMousePos() mx, my := robotgo.Location()
sx, sy := mx*s/100, my*s/100 sx, sy := mx*s/100, my*s/100
rx, ry, rw, rh := sx, sy, robotx, roboty rx, ry, rw, rh := sx, sy, robotx, roboty

6
key.go
View File

@ -417,7 +417,7 @@ func keyTaps(k string, keyArr []string, pid int) error {
return nil 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 { if len(keyArr) <= 0 {
keyArr = append(keyArr, "down") 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)) C.toggleKeyCode(key, C.bool(down), flags, C.uintptr(pid))
MilliSleep(KeySleep) if len(args) > 0 {
MilliSleep(KeySleep)
}
return nil return nil
} }

View File

@ -60,6 +60,8 @@
} else { } else {
CGEventPost(kCGSessionEventTap, event); CGEventPost(kCGSessionEventTap, event);
} }
CFRelease(event);
return 0; return 0;
} }
@ -187,7 +189,6 @@ void toggleKeyCode(MMKeyCode code, const bool down, MMKeyFlags flags, uintptr pi
} }
SendTo(pid, keyEvent); SendTo(pid, keyEvent);
CFRelease(keyEvent);
} }
#elif defined(IS_WINDOWS) #elif defined(IS_WINDOWS)
const DWORD dwFlags = down ? 0 : KEYEVENTF_KEYUP; 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) { void toggleKey(char c, const bool down, MMKeyFlags flags, uintptr pid) {
MMKeyCode keyCode = keyCodeForChar(c); MMKeyCode keyCode = keyCodeForChar(c);
//Prevent unused variable warning for Mac and Linux.
#if defined(IS_WINDOWS)
int modifiers;
#endif
#if defined(USE_X11) #if defined(USE_X11)
if (toUpper(c) && !(flags & MOD_SHIFT)) { if (toUpper(c) && !(flags & MOD_SHIFT)) {
flags |= MOD_SHIFT; /* Not sure if this is safe for all layouts. */ 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 #endif
#if defined(IS_WINDOWS) #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 & 1) != 0) { flags |= MOD_SHIFT; } // Uptdate flags from keycode modifiers.
if ((modifiers & 2) != 0) { flags |= MOD_CONTROL; } if ((modifiers & 2) != 0) { flags |= MOD_CONTROL; }
if ((modifiers & 4) != 0) { flags |= MOD_ALT; } if ((modifiers & 4) != 0) { flags |= MOD_ALT; }
@ -286,7 +283,6 @@ void toggleUnicode(UniChar ch, const bool down, uintptr pid) {
CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch); CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch);
SendTo(pid, keyEvent); SendTo(pid, keyEvent);
CFRelease(keyEvent);
} }
#endif #endif

View File

@ -133,7 +133,7 @@ void dragMouse(MMPointInt32 point, const MMMouseButton button){
#endif #endif
} }
MMPointInt32 getMousePos() { MMPointInt32 location() {
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
CGEventRef event = CGEventCreate(NULL); CGEventRef event = CGEventCreate(NULL);
CGPoint point = CGEventGetLocation(event); CGPoint point = CGEventGetLocation(event);
@ -161,7 +161,7 @@ MMPointInt32 getMousePos() {
/* Press down a button, or release it. */ /* Press down a button, or release it. */
void toggleMouse(bool down, MMMouseButton button) { void toggleMouse(bool down, MMMouseButton button) {
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
const CGPoint currentPos = CGPointFromMMPointInt32(getMousePos()); const CGPoint currentPos = CGPointFromMMPointInt32(location());
const CGEventType mouseType = MMMouseToCGEventType(down, button); const CGEventType mouseType = MMMouseToCGEventType(down, button);
CGEventRef event = CGEventCreateMouseEvent(NULL, mouseType, currentPos, (CGMouseButton)button); CGEventRef event = CGEventCreateMouseEvent(NULL, mouseType, currentPos, (CGMouseButton)button);
@ -196,7 +196,7 @@ void clickMouse(MMMouseButton button){
void doubleClick(MMMouseButton button){ void doubleClick(MMMouseButton button){
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
/* Double click for Mac. */ /* Double click for Mac. */
const CGPoint currentPos = CGPointFromMMPointInt32(getMousePos()); const CGPoint currentPos = CGPointFromMMPointInt32(location());
const CGEventType mouseTypeDown = MMMouseToCGEventType(true, button); const CGEventType mouseTypeDown = MMMouseToCGEventType(true, button);
const CGEventType mouseTypeUP = MMMouseToCGEventType(false, 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){ bool smoothlyMoveMouse(MMPointInt32 endPoint, double lowSpeed, double highSpeed){
MMPointInt32 pos = getMousePos(); 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;

View File

@ -211,9 +211,9 @@ func GetPixelColor(x, y int, displayId ...int) string {
return PadHex(GetPxColor(x, y, displayId...)) return PadHex(GetPxColor(x, y, displayId...))
} }
// GetMouseColor get the mouse pos's color // GetLocationColor get the location pos's color
func GetMouseColor(displayId ...int) string { func GetLocationColor(displayId ...int) string {
x, y := GetMousePos() x, y := Location()
return GetPixelColor(x, y, displayId...) return GetPixelColor(x, y, displayId...)
} }
@ -651,7 +651,7 @@ func GetMousePos() (int, int) {
// Location get the mouse location position return x, y // Location get the mouse location position return x, y
func Location() (int, int) { func Location() (int, int) {
pos := C.getMousePos() pos := C.location()
x := int(pos.x) x := int(pos.x)
y := int(pos.y) y := int(pos.y)
@ -731,29 +731,31 @@ func MovesClick(x, y int, args ...interface{}) {
// //
// robotgo.Toggle("left") // default is down // robotgo.Toggle("left") // default is down
// robotgo.Toggle("left", "up") // robotgo.Toggle("left", "up")
func Toggle(key ...string) error { func Toggle(key ...interface{}) error {
var button C.MMMouseButton = C.LEFT_BUTTON var button C.MMMouseButton = C.LEFT_BUTTON
if len(key) > 0 { if len(key) > 0 {
button = CheckMouse(key[0]) button = CheckMouse(key[0].(string))
} }
down := true down := true
if len(key) > 1 && key[1] == "up" { if len(key) > 1 && key[1].(string) == "up" {
down = false down = false
} }
C.toggleMouse(C.bool(down), button) C.toggleMouse(C.bool(down), button)
MilliSleep(MouseSleep) if len(key) > 2 {
MilliSleep(MouseSleep)
}
return nil return nil
} }
// MouseDown send mouse down event // MouseDown send mouse down event
func MouseDown(key ...string) error { func MouseDown(key ...interface{}) error {
return Toggle(key...) return Toggle(key...)
} }
// MouseUp send mouse up event // MouseUp send mouse up event
func MouseUp(key ...string) error { func MouseUp(key ...interface{}) error {
if len(key) <= 0 { if len(key) <= 0 {
key = append(key, "left") key = append(key, "left")
} }

View File

@ -42,7 +42,7 @@ func TestSize(t *testing.T) {
func TestMoveMouse(t *testing.T) { func TestMoveMouse(t *testing.T) {
Move(20, 20) Move(20, 20)
MilliSleep(50) MilliSleep(50)
x, y := GetMousePos() x, y := Location()
tt.Equal(t, 20, x) tt.Equal(t, 20, x)
tt.Equal(t, 20, y) tt.Equal(t, 20, y)
@ -51,7 +51,7 @@ func TestMoveMouse(t *testing.T) {
func TestMoveMouseSmooth(t *testing.T) { func TestMoveMouseSmooth(t *testing.T) {
b := MoveSmooth(100, 100) b := MoveSmooth(100, 100)
MilliSleep(50) MilliSleep(50)
x, y := GetMousePos() x, y := Location()
tt.True(t, b) tt.True(t, b)
tt.Equal(t, 100, x) tt.Equal(t, 100, x)
@ -61,7 +61,7 @@ func TestMoveMouseSmooth(t *testing.T) {
func TestDragMouse(t *testing.T) { func TestDragMouse(t *testing.T) {
DragSmooth(500, 500) DragSmooth(500, 500)
MilliSleep(50) MilliSleep(50)
x, y := GetMousePos() x, y := Location()
tt.Equal(t, 500, x) tt.Equal(t, 500, x)
tt.Equal(t, 500, y) tt.Equal(t, 500, y)
@ -85,7 +85,7 @@ func TestMoveRelative(t *testing.T) {
MoveRelative(10, -10) MoveRelative(10, -10)
MilliSleep(50) MilliSleep(50)
x, y := GetMousePos() x, y := Location()
tt.Equal(t, 210, x) tt.Equal(t, 210, x)
tt.Equal(t, 190, y) tt.Equal(t, 190, y)
} }
@ -97,7 +97,7 @@ func TestMoveSmoothRelative(t *testing.T) {
MoveSmoothRelative(10, -10) MoveSmoothRelative(10, -10)
MilliSleep(50) MilliSleep(50)
x, y := GetMousePos() x, y := Location()
tt.Equal(t, 210, x) tt.Equal(t, 210, x)
tt.Equal(t, 190, y) tt.Equal(t, 190, y)
} }