javascript 使用 node.js 移动鼠标光标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22695122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:35:13  来源:igfitidea点击:

Move mouse cursor with node.js

javascriptnode.jsmouse

提问by Nikolay Ayrapetov

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

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

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

我找到了这个库https://www.npmjs.org/package/win_mouse但似乎它不起作用

回答by Jason Stallings

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

我一直在为此开发一个模块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!

它仍在进行中,但它会做你想做的!

回答by Miichi

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

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

One solution would be to use the ffipackage, which allows you to dynamically load and call native libraries. To move the mouse on windows, you'd need to call the SetCursorPosfunction from the user32.dlllike this:

一种解决方案是使用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);

Another solution would be to write a native node add-onthat wraps around the SetCursorPosfunction, but it is more complex.

另一种解决方案是编写一个围绕该函数的本机节点附加组件SetCursorPos,但它更复杂。