Move mouse cursor with node.js

匿名 (未验证) 提交于 2019-12-03 01:59:02

问题:

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

回答1:

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!



回答2:

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.



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