Skip to content

Commit

Permalink
Merge pull request #555 from go-vgo/bitmap-pr
Browse files Browse the repository at this point in the history
Fixed: fixed mac key release and remove the toggle defaut sleep
  • Loading branch information
vcaesar authored Jan 15, 2023
2 parents 4bc64eb + 95be5f7 commit 9f8d308
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 42 deletions.
31 changes: 20 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<!--[![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)-->
<!--<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)
[![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)
Expand All @@ -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)

<!-- [![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> -->

Expand All @@ -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)
Expand All @@ -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) <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:

[ADB](https://github.com/vcaesar/adb), packaging android adb API.

[Robotn](https://github.com/vcaesar/robotn), binding JavaScript and other, support more language.
Expand All @@ -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
Expand All @@ -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:

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -144,14 +148,14 @@ 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
```

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)
Expand Down Expand Up @@ -486,6 +490,7 @@ func main() {
## CrossCompiling

##### Windows64 to windows32

```Go
SET CGO_ENABLED=1
SET GOARCH=386
Expand All @@ -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
Expand All @@ -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 ./
```
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/scale/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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
}

Expand Down
12 changes: 4 additions & 8 deletions key/keypress_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
} else {
CGEventPost(kCGSessionEventTap, event);
}

CFRelease(event);
return 0;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand All @@ -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; }
Expand Down Expand Up @@ -286,7 +283,6 @@ void toggleUnicode(UniChar ch, const bool down, uintptr pid) {
CGEventKeyboardSetUnicodeString(keyEvent, 1, &ch);

SendTo(pid, keyEvent);
CFRelease(keyEvent);
}
#endif

Expand Down
8 changes: 4 additions & 4 deletions mouse/mouse_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
22 changes: 12 additions & 10 deletions robotgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
}

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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")
}
Expand Down
10 changes: 5 additions & 5 deletions robotgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 9f8d308

Please sign in to comment.