mirror of
https://github.com/go-vgo/robotgo.git
synced 2025-06-03 07:33:55 +00:00
add scroll mouse support x, y
This commit is contained in:
parent
4544fabb91
commit
0a93336c11
@ -110,6 +110,7 @@ int mouse_toggle(char* d, MMMouseButton button){
|
|||||||
|
|
||||||
toggleMouse(down, button);
|
toggleMouse(down, button);
|
||||||
microsleep(mouseDelay);
|
microsleep(mouseDelay);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -120,6 +121,13 @@ int set_mouse_delay(size_t val){
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int scroll(int x, int y, int msDelay){
|
||||||
|
scrollMouseXY(x, y);
|
||||||
|
microsleep(msDelay);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int scroll_mouse(size_t scrollMagnitude, char *s){
|
int scroll_mouse(size_t scrollMagnitude, char *s){
|
||||||
// int scrollMagnitude = 20;
|
// int scrollMagnitude = 20;
|
||||||
|
|
||||||
|
104
mouse/mouse_c.h
104
mouse/mouse_c.h
@ -66,8 +66,7 @@
|
|||||||
* @param event The mouse move event (by ref).
|
* @param event The mouse move event (by ref).
|
||||||
* @param point The new mouse x and y.
|
* @param point The new mouse x and y.
|
||||||
*/
|
*/
|
||||||
void calculateDeltas(CGEventRef *event, MMPoint point)
|
void calculateDeltas(CGEventRef *event, MMPoint point){
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* The next few lines are a workaround for games not detecting mouse moves.
|
* The next few lines are a workaround for games not detecting mouse moves.
|
||||||
* See this issue for more information:
|
* See this issue for more information:
|
||||||
@ -92,8 +91,7 @@ void calculateDeltas(CGEventRef *event, MMPoint point)
|
|||||||
* Move the mouse to a specific point.
|
* Move the mouse to a specific point.
|
||||||
* @param point The coordinates to move the mouse to (x, y).
|
* @param point The coordinates to move the mouse to (x, y).
|
||||||
*/
|
*/
|
||||||
void moveMouse(MMPoint point)
|
void moveMouse(MMPoint point){
|
||||||
{
|
|
||||||
#if defined(IS_MACOSX)
|
#if defined(IS_MACOSX)
|
||||||
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
|
CGEventRef move = CGEventCreateMouseEvent(NULL, kCGEventMouseMoved,
|
||||||
CGPointFromMMPoint(point),
|
CGPointFromMMPoint(point),
|
||||||
@ -126,8 +124,7 @@ void moveMouse(MMPoint point)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void dragMouse(MMPoint point, const MMMouseButton button)
|
void dragMouse(MMPoint point, const MMMouseButton button){
|
||||||
{
|
|
||||||
#if defined(IS_MACOSX)
|
#if defined(IS_MACOSX)
|
||||||
const CGEventType dragType = MMMouseDragToCGEventType(button);
|
const CGEventType dragType = MMMouseDragToCGEventType(button);
|
||||||
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
|
CGEventRef drag = CGEventCreateMouseEvent(NULL, dragType,
|
||||||
@ -142,8 +139,7 @@ void dragMouse(MMPoint point, const MMMouseButton button)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
MMPoint getMousePos()
|
MMPoint getMousePos(){
|
||||||
{
|
|
||||||
#if defined(IS_MACOSX)
|
#if defined(IS_MACOSX)
|
||||||
CGEventRef event = CGEventCreate(NULL);
|
CGEventRef event = CGEventCreate(NULL);
|
||||||
CGPoint point = CGEventGetLocation(event);
|
CGPoint point = CGEventGetLocation(event);
|
||||||
@ -174,8 +170,7 @@ MMPoint getMousePos()
|
|||||||
* @param down True for down, false for up.
|
* @param down True for down, false for up.
|
||||||
* @param button The button to press down or release.
|
* @param button The button to press down or release.
|
||||||
*/
|
*/
|
||||||
void toggleMouse(bool down, MMMouseButton button)
|
void toggleMouse(bool down, MMMouseButton button){
|
||||||
{
|
|
||||||
#if defined(IS_MACOSX)
|
#if defined(IS_MACOSX)
|
||||||
const CGPoint currentPos = CGPointFromMMPoint(getMousePos());
|
const CGPoint currentPos = CGPointFromMMPoint(getMousePos());
|
||||||
const CGEventType mouseType = MMMouseToCGEventType(down, button);
|
const CGEventType mouseType = MMMouseToCGEventType(down, button);
|
||||||
@ -194,8 +189,7 @@ void toggleMouse(bool down, MMMouseButton button)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void clickMouse(MMMouseButton button)
|
void clickMouse(MMMouseButton button){
|
||||||
{
|
|
||||||
toggleMouse(true, button);
|
toggleMouse(true, button);
|
||||||
toggleMouse(false, button);
|
toggleMouse(false, button);
|
||||||
}
|
}
|
||||||
@ -204,9 +198,7 @@ void clickMouse(MMMouseButton button)
|
|||||||
* Special function for sending double clicks, needed for Mac OS X.
|
* Special function for sending double clicks, needed for Mac OS X.
|
||||||
* @param button Button to click.
|
* @param button Button to click.
|
||||||
*/
|
*/
|
||||||
void doubleClick(MMMouseButton button)
|
void doubleClick(MMMouseButton button){
|
||||||
{
|
|
||||||
|
|
||||||
#if defined(IS_MACOSX)
|
#if defined(IS_MACOSX)
|
||||||
|
|
||||||
/* Double click for Mac. */
|
/* Double click for Mac. */
|
||||||
@ -241,8 +233,7 @@ void doubleClick(MMMouseButton button)
|
|||||||
* This uses the magnitude to scroll the required amount in the direction.
|
* This uses the magnitude to scroll the required amount in the direction.
|
||||||
* TODO Requires further fine tuning based on the requirements.
|
* TODO Requires further fine tuning based on the requirements.
|
||||||
*/
|
*/
|
||||||
void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection)
|
void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection){
|
||||||
{
|
|
||||||
#if defined(IS_WINDOWS)
|
#if defined(IS_WINDOWS)
|
||||||
// Fix for #97 https://github.com/go-vgo/robotgo/issues/97,
|
// Fix for #97 https://github.com/go-vgo/robotgo/issues/97,
|
||||||
// C89 needs variables declared on top of functions (mouseScrollInput)
|
// C89 needs variables declared on top of functions (mouseScrollInput)
|
||||||
@ -252,8 +243,7 @@ void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection)
|
|||||||
/* Direction should only be considered based on the scrollDirection. This
|
/* Direction should only be considered based on the scrollDirection. This
|
||||||
* Should not interfere. */
|
* Should not interfere. */
|
||||||
int cleanScrollMagnitude = abs(scrollMagnitude);
|
int cleanScrollMagnitude = abs(scrollMagnitude);
|
||||||
if (!(scrollDirection == DIRECTION_UP || scrollDirection == DIRECTION_DOWN))
|
if (!(scrollDirection == DIRECTION_UP || scrollDirection == DIRECTION_DOWN)){
|
||||||
{
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -303,6 +293,76 @@ void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void scrollMouseXY(int x, int y){
|
||||||
|
#if defined(IS_WINDOWS)
|
||||||
|
// Fix for #97,
|
||||||
|
// C89 needs variables declared on top of functions (mouseScrollInput)
|
||||||
|
INPUT mouseScrollInputH;
|
||||||
|
INPUT mouseScrollInputV;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/* Direction should only be considered based on the scrollDirection. This
|
||||||
|
* Should not interfere. */
|
||||||
|
|
||||||
|
/* Set up the OS specific solution */
|
||||||
|
#if defined(__APPLE__)
|
||||||
|
|
||||||
|
CGEventRef event;
|
||||||
|
|
||||||
|
event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x);
|
||||||
|
CGEventPost(kCGHIDEventTap, event);
|
||||||
|
|
||||||
|
CFRelease(event);
|
||||||
|
|
||||||
|
#elif defined(USE_X11)
|
||||||
|
|
||||||
|
int ydir = 4; /* Button 4 is up, 5 is down. */
|
||||||
|
int xdir = 6;
|
||||||
|
Display *display = XGetMainDisplay();
|
||||||
|
|
||||||
|
if (y < 0){
|
||||||
|
ydir = 5;
|
||||||
|
}
|
||||||
|
if (x < 0){
|
||||||
|
xdir = 7;
|
||||||
|
}
|
||||||
|
|
||||||
|
int xi;
|
||||||
|
int yi;
|
||||||
|
for (xi = 0; xi < abs(x); xi++) {
|
||||||
|
XTestFakeButtonEvent(display, xdir, 1, CurrentTime);
|
||||||
|
XTestFakeButtonEvent(display, xdir, 0, CurrentTime);
|
||||||
|
}
|
||||||
|
for (yi = 0; yi < abs(y); yi++) {
|
||||||
|
XTestFakeButtonEvent(display, ydir, 1, CurrentTime);
|
||||||
|
XTestFakeButtonEvent(display, ydir, 0, CurrentTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
XFlush(display);
|
||||||
|
|
||||||
|
#elif defined(IS_WINDOWS)
|
||||||
|
|
||||||
|
mouseScrollInputH.type = INPUT_MOUSE;
|
||||||
|
mouseScrollInputH.mi.dx = 0;
|
||||||
|
mouseScrollInputH.mi.dy = 0;
|
||||||
|
mouseScrollInputH.mi.dwFlags = MOUSEEVENTF_WHEEL;
|
||||||
|
mouseScrollInputH.mi.time = 0;
|
||||||
|
mouseScrollInputH.mi.dwExtraInfo = 0;
|
||||||
|
mouseScrollInputH.mi.mouseData = WHEEL_DELTA * x;
|
||||||
|
|
||||||
|
mouseScrollInputV.type = INPUT_MOUSE;
|
||||||
|
mouseScrollInputV.mi.dx = 0;
|
||||||
|
mouseScrollInputV.mi.dy = 0;
|
||||||
|
mouseScrollInputV.mi.dwFlags = MOUSEEVENTF_HWHEEL;
|
||||||
|
mouseScrollInputV.mi.time = 0;
|
||||||
|
mouseScrollInputV.mi.dwExtraInfo = 0;
|
||||||
|
mouseScrollInputV.mi.mouseData = WHEEL_DELTA * y;
|
||||||
|
|
||||||
|
SendInput(1, &mouseScrollInputH, sizeof(mouseScrollInputH));
|
||||||
|
SendInput(1, &mouseScrollInputV, sizeof(mouseScrollInputV));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* A crude, fast hypot() approximation to get around the fact that hypot() is
|
* A crude, fast hypot() approximation to get around the fact that hypot() is
|
||||||
* not a standard ANSI C function.
|
* not a standard ANSI C function.
|
||||||
@ -313,8 +373,7 @@ void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection)
|
|||||||
* http://stackoverflow.com/questions/3506404/fast-hypotenuse-algorithm-for-embedded-processor#3507882
|
* http://stackoverflow.com/questions/3506404/fast-hypotenuse-algorithm-for-embedded-processor#3507882
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static double crude_hypot(double x, double y)
|
static double crude_hypot(double x, double y){
|
||||||
{
|
|
||||||
double big = fabs(x); /* max(|x|, |y|) */
|
double big = fabs(x); /* max(|x|, |y|) */
|
||||||
double small = fabs(y); /* min(|x|, |y|) */
|
double small = fabs(y); /* min(|x|, |y|) */
|
||||||
|
|
||||||
@ -327,8 +386,7 @@ static double crude_hypot(double x, double y)
|
|||||||
return ((M_SQRT2 - 1.0) * small) + big;
|
return ((M_SQRT2 - 1.0) * small) + big;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed)
|
bool smoothlyMoveMouse(MMPoint endPoint, double lowSpeed, double highSpeed){
|
||||||
{
|
|
||||||
MMPoint pos = getMousePos();
|
MMPoint pos = getMousePos();
|
||||||
MMSize screenSize = getMainDisplaySize();
|
MMSize screenSize = getMainDisplaySize();
|
||||||
double velo_x = 0.0, velo_y = 0.0;
|
double velo_x = 0.0, velo_y = 0.0;
|
||||||
|
20
robotgo.go
20
robotgo.go
@ -543,9 +543,23 @@ func SetMouseDelay(x int) {
|
|||||||
// ScrollMouse scroll the mouse
|
// ScrollMouse scroll the mouse
|
||||||
func ScrollMouse(x int, y string) {
|
func ScrollMouse(x int, y string) {
|
||||||
cx := C.size_t(x)
|
cx := C.size_t(x)
|
||||||
z := C.CString(y)
|
cy := C.CString(y)
|
||||||
C.scroll_mouse(cx, z)
|
C.scroll_mouse(cx, cy)
|
||||||
defer C.free(unsafe.Pointer(z))
|
|
||||||
|
defer C.free(unsafe.Pointer(cy))
|
||||||
|
}
|
||||||
|
|
||||||
|
func Scroll(x, y int, args ...int) {
|
||||||
|
var msDelay = 10
|
||||||
|
if len(args) > 0 {
|
||||||
|
msDelay = args[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
cx := C.int(x)
|
||||||
|
cy := C.int(y)
|
||||||
|
cz:=C.int(msDelay)
|
||||||
|
|
||||||
|
C.scroll(cx, cy, cz)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user