Merge pull request #392 from go-vgo/bitmap-pr

add more mouse button support and Toggle() function style with KeyTog…
This commit is contained in:
Evans 2021-11-10 11:53:17 -05:00 committed by GitHub
commit 8839fc2157
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 109 additions and 35 deletions

View File

@ -23,10 +23,14 @@ extern "C"
#include <ApplicationServices/ApplicationServices.h> #include <ApplicationServices/ApplicationServices.h>
// #include </System/Library/Frameworks/ApplicationServices.framework/Versions/A/Headers/ApplicationServices.h> // #include </System/Library/Frameworks/ApplicationServices.framework/Versions/A/Headers/ApplicationServices.h>
typedef enum { typedef enum {
LEFT_BUTTON = kCGMouseButtonLeft, LEFT_BUTTON = kCGMouseButtonLeft,
RIGHT_BUTTON = kCGMouseButtonRight, RIGHT_BUTTON = kCGMouseButtonRight,
CENTER_BUTTON = kCGMouseButtonCenter CENTER_BUTTON = kCGMouseButtonCenter,
WheelDown = 4,
WheelUp = 5,
WheelLeft = 6,
WheelRight = 7,
} MMMouseButton; } MMMouseButton;
#elif defined(USE_X11) #elif defined(USE_X11)
@ -34,7 +38,11 @@ extern "C"
enum _MMMouseButton { enum _MMMouseButton {
LEFT_BUTTON = 1, LEFT_BUTTON = 1,
CENTER_BUTTON = 2, CENTER_BUTTON = 2,
RIGHT_BUTTON = 3 RIGHT_BUTTON = 3,
WheelDown = 4,
WheelUp = 5,
WheelLeft = 6,
WheelRight = 7,
}; };
typedef unsigned int MMMouseButton; typedef unsigned int MMMouseButton;
@ -43,7 +51,11 @@ extern "C"
enum _MMMouseButton { enum _MMMouseButton {
LEFT_BUTTON = 1, LEFT_BUTTON = 1,
CENTER_BUTTON = 2, CENTER_BUTTON = 2,
RIGHT_BUTTON = 3 RIGHT_BUTTON = 3,
WheelDown = 4,
WheelUp = 5,
WheelLeft = 6,
WheelRight = 7,
}; };
typedef unsigned int MMMouseButton; typedef unsigned int MMMouseButton;
@ -53,10 +65,11 @@ extern "C"
#define MMMouseButtonIsValid(button) \ #define MMMouseButtonIsValid(button) \
(button == LEFT_BUTTON || button == RIGHT_BUTTON || \ (button == LEFT_BUTTON || button == RIGHT_BUTTON || \
button == CENTER_BUTTON) button == CENTER_BUTTON || button == WheelDown || \
button == WheelUp || button == WheelLeft || \
button == WheelRight)
enum __MMMouseWheelDirection enum __MMMouseWheelDirection {
{
DIRECTION_DOWN = -1, DIRECTION_DOWN = -1,
DIRECTION_UP = 1 DIRECTION_UP = 1
}; };

View File

