用node.js移动鼠标光标

时间:2014-03-27 17:36:41

标签: javascript node.js mouse

是否有任何方法或模块可以移动光标并使用node.js模拟windows7 / 8中的鼠标点击?

我找到了这个库https://www.npmjs.org/package/win_mouse,但似乎无效

2 个答案:

答案 0 :(得分:21)

我一直在研究一个模块,RobotJS

示例代码:

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();

这仍然是一项正在进行的工作,但它会做你想要的!

答案 1 :(得分:6)

我之前尝试过win_mouse包,但它对我来说也不起作用,认为它需要较旧版本的node.js。

一种解决方案是使用ffi包,它允许您动态加载和调用本机库。要在Windows上移动鼠标,您需要调用SetCursorPos中的user32.dll函数,如下所示:

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);

另一个解决方案是编写一个包裹SetCursorPos函数的native node add-on,但它更复杂。

相关问题