Skip to content

Commit 4052946

Browse files
committed
MouseScroll feature addition
Adds the feature of mouse scroll in windows.
1 parent 03a437a commit 4052946

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

src/mouse.c

+32
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,38 @@ void clickMouse(MMMouseButton button)
124124
toggleMouse(false, button);
125125
}
126126

127+
/**
128+
* Function used to scroll the screen in the required direction.
129+
* This uses the magnitude to scroll the required amount in the direction.
130+
* TODO Requires further fine tuning based on the requirements.
131+
*/
132+
void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection)
133+
{
134+
/* Direction should only be considered based on the scrollDirection. This
135+
* Should not interfere. */
136+
int cleanScrollMagnitude = abs(scrollMagnitude);
137+
if (!(scrollDirection == DIRECTION_UP || scrollDirection == DIRECTION_DOWN))
138+
{
139+
return;
140+
}
141+
142+
/* Set up the OS specific solution */
143+
#if defined(__APPLE__)
144+
/* TODO Add Code for this platform */
145+
#elif defined(USE_X11)
146+
/* TODO Add Code for this platform */
147+
#elif defined(IS_WINDOWS)
148+
INPUT mouseScrollInput;
149+
mouseScrollInput.type = INPUT_MOUSE;
150+
mouseScrollInput.mi.dx = 0;
151+
mouseScrollInput.mi.dy = 0;
152+
mouseScrollInput.mi.dwFlags = MOUSEEVENTF_WHEEL;
153+
mouseScrollInput.mi.time = 0;
154+
mouseScrollInput.mi.dwExtraInfo = 0;
155+
mouseScrollInput.mi.mouseData = WHEEL_DELTA * scrollDirection * cleanScrollMagnitude;
156+
SendInput(1, &mouseScrollInput, sizeof(mouseScrollInput));
157+
#endif
158+
}
127159
/*
128160
* A crude, fast hypot() approximation to get around the fact that hypot() is
129161
* not a standard ANSI C function.

src/mouse.h

+10
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ extern "C"
4242
RIGHT_BUTTON = 3
4343
};
4444
typedef unsigned int MMMouseButton;
45+
46+
enum __MMMouseWheelDirection {
47+
DIRECTION_DOWN = -1,
48+
DIRECTION_UP = 1
49+
};
50+
typedef unsigned int MMMouseWheelDirection;
4551

4652
#else
4753
#error "No mouse button constants set for platform"
@@ -73,6 +79,10 @@ void toggleMouse(bool down, MMMouseButton button);
7379
/* Clicks the mouse with the given button in the current position. */
7480
void clickMouse(MMMouseButton button);
7581

82+
/* Scrolls the mouse in the stated direction.
83+
* TODO: Add a smoothly scroll mouse next. */
84+
void scrollMouse(int scrollMagnitude, MMMouseWheelDirection scrollDirection);
85+
7686
#endif /* MOUSE_H */
7787

7888
#ifdef __cplusplus

src/robotjs.cc

+22
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,25 @@ NAN_METHOD(mouseToggle)
166166
NanReturnValue(NanNew("1"));
167167
}
168168

169+
NAN_METHOD(scrollMouse)
170+
{
171+
NanScope();
172+
173+
//Get the values of magnitude and direction from the arguments list
174+
if(args.Length() == 2)
175+
{
176+
int scrollMagnitude = args[0]->Int32Value();
177+
int scrollDirection = args[1]->Int32Value();
178+
179+
scrollMouse(scrollMagnitude, scrollDirection);
180+
181+
NanReturnValue(NanNew("1"));
182+
}
183+
else
184+
{
185+
return NanThrowError("Invalid number of arguments.");
186+
}
187+
}
169188
/*
170189
_ __ _ _
171190
| |/ /___ _ _| |__ ___ __ _ _ __ __| |
@@ -489,6 +508,9 @@ void init(Handle<Object> target)
489508

490509
target->Set(NanNew<String>("mouseToggle"),
491510
NanNew<FunctionTemplate>(mouseToggle)->GetFunction());
511+
512+
target->Set(NanNew<String>("scrollMouse"),
513+
NanNew<FunctionTemplate>(scrollMouse)->GetFunction());
492514

493515
target->Set(NanNew<String>("keyTap"),
494516
NanNew<FunctionTemplate>(keyTap)->GetFunction());

0 commit comments

Comments
 (0)