我可以在 Electron 中控制鼠标吗?

Kio*_*han 4 javascript electron

我只想将鼠标移动到相对于我的应用程序窗口的特定位置。

我找不到实现这种行为的方法,这可能吗?

Cas*_*yer 6

请参阅WebContents.prototype.sendInputEvent

例子:

remote.getCurrentWebContents().sendInputEvent({type: 'mouseMove', x: 10, y: 10})
Run Code Online (Sandbox Code Playgroud)


Jen*_*ger 5

看看RobotJS,它应该适合你的情况。从它的文档:

// Move the mouse across the screen as a sine wave.
var robot = require("robotjs");

// Speed up the mouse.
robot.setMouseDelay(2);

var twoPI = Math.PI * 2.0;
var screenSize = robot.getScreenSize();
var height = (screenSize.height / 2) - 10;
var width = screenSize.width;

for (var x = 0; x < width; x++)
{
    y = height * Math.sin((twoPI * x) / width) + height;
    robot.moveMouse(x, y);
}
Run Code Online (Sandbox Code Playgroud)