@ -415,16 +415,17 @@ func ToRGBA(bit C.MMBitmapRef) *image.RGBA {
// CheckMouse check the mouse button // CheckMouse check the mouse button
func CheckMouse(btn string) C.MMMouseButton { func CheckMouse(btn string) C.MMMouseButton {
// button = args[0].(C.MMMouseButton) // button = args[0].(C.MMMouseButton)
if btn == "left" { m1 := map[string]C.MMMouseButton{
return C.LEFT_BUTTON "left": C.LEFT_BUTTON,
"center": C.CENTER_BUTTON,
"right": C.RIGHT_BUTTON,
"wheelDown": C.WheelDown,
"wheelUp": C.WheelUp,
"wheelLeft": C.WheelLeft,
"wheelRight": C.WheelRight,
} }
if v, ok := m1[btn]; ok {
if btn == "center" { return v
return C.CENTER_BUTTON
}
if btn == "right" {
return C.RIGHT_BUTTON
} }
return C.LEFT_BUTTON return C.LEFT_BUTTON
@ -436,6 +437,10 @@ func MoveMouse(x, y int) {
} }
// Move move the mouse to (x, y) // Move move the mouse to (x, y)
//
// Examples:
// robotgo.MouseSleep = 100 // 100 millisecond
// robotgo.Move(10, 10)
func Move(x, y int) { func Move(x, y int) {
cx := C.int32_t(x) cx := C.int32_t(x)
cy := C.int32_t(y) cy := C.int32_t(y)
@ -445,9 +450,13 @@ func Move(x, y int) {
} }
// DragMouse drag the mouse to (x, y), // DragMouse drag the mouse to (x, y),
// It's not valid now, use the DragSmooth() // It's same with the DragSmooth() now
func DragMouse(x, y int, args ...string) { func DragMouse(x, y int, args ...interface{}) {
Drag(x, y, args...) Toggle("left")
MilliSleep(50)
// Drag(x, y, args...)
MoveSmooth(x, y, args...)
Toggle("left", "up")
} }
// Drag drag the mouse to (x, y), // Drag drag the mouse to (x, y),
@ -465,12 +474,15 @@ func Drag(x, y int, args ...string) {
MilliSleep(MouseSleep) MilliSleep(MouseSleep)
} }
// DragSmooth drag the mouse smooth // DragSmooth drag the mouse like smooth to (x, y)
//
// Examples:
// robotgo.DragSmooth(10, 10)
func DragSmooth(x, y int, args ...interface{}) { func DragSmooth(x, y int, args ...interface{}) {
MouseToggle("down") Toggle("left")
MilliSleep(50) MilliSleep(50)
MoveSmooth(x, y, args...) MoveSmooth(x, y, args...)
MouseToggle("up") Toggle("left", "up")
} }
// MoveMouseSmooth move the mouse smooth, // MoveMouseSmooth move the mouse smooth,
@ -483,6 +495,10 @@ func MoveMouseSmooth(x, y int, args ...interface{}) bool {
// moves mouse to x, y human like, with the mouse button up. // moves mouse to x, y human like, with the mouse button up.
// //
// robotgo.MoveSmooth(x, y int, low, high float64, mouseDelay int) // robotgo.MoveSmooth(x, y int, low, high float64, mouseDelay int)
//
// Examples:
// robotgo.MoveSmooth(10, 10)
// robotgo.MoveSmooth(10, 10, 1.0, 2.0)
func MoveSmooth(x, y int, args ...interface{}) bool { func MoveSmooth(x, y int, args ...interface{}) bool {
cx := C.int32_t(x) cx := C.int32_t(x)
cy := C.int32_t(y) cy := C.int32_t(y)
@ -511,7 +527,7 @@ func MoveSmooth(x, y int, args ...interface{}) bool {
return bool(cbool) return bool(cbool)
} }
// MoveArgs move mouse relative args // MoveArgs get the mouse relative args
func MoveArgs(x, y int) (int, int) { func MoveArgs(x, y int) (int, int) {
mx, my := GetMousePos() mx, my := GetMousePos()
mx = mx + x mx = mx + x
@ -531,7 +547,7 @@ func MoveSmoothRelative(x, y int, args ...interface{}) {
MoveSmooth(mx, my, args...) MoveSmooth(mx, my, args...)
} }
// GetMousePos get mouse's portion // GetMousePos get mouse's portion return x, y
func GetMousePos() (int, int) { func GetMousePos() (int, int) {
pos := C.get_mouse_pos() pos := C.get_mouse_pos()
@ -548,9 +564,14 @@ func MouseClick(args ...interface{}) {
Click(args...) Click(args...)
} }
// Click click the mouse // Click click the mouse button
// //
// robotgo.Click(button string, double bool) // robotgo.Click(button string, double bool)
//
// Examples:
// robotgo.Click() // default is left button
// robotgo.Click("right")
// robotgo.Click("wheelLeft")
func Click(args ...interface{}) { func Click(args ...interface{}) {
var ( var (
button C.MMMouseButton = C.LEFT_BUTTON button C.MMMouseButton = C.LEFT_BUTTON
@ -583,7 +604,33 @@ func MovesClick(x, y int, args ...interface{}) {
MouseClick(args...) MouseClick(args...)
} }
// Toggle toggle the mouse, support button: "left", "center", "right",
// "wheelDown", "wheelUp", "wheelLeft", "wheelRight"
//
// Examples:
// robotgo.Toggle("left") // default is down
// robotgo.Toggle("left", "up")
func Toggle(key ...string) int {
var button C.MMMouseButton = C.LEFT_BUTTON
if len(key) > 0 {
button = CheckMouse(key[0])
}
down := C.CString("down")
if len(key) > 1 {
down = C.CString(key[1])
}
i := C.mouse_toggle(down, button)
C.free(unsafe.Pointer(down))
MilliSleep(MouseSleep)
return int(i)
}
// MouseToggle toggle the mouse // MouseToggle toggle the mouse
//
// Examples:
// robotgo.MouseToggle("down", "right")
// robotgo.MouseToggle("up", "right")
func MouseToggle(togKey string, args ...interface{}) int { func MouseToggle(togKey string, args ...interface{}) int {
var button C.MMMouseButton = C.LEFT_BUTTON var button C.MMMouseButton = C.LEFT_BUTTON
@ -599,7 +646,11 @@ func MouseToggle(togKey string, args ...interface{}) int {
return int(i) return int(i)
} }
// ScrollMouse scroll the mouse // ScrollMouse scroll the mouse to (x, "up")
//
// Examples:
// robotgo.ScrollMouse(10, "down")
// robotgo.ScrollMouse(10, "up")
func ScrollMouse(x int, direction string) { func ScrollMouse(x int, direction string) {
cx := C.size_t(x) cx := C.size_t(x)
cy := C.CString(direction) cy := C.CString(direction)
@ -612,6 +663,9 @@ func ScrollMouse(x int, direction string) {
// Scroll scroll the mouse to (x, y) // Scroll scroll the mouse to (x, y)
// //
// robotgo.Scroll(x, y, msDelay int) // robotgo.Scroll(x, y, msDelay int)
//
// Examples:
// robotgo.Scroll(10, 10)
func Scroll(x, y int, args ...int) { func Scroll(x, y int, args ...int) {
var msDelay = 10 var msDelay = 10
if len(args) > 0 { if len(args) > 0 {
@ -627,6 +681,9 @@ func Scroll(x, y int, args ...int) {
} }
// ScrollRelative scroll mouse with relative // ScrollRelative scroll mouse with relative
//
// Examples:
// robotgo.ScrollRelative(10, 10)
func ScrollRelative(x, y int, args ...int) { func ScrollRelative(x, y int, args ...int) {
mx, my := MoveArgs(x, y) mx, my := MoveArgs(x, y)
Scroll(mx, my, args...) Scroll(mx, my, args...)
@ -654,11 +711,12 @@ func SetMouseDelay(delay int) {
// https://github.com/go-vgo/robotgo/blob/master/docs/keys.md // https://github.com/go-vgo/robotgo/blob/master/docs/keys.md
// //
// Examples: // Examples:
// robotgo.KeyTap("a") // robotgo.KeySleep = 100 // 100 millisecond
// robotgo.KeyTap("i", "alt", "command") // robotgo.KeyTap("a")
// robotgo.KeyTap("i", "alt", "command")
// //
// arr := []string{"alt", "command"} // arr := []string{"alt", "command"}
// robotgo.KeyTap("i", arr) // robotgo.KeyTap("i", arr)
// //
func KeyTap(tapKey string, args ...interface{}) string { func KeyTap(tapKey string, args ...interface{}) string {
var ( var (
@ -754,10 +812,10 @@ func KeyTap(tapKey string, args ...interface{}) string {
// https://github.com/go-vgo/robotgo/blob/master/docs/keys.md // https://github.com/go-vgo/robotgo/blob/master/docs/keys.md
// //
// Examples: // Examples:
// robotgo.KeyToggle("a") // robotgo.KeyToggle("a")
// robotgo.KeyToggle("a", "up") // robotgo.KeyToggle("a", "up")
// //
// robotgo.KeyToggle("a", "up", "alt", "cmd") // robotgo.KeyToggle("a", "up", "alt", "cmd")
// //
func KeyToggle(key string, args ...string) string { func KeyToggle(key string, args ...string) string {
if len(args) <= 0 { if len(args) <= 0 {
@ -897,7 +955,7 @@ func inputUTF(str string) {
// robotgo.TypeStr(string: The string to send, float64: microsleep time, x11 option) // robotgo.TypeStr(string: The string to send, float64: microsleep time, x11 option)
// //
// Examples: // Examples:
// robotgo.TypeStr("abc@123, hi, こんにちは") // robotgo.TypeStr("abc@123, hi, こんにちは")
// //
func TypeStr(str string, args ...float64) { func TypeStr(str string, args ...float64) {
var tm, tm1 = 0.0, 7.0 var tm, tm1 = 0.0, 7.0
@ -1225,7 +1283,10 @@ func internalActive(pid int32, hwnd int) {
// C.active_PID(C.uintptr(pid), C.uintptr(hwnd)) // C.active_PID(C.uintptr(pid), C.uintptr(hwnd))
// } // }
// ActiveName active window by name // ActiveName active the window by name
//
// Examples:
// robotgo.ActiveName("chrome")
func ActiveName(name string) error { func ActiveName(name string) error {
pids, err := FindIds(name) pids, err := FindIds(name)
if err == nil && len(pids) > 0 { if err == nil && len(pids) > 0 {