Skip to content

Commit

Permalink
Changed mouseScroll to use X and Y as direction.
Browse files Browse the repository at this point in the history
  • Loading branch information
BHamrick1 committed May 18, 2016
1 parent 58f6c52 commit 9917f49
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 82 deletions.
95 changes: 49 additions & 46 deletions src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,71 +232,74 @@ 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(MMPoint scroll)
{
#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, scroll.y, scroll.x);
CGEventPost(kCGHIDEventTap, event);

#elif defined(USE_X11)
CFRelease(event);

#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 (scroll.y < 0){
ydir = 5;
}

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

XFlush(display);
for (int x = 0; x < abs(scroll.x); x++) {
XTestFakeButtonEvent(display, xdir, 1, CurrentTime);
XTestFakeButtonEvent(display, xdir, 0, CurrentTime);
}

#elif defined(IS_WINDOWS)
for (int y = 0; y < abs(scroll.y); y++) {
XTestFakeButtonEvent(display, ydir, 1, CurrentTime);
XTestFakeButtonEvent(display, ydir, 0, CurrentTime);
}

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;
XFlush(display);

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 * scroll.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 * scroll.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(MMPoint scroll);

#endif /* MOUSE_H */

Expand Down
48 changes: 13 additions & 35 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,41 +240,19 @@ 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)
{
return Nan::ThrowError("Invalid number of arguments.");
}
size_t x = info[0]->Int32Value();
size_t y = info[1]->Int32Value();

MMPoint point;
point = MMPointMake(x, y);
scrollMouse(point);
microsleep(mouseDelay);

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

0 comments on commit 9917f49

Please sign in to comment.