I want to catch mouse move event from the main process (Not render) with Electron.
Now, I'm doing a setInterval loop to catch the mouse position, but this is not very clean (and from the render process)...
It's look like this:
setInterval(function () {
let mousePos = SCREEN.getCursorScreenPoint()
}, 0)
So... How can i catch the event from the main process ?
I want to know the position of the mouse, when the mouse is outside the window
You can get the mouse position from the main process exactly the same way as you would do in the renderer process, the only thing is you need to wait until the ready event of the app module is emitted.
So, for example:
// wait until ready event is fired
electron.app.on('ready', function() {
// get the mouse position
let mousePos = electron.screen.getCursorScreenPoint();
console.log(mousePos);
});
https://electronjs.org/docs/api/screen#screengetcursorscreenpoint
You can add a hook to the BrowserWindow to listen for the Windows WM_MOUSEMOVE message (in this case the message code is 0x200 https://docs.microsoft.com/en-us/windows/desktop/inputdev/wm-mousemove).
browserWindow.hookWindowMessage(0x200, () => {
// Your function here
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With