Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I control mouse in Electron?

I want just move the mouse to a specific location relative to my App Window.

I don't find a way to achieve this behavior, is that even possible?

like image 639
Kiochan Avatar asked Jan 05 '23 18:01

Kiochan


2 Answers

Check out RobotJS, it should do just fine in your case. From its docs:

// 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);
}
like image 66
Jens Habegger Avatar answered Jan 15 '23 12:01

Jens Habegger


See WebContents.prototype.sendInputEvent

Example:

remote.getCurrentWebContents().sendInputEvent({type: 'mouseMove', x: 10, y: 10})
like image 30
Casper Beyer Avatar answered Jan 15 '23 13:01

Casper Beyer