Skip to content

Commit 9917f49

Browse files
committed
Changed mouseScroll to use X and Y as direction.
1 parent 58f6c52 commit 9917f49

File tree

3 files changed

+63
-82
lines changed

3 files changed

+63
-82
lines changed

src/mouse.c

+49-46
Original file line numberDiff line numberDiff line change
@@ -232,71 +232,74 @@ void doubleClick(MMMouseButton button)
232232
#endif
233233
}
234234

235-
/**
236-
* Function used to scroll the screen in the required direction.
237-
* This uses the magnitude to scroll the required amount in the direction.
238-
* TODO Requires further fine tuning based on the requirements.
239-
*/
240-
void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection)
235+
void scrollMouse(MMPoint scroll)
241236
{
242-
#if defined(IS_WINDOWS)
237+
#if defined(IS_WINDOWS)
243238
// Fix for #97 https://github.com/octalmage/robotjs/issues/97,
244239
// C89 needs variables declared on top of functions (mouseScrollInput)
245-
INPUT mouseScrollInput;
246-
#endif
247-
248-
/* Direction should only be considered based on the scrollDirection. This
249-
* Should not interfere. */
250-
int cleanScrollMagnitude = abs(scrollMagnitude);
251-
if (!(scrollDirection == DIRECTION_UP || scrollDirection == DIRECTION_DOWN))
252-
{
253-
return;
254-
}
240+
INPUT mouseScrollInputH;
241+
INPUT mouseScrollInputV;
242+
#endif
255243

256-
/* Set up the OS specific solution */
257-
#if defined(__APPLE__)
244+
/* Direction should only be considered based on the scrollDirection. This
245+
* Should not interfere. */
258246

259-
CGWheelCount wheel = 1;
260-
CGEventRef event;
247+
/* Set up the OS specific solution */
248+
#if defined(__APPLE__)
261249

262-
/* Make scroll magnitude negative if we're scrolling down. */
263-
cleanScrollMagnitude = cleanScrollMagnitude * scrollDirection;
250+
CGEventRef event;
264251

265-
event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitLine, wheel, cleanScrollMagnitude, 0);
252+
event = CGEventCreateScrollWheelEvent(NULL, kCGScrollEventUnitPixel, 2, scroll.y, scroll.x);
266253
CGEventPost(kCGHIDEventTap, event);
267254

268-
#elif defined(USE_X11)
255+
CFRelease(event);
256+
257+
#elif defined(USE_X11)
269258

270-
int x;
271-
int dir = 4; /* Button 4 is up, 5 is down. */
259+
int ydir = 4; /* Button 4 is up, 5 is down. */
260+
int xdir = 6;
272261
Display *display = XGetMainDisplay();
273262

