Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch mouse move event from main process (Electron)

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

like image 424
owencalvin Avatar asked Mar 29 '18 16:03

owencalvin


2 Answers

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

like image 88
Thomas Orlita Avatar answered Sep 28 '22 02:09

Thomas Orlita


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
})
like image 36
Daniel Robinson Avatar answered Sep 28 '22 00:09

Daniel Robinson