Move mouse cursor with node.js [closed]

被刻印的时光 ゝ 提交于 2019-11-28 23:57:56

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!

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!