Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed mouseScroll to use X and Y as direction. #194

Merged
merged 11 commits into from
Feb 23, 2017
98 changes: 51 additions & 47 deletions src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,71 +232,75 @@ void doubleClick(MMMouseButton button)
#endif
}

/**
* Function used to scroll the screen in the required direction.
* This uses the magnitude to scroll the required amount in the direction.
* TODO Requires further fine tuning based on the requirements.
*/
void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection)
void scrollMouse(int x, int y)
{
#if defined(IS_WINDOWS)
#if defined(IS_WINDOWS)
// Fix for #97 https://github.com/octalmage/robotjs/issues/97,
// C89 needs variables declared on top of functions (mouseScrollInput)
INPUT mouseScrollInput;
#endif

/* Direction should only be considered based on the scrollDirection. This
* Should not interfere. */
int cleanScrollMagnitude = abs(scrollMagnitude);
if (!(scrollDirection == DIRECTION_UP || scrollDirection == DIRECTION_DOWN))
{
return;
}
INPUT mouseScrollInputH;
INPUT mouseScrollInputV;
#endif

/* Set up the OS specific solution */
#if defined(__APPLE__)
/* Direction should only be considered based on the scrollDirection. This
* Should not interfere. */

CGWheelCount wheel = 1;
CGEventRef event;
/* Set up the OS specific solution */
#if defined(__APPLE__)

/* Make scroll magnitude negative if we're scrolling down. */
cleanScrollMagnitude = cleanScrollMagnitude * scrollDirection;
CGEventRef event;

event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0);
event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x);
CGEventPost(kCGHIDEventTap, event);

#elif defined(USE_X11)
CFRelease(event);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces -> tabs.


#elif defined(USE_X11)

int x;
int dir = 4; /* Button 4 is up, 5 is down. */
int ydir = 4; /* Button 4 is up, 5 is down. */
int xdir = 6;
Display *display = XGetMainDisplay();

if (scrollDirection == DIRECTION_DOWN)
{
dir = 5;
if (y < 0){
ydir = 5;
}

for (x = 0; x < cleanScrollMagnitude; x++)
{
XTestFakeButtonEvent(display, dir, 1, CurrentTime);
XTestFakeButtonEvent(display, dir, 0, CurrentTime);
if (x < 0){
xdir = 7;
}

int xi;
int yi;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind fixing the whitespace here?

for (xi = 0; xi < abs(x); xi++) {
XTestFakeButtonEvent(display, xdir, 1, CurrentTime);
XTestFakeButtonEvent(display, xdir, 0, CurrentTime);
}
for (yi = 0; yi < abs(y); yi++) {
YTestFakeButtonEvent(display, ydir, 1, CurrentTime);
YTestFakeButtonEvent(display, ydir, 0, CurrentTime);
}

XFlush(display);

#elif defined(IS_WINDOWS)

mouseScrollInput.type = INPUT_MOUSE;
mouseScrollInput.mi.dx = 0;
mouseScrollInput.mi.dy = 0;
mouseScrollInput.mi.dwFlags = MOUSEEVENTF_WHEEL;
mouseScrollInput.mi.time = 0;
mouseScrollInput.mi.dwExtraInfo = 0;
mouseScrollInput.mi.mouseData = WHEEL_DELTA * scrollDirection * cleanScrollMagnitude;

SendInput(1, &mouseScrollInput, sizeof(mouseScrollInput));
#elif defined(IS_WINDOWS)

#endif
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
}

/*
Expand Down
2 changes: 1 addition & 1 deletion src/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void doubleClick(MMMouseButton button);

/* Scrolls the mouse in the stated direction.
* TODO: Add a smoothly scroll mouse next. */
void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection);
void scrollMouse(int x, int y);

#endif /* MOUSE_H */

Expand Down
46 changes: 11 additions & 35 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,41 +240,17 @@ NAN_METHOD(setMouseDelay)

NAN_METHOD(scrollMouse)
{
Nan::HandleScope scope;

//Get the values of magnitude and direction from the arguments list.
if(info.Length() == 2)
{
int scrollMagnitude = info[0]->Int32Value();
char *s;

Nan::Utf8String sstr(info[1]);
s = *sstr;

MMMouseWheelDirection scrollDirection;

if (strcmp(s, "up") == 0)
{
scrollDirection = DIRECTION_UP;
}
else if (strcmp(s, "down") == 0)
{
scrollDirection = DIRECTION_DOWN;
}
else
{
return Nan::ThrowError("Invalid scroll direction specified.");
}

scrollMouse(scrollMagnitude, scrollDirection);
microsleep(mouseDelay);

info.GetReturnValue().Set(Nan::New(1));
}
else
{
return Nan::ThrowError("Invalid number of arguments.");
}
if (info.Length() != 2)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces here too!

{
return Nan::ThrowError("Invalid number of arguments.");
}
int x = info[0]->Int32Value();
int y = info[1]->Int32Value();

scrollMouse(x, y);
microsleep(mouseDelay);

info.GetReturnValue().Set(Nan::New(1));
}
/*
_ __ _ _
Expand Down