Skip to content

Commit

Permalink
Merge pull request octalmage#194 from BHamrick1/master
Browse files Browse the repository at this point in the history
Changed mouseScroll to use X and Y as direction.
  • Loading branch information
octalmage authored Feb 23, 2017
2 parents f9b2284 + 6f8d2b6 commit 01f3464
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 97 deletions.
132 changes: 68 additions & 64 deletions src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void moveMouse(MMPoint point)
mouseInput.mi.dwExtraInfo = 0;
mouseInput.mi.mouseData = 0;
SendInput(1, &mouseInput, sizeof(mouseInput));

#endif
}

Expand Down Expand Up @@ -202,101 +202,105 @@ void clickMouse(MMMouseButton button)
*/
void doubleClick(MMMouseButton button)
{

#if defined(IS_MACOSX)

/* Double click for Mac. */
const CGPoint currentPos = CGPointFromMMPoint(getMousePos());
const CGEventType mouseTypeDown = MMMouseToCGEventType(true, button);
const CGEventType mouseTypeUP = MMMouseToCGEventType(false, button);

CGEventRef event = CGEventCreateMouseEvent(NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft);
/* Set event to double click. */
CGEventRef event = CGEventCreateMouseEvent(NULL, mouseTypeDown, currentPos, kCGMouseButtonLeft);

/* Set event to double click. */
CGEventSetIntegerValueField(event, kCGMouseEventClickState, 2);

CGEventPost(kCGHIDEventTap, event);

CGEventSetType(event, mouseTypeUP);
CGEventPost(kCGHIDEventTap, event);

CFRelease(event);
CGEventPost(kCGHIDEventTap, event);

CGEventSetType(event, mouseTypeUP);
CGEventPost(kCGHIDEventTap, event);

CFRelease(event);

#else

/* Double click for everything else. */
clickMouse(button);
microsleep(200);
clickMouse(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)
// 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;
}

/* Set up the OS specific solution */
#if defined(__APPLE__)
#if defined(IS_WINDOWS)
// Fix for #97 https://github.com/octalmage/robotjs/issues/97,
// C89 needs variables declared on top of functions (mouseScrollInput)
INPUT mouseScrollInputH;
INPUT mouseScrollInputV;
#endif

CGWheelCount wheel = 1;
CGEventRef event;
/* Direction should only be considered based on the scrollDirection. This
* Should not interfere. */

/* Make scroll magnitude negative if we're scrolling down. */
cleanScrollMagnitude = cleanScrollMagnitude * scrollDirection;
/* Set up the OS specific solution */
#if defined(__APPLE__)

event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0);
CGEventPost(kCGHIDEventTap, event);
CGEventRef event;

#elif defined(USE_X11)
event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, y, x);
CGEventPost(kCGHIDEventTap, event);

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

if (scrollDirection == DIRECTION_DOWN)
{
dir = 5;
}
#elif defined(USE_X11)

for (x = 0; x < cleanScrollMagnitude; x++)
{
XTestFakeButtonEvent(display, dir, 1, CurrentTime);
XTestFakeButtonEvent(display, dir, 0, CurrentTime);
}
int ydir = 4; /* Button 4 is up, 5 is down. */
int xdir = 6;
Display *display = XGetMainDisplay();

XFlush(display);
if (y < 0){
ydir = 5;
}
if (x < 0){
xdir = 7;
}

#elif defined(IS_WINDOWS)
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++) {
YTestFakeButtonEvent(display, ydir, 1, CurrentTime);
YTestFakeButtonEvent(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 * 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
41 changes: 9 additions & 32 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -243,41 +243,18 @@ NAN_METHOD(setMouseDelay)

NAN_METHOD(scrollMouse)
{
Nan::HandleScope scope;

//Get the values of magnitude and direction from the arguments list.
if(info.Length() == 2)
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.");
}
return Nan::ThrowError("Invalid number of arguments.");
}

int x = info[0]->Int32Value();
int y = info[1]->Int32Value();

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

info.GetReturnValue().Set(Nan::New(1));
}
else
{
return Nan::ThrowError("Invalid number of arguments.");
}
info.GetReturnValue().Set(Nan::New(1));
}
/*
_ __ _ _
Expand Down

0 comments on commit 01f3464

Please sign in to comment.