Skip to content

Commit

Permalink
MouseScroll feature addition
Browse files Browse the repository at this point in the history
Adds the feature of mouse scroll in windows.
  • Loading branch information
Deltatiger committed Jul 28, 2015
1 parent 03a437a commit 4052946
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,38 @@ void clickMouse(MMMouseButton button)
toggleMouse(false, button);
}

/**
* 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)
{
/* 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__)
/* TODO Add Code for this platform */
#elif defined(USE_X11)
/* TODO Add Code for this platform */
#elif defined(IS_WINDOWS)
INPUT mouseScrollInput;
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));
#endif
}
/*
* A crude, fast hypot() approximation to get around the fact that hypot() is
* not a standard ANSI C function.
Expand Down
10 changes: 10 additions & 0 deletions src/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ extern "C"
RIGHT_BUTTON = 3
};
typedef unsigned int MMMouseButton;

enum __MMMouseWheelDirection {
DIRECTION_DOWN = -1,
DIRECTION_UP = 1
};
typedef unsigned int MMMouseWheelDirection;

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

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

#endif /* MOUSE_H */

#ifdef __cplusplus
Expand Down
22 changes: 22 additions & 0 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,25 @@ NAN_METHOD(mouseToggle)
NanReturnValue(NanNew("1"));
}

NAN_METHOD(scrollMouse)
{
NanScope();

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

scrollMouse(scrollMagnitude, scrollDirection);

NanReturnValue(NanNew("1"));
}
else
{
return NanThrowError("Invalid number of arguments.");
}
}
/*
_ __ _ _
| |/ /___ _ _| |__ ___ __ _ _ __ __| |
Expand Down Expand Up @@ -489,6 +508,9 @@ void init(Handle<Object> target)

target->Set(NanNew<String>("mouseToggle"),
NanNew<FunctionTemplate>(mouseToggle)->GetFunction());

target->Set(NanNew<String>("scrollMouse"),
NanNew<FunctionTemplate>(scrollMouse)->GetFunction());

target->Set(NanNew<String>("keyTap"),
NanNew<FunctionTemplate>(keyTap)->GetFunction());
Expand Down

0 comments on commit 4052946

Please sign in to comment.