15

Is there is any way or module to move cursor and simulate mouse clicks in windows7/8 with node.js?

I found this library https://www.npmjs.org/package/win_mouse but seems like it doesn't work

Nikolay Ayrapetov
  • 184
  • 1
  • 1
  • 6
  • If you have Java on your system, you can use Java + node as my answer here describes: http://stackoverflow.com/a/21080830/586621 – Jay Mar 27 '14 at 17:42
  • What do you mean by "seems like it doesn't work"? Are there errors? –  Mar 27 '14 at 17:42
  • @RalphWiggum Yes, when I'm trying to run my js file through prompt, I'm getting this error: module.js:356 Module._extensions[extension](this, filename); Error: %1 is not a valid Win32 application. – Nikolay Ayrapetov Mar 27 '14 at 17:57
  • @TrevorSenior I tried your solution, but I'm getting this error: Error: Error creating class java.lang.InternalError: Can't start the AWT because Java was started on the first thread. Make sure StartOnFirstThread is not specified in your application's Info.plist or on the command line – Nikolay Ayrapetov Mar 27 '14 at 18:09
  • let's take a moment to acknowledge that almost everyone came here because they want to create a simple mouse juggler, to continue appearing online on Microsoft Teams or other while they are away slacking during home office. – Moha the almighty camel Nov 25 '21 at 11:45

2 Answers2

44

I've been working on a module for this, RobotJS.

Example code:

var robot = require("robotjs");

//Get the mouse position, retuns an object with x and y. 
var mouse=robot.getMousePos();
console.log("Mouse is at x:" + mouse.x + " y:" + mouse.y);

//Move the mouse down by 100 pixels.
robot.moveMouse(mouse.x,mouse.y+100);

//Left click!
robot.mouseClick();

It's still a work in progress but it will do what you want!

Jason Stallings
  • 1,443
  • 20
  • 17
8

I've previously tried the win_mouse package, but it didn't work for me either, think it requires an older version of node.js.

One solution would be to use the ffi package, which allows you to dynamically load and call native libraries. To move the mouse on windows, you'd need to call the SetCursorPos function from the user32.dll like this:

var ffi = require("ffi");

var user32 = ffi.Library('user32', {
    'SetCursorPos': [ 'long', ['long', 'long'] ]
    // put other functions that you want to use from the library here, e.g., "GetCursorPos"
});

var result = user32.SetCursorPos(10, 10);
console.log(result);

Another solution would be to write a native node add-on that wraps around the SetCursorPos function, but it is more complex.

Miichi
  • 1,739
  • 1
  • 14
  • 16
  • 2
    +1 but how would you define GetCursorPos, given that the method requires a pointer to a `Point`? https://msdn.microsoft.com/en-us/library/windows/desktop/ms648390%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 – Mohamed Khamis Apr 29 '15 at 09:54
  • 1
    Follow this guide: https://github.com/node-ffi/node-ffi/wiki/Node-FFI-Tutorial Specifically, the section on `Structs` at the bottom. – Daniel T. Jul 01 '15 at 09:20