add more mouse button support and Toggle() function style with KeyToggle()

This commit is contained in:
vcaesar 2021-11-10 12:12:29 -04:00
parent 39e066c5f7
commit dee7a282a0
2 changed files with 62 additions and 22 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
@ -445,9 +446,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 +470,12 @@ 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)
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,
@ -583,6 +588,28 @@ func MovesClick(x, y int, args ...interface{}) {
MouseClick(args...) MouseClick(args...)
} }
// Toggle toggle the mose, support: "left", "center", "right",
// "wheelDown", "wheelUp", "wheelLeft", "wheelRight"
//
// Examples:
// robotgo.Toggle("left", "up")
// robotgo.Toggle("left") // default is down
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
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