274-
if (scrollDirection == DIRECTION_DOWN)
275-
{
276-
dir = 5;
263+
if (scroll.y < 0){
264+
ydir = 5;
277265
}
278-
279-
for (x = 0; x < cleanScrollMagnitude; x++)
280-
{
281-
XTestFakeButtonEvent(display, dir, 1, CurrentTime);
282-
XTestFakeButtonEvent(display, dir, 0, CurrentTime);
266+
if (scroll.x < 0){
267+
xdir = 7;
283268
}
284269

285-
XFlush(display);
270+
for (int x = 0; x < abs(scroll.x); x++) {
271+
XTestFakeButtonEvent(display, xdir, 1, CurrentTime);
272+
XTestFakeButtonEvent(display, xdir, 0, CurrentTime);
273+
}
286274

287-
#elif defined(IS_WINDOWS)
275+
for (int y = 0; y < abs(scroll.y); y++) {
276+
XTestFakeButtonEvent(display, ydir, 1, CurrentTime);
277+
XTestFakeButtonEvent(display, ydir, 0, CurrentTime);
278+
}
288279

289-
mouseScrollInput.type = INPUT_MOUSE;
290-
mouseScrollInput.mi.dx = 0;
291-
mouseScrollInput.mi.dy = 0;
292-
mouseScrollInput.mi.dwFlags = MOUSEEVENTF_WHEEL;
293-
mouseScrollInput.mi.time = 0;
294-
mouseScrollInput.mi.dwExtraInfo = 0;
295-
mouseScrollInput.mi.mouseData = WHEEL_DELTA * scrollDirection * cleanScrollMagnitude;
280+
XFlush(display);
296281

297-
SendInput(1, &mouseScrollInput, sizeof(mouseScrollInput));
282+
#elif defined(IS_WINDOWS)
298283

299-
#endif
284+
mouseScrollInputH.type = INPUT_MOUSE;
285+
mouseScrollInputH.mi.dx = 0;
286+
mouseScrollInputH.mi.dy = 0;
287+
mouseScrollInputH.mi.dwFlags = MOUSEEVENTF_WHEEL;
288+
mouseScrollInputH.mi.time = 0;
289+
mouseScrollInputH.mi.dwExtraInfo = 0;
290+
mouseScrollInputH.mi.mouseData = WHEEL_DELTA * scroll.x;
291+
292+
mouseScrollInputV.type = INPUT_MOUSE;
293+
mouseScrollInputV.mi.dx = 0;
294+
mouseScrollInputV.mi.dy = 0;
295+
mouseScrollInputV.mi.dwFlags = MOUSEEVENTF_HWHEEL;
296+
mouseScrollInputV.mi.time = 0;
297+
mouseScrollInputV.mi.dwExtraInfo = 0;
298+
mouseScrollInputV.mi.mouseData = WHEEL_DELTA * scroll.y;
299+
300+
SendInput(1, &mouseScrollInputH, sizeof(mouseScrollInputH));
301+
SendInput(1, &mouseScrollInputV, sizeof(mouseScrollInputV));
302+
#endif
300303
}
301304

302305
/*

src/mouse.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void doubleClick(MMMouseButton button);
9090

9191
/* Scrolls the mouse in the stated direction.
9292
* TODO: Add a smoothly scroll mouse next. */
93-
void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection);
93+
void scrollMouse(MMPoint scroll);
9494

9595
#endif /* MOUSE_H */
9696

src/robotjs.cc

+13-35
Original file line numberDiff line numberDiff line change
@@ -240,41 +240,19 @@ NAN_METHOD(setMouseDelay)
240240

241241
NAN_METHOD(scrollMouse)
242242
{
243-
Nan::HandleScope scope;
244-
245-
//Get the values of magnitude and direction from the arguments list.
246-
if(info.Length() == 2)
247-
{
248-
int scrollMagnitude = info[0]->Int32Value();
249-
char *s;
250-
251-
Nan::Utf8String sstr(info[1]);
252-
s = *sstr;
253-
254-
MMMouseWheelDirection scrollDirection;
255-
256-
if (strcmp(s, "up") == 0)
257-
{
258-
scrollDirection = DIRECTION_UP;
259-
}
260-
else if (strcmp(s, "down") == 0)
261-
{
262-
scrollDirection = DIRECTION_DOWN;
263-
}
264-
else
265-
{
266-
return Nan::ThrowError("Invalid scroll direction specified.");
267-
}
268-
269-
scrollMouse(scrollMagnitude, scrollDirection);
270-
microsleep(mouseDelay);
271-
272-
info.GetReturnValue().Set(Nan::New(1));
273-
}
274-
else
275-
{
276-
return Nan::ThrowError("Invalid number of arguments.");
277-
}
243+
if (info.Length() != 2)
244+
{
245+
return Nan::ThrowError("Invalid number of arguments.");
246+
}
247+
size_t x = info[0]->Int32Value();
248+
size_t y = info[1]->Int32Value();
249+
250+
MMPoint point;
251+
point = MMPointMake(x, y);
252+
scrollMouse(point);
253+
microsleep(mouseDelay);
254+
255+
info.GetReturnValue().Set(Nan::New(1));
278256
}
279257
/*
280258
_ __ _ _

0 commit comments

Comments
 (0)