Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
This properly fixes the multi-monitor support for windows(tested only on 10, assuming 7+ will work)<br><br>
Also supports user defined speed of smooth mouse movement (original randomness updated to alter a bit based on distance)<br><br>
=================Original ReadMe To Follow==================
<p align="center"><img src="https://cldup.com/1ATDf2JMtv.png"></p>

<p align="center"><a href="https://travis-ci.org/octalmage/robotjs"><img src="https://api.travis-ci.org/octalmage/robotjs.svg?branch=master"></a> <a href="https://ci.appveyor.com/project/octalmage/robotjs"><img src="https://ci.appveyor.com/api/projects/status/qh2eqb37j7ap6x36?svg=true"></a> <a href="https://www.npmjs.com/package/robotjs"><img src="https://img.shields.io/npm/v/robotjs.svg"></a> <a href="https://gitter.im/octalmage/robotjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge"><img src="https://img.shields.io/badge/gitter-join%20chat-blue.svg"></a> <a href="http://waffle.io/octalmage/robotjs"><img src="https://img.shields.io/waffle/label/octalmage/robotjs/ready.svg?maxAge=3600"></a></p>
Expand Down Expand Up @@ -153,8 +156,7 @@ Soon! This is a bit more complicated than the rest of the features, so I saved i
We've been implementing keys as we need them. Feel free to create an issue or submit a pull request!

#### How about multi-monitor support?

The library doesn't have explicit multi-monitor support, so anything that works is kind of on accident. Subscribe to [#88](https://github.com/octalmage/robotjs/issues/88) for updates.
Multiple monitor support working. Tested in windows only, need additional feedback for mac/nix.

For any other questions please [submit an issue](https://github.com/octalmage/robotjs/issues/new).

Expand Down
24 changes: 13 additions & 11 deletions src/mouse.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ void moveMouse(MMSignedPoint point)
//Mouse motion is now done using SendInput with MOUSEINPUT. We use Absolute mouse positioning
#define MOUSE_COORD_TO_ABS(coord, width_or_height) ((65536 * (coord) / width_or_height) + ((coord) < 0 ? -1 : 1))

size_t x = MOUSE_COORD_TO_ABS(point.x-vscreenMinX, vscreenWidth);
size_t y = MOUSE_COORD_TO_ABS(point.y-vscreenMinY, vscreenHeight);
int32_t x = MOUSE_COORD_TO_ABS(point.x - vscreenMinX, vscreenWidth);
int32_t y = MOUSE_COORD_TO_ABS(point.y - vscreenMinY, vscreenHeight);

INPUT mouseInput = {0};
mouseInput.type = INPUT_MOUSE;
Expand Down Expand Up @@ -162,7 +162,7 @@ void dragMouse(MMSignedPoint point, const MMMouseButton button)
#endif
}

MMPoint getMousePos()
MMSignedPoint getMousePos()
{
#if defined(IS_MACOSX)
CGEventRef event = CGEventCreate(NULL);
Expand All @@ -185,7 +185,7 @@ MMPoint getMousePos()
POINT point;
GetCursorPos(&point);

return MMPointFromPOINT(point);
return MMSignedPointFromPOINT(point);
#endif
}

Expand Down Expand Up @@ -371,15 +371,17 @@ static double crude_hypot(double x, double y)
return ((M_SQRT2 - 1.0) * small) + big;
}

bool smoothlyMoveMouse(MMPoint endPoint,double speed)
bool smoothlyMoveMouse(MMPoint endPoint, double speed)
{
MMPoint pos = getMousePos();
MMSize screenSize = getMainDisplaySize();
MMSignedPoint pos = getMousePos();
MMSignedSize screenSize = getMainDisplaySize();
double velo_x = 0.0, velo_y = 0.0;
double distance;

if (vscreenWidth < 0 || vscreenHeight < 0)
updateScreenMetrics();
double bdist = (distance = crude_hypot((double)pos.x - endPoint.x,(double)pos.y - endPoint.y));
while ((distance = crude_hypot((double)pos.x - endPoint.x,
(double)pos.y - endPoint.y)) > 1.0) {
(double)pos.y - endPoint.y)) > 1.0) {
double gravity = DEADBEEF_UNIFORM(5.0, 500.0);
double veloDistance;
velo_x += (gravity * ((double)endPoint.x - pos.x)) / distance;
Expand All @@ -399,10 +401,10 @@ bool smoothlyMoveMouse(MMPoint endPoint,double speed)
return false;
}

moveMouse(MMSignedPointMake((int32_t)pos.x, (int32_t)pos.y));
moveMouse(MMSignedPointMake(pos.x, pos.y));

/* Wait 1 - (speed) milliseconds. */
microsleep(DEADBEEF_UNIFORM(0.7, speed));
microsleep(DEADBEEF_UNIFORM((min(0.7,speed) + (1-(distance / ((bdist + 0.0001) * 2)))), (max(0.7, speed) - (distance / ((bdist + 0.0001) * 1.5)))));
}

return true;
Expand Down
2 changes: 1 addition & 1 deletion src/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void dragMouse(MMSignedPoint point, const MMMouseButton button);
bool smoothlyMoveMouse(MMPoint point,double speed);

/* Returns the coordinates of the mouse on the current screen. */
MMPoint getMousePos(void);
MMSignedPoint getMousePos(void);

/* Holds down or releases the mouse with the given button in the current
* position. */
Expand Down
6 changes: 3 additions & 3 deletions src/robotjs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ NAN_METHOD(moveMouseSmooth)

NAN_METHOD(getMousePos)
{
MMPoint pos = getMousePos();
MMSignedPoint pos = getMousePos();

//Return object with .x and .y.
Local<Object> obj = Nan::New<Object>();
Expand Down Expand Up @@ -713,7 +713,7 @@ NAN_METHOD(getPixelColor)
NAN_METHOD(getScreenSize)
{
//Get display size.
MMSize displaySize = getMainDisplaySize();
MMSignedSize displaySize = getMainDisplaySize();

//Create our return object.
Local<Object> obj = Nan::New<Object>();
Expand Down Expand Up @@ -770,7 +770,7 @@ NAN_METHOD(captureScreen)
y = 0;

//Get screen size.
MMSize displaySize = getMainDisplaySize();
MMSignedSize displaySize = getMainDisplaySize();
w = displaySize.width;
h = displaySize.height;
}
Expand Down
8 changes: 4 additions & 4 deletions src/screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "xdisplay.h"
#endif

MMSize getMainDisplaySize(void)
MMSignedSize getMainDisplaySize(void)
{
#if defined(IS_MACOSX)
CGDirectDisplayID displayID = CGMainDisplayID();
Expand All @@ -21,13 +21,13 @@ MMSize getMainDisplaySize(void)
return MMSizeMake((size_t)DisplayWidth(display, screen),
(size_t)DisplayHeight(display, screen));
#elif defined(IS_WINDOWS)
return MMSizeMake((size_t)GetSystemMetrics(SM_CXSCREEN),
(size_t)GetSystemMetrics(SM_CYSCREEN));
return MMSignedSizeMake((size_t)GetSystemMetrics(SM_CXVIRTUALSCREEN),
(size_t)GetSystemMetrics(SM_CYVIRTUALSCREEN));
#endif
}

bool pointVisibleOnMainDisplay(MMPoint point)
{
MMSize displaySize = getMainDisplaySize();
MMSignedSize displaySize = getMainDisplaySize();
return point.x < displaySize.width && point.y < displaySize.height;
}
2 changes: 1 addition & 1 deletion src/screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern "C"
#endif

/* Returns the size of the main display. */
MMSize getMainDisplaySize(void);
MMSignedSize getMainDisplaySize(void);

/* Convenience function that returns whether the given point is in the bounds
* of the main screen. */
Expand Down
15 changes: 15 additions & 0 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ struct _MMSize {

typedef struct _MMSize MMSize;

struct _MMSignedSize {
int32_t width;
int32_t height;
};

typedef struct _MMSignedSize MMSignedSize;

struct _MMRect {
MMPoint origin;
MMSize size;
Expand Down Expand Up @@ -61,6 +68,13 @@ H_INLINE MMSize MMSizeMake(size_t width, size_t height)
size.height = height;
return size;
}
H_INLINE MMSignedSize MMSignedSizeMake(int32_t width, int32_t height)
{
MMSignedSize size;
size.width = width;
size.height = height;
return size;
}

H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height)
{
Expand All @@ -83,6 +97,7 @@ H_INLINE MMRect MMRectMake(size_t x, size_t y, size_t width, size_t height)
#elif defined(IS_WINDOWS)

#define MMPointFromPOINT(p) MMPointMake((size_t)p.x, (size_t)p.y)
#define MMSignedPointFromPOINT(p) MMSignedPointMake((int32_t)p.x, (int32_t)p.y)

#endif

Expand Down