Catch mouse move event from main process (Electron)
Asked Answered
B

2

10

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

Booster answered 29/3, 2018 at 16:9 Comment(4)
What's the question?Valentijn
How to catch mouse move event from the main process in ElectronBooster
Can you window.addEventListender('mousemove', callback)?Peba
No because i want to know the position of the mouse, when the mouse is outside the windowBooster
V
7

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

Valentijn answered 29/3, 2018 at 16:27 Comment(2)
Yes but i want to trigger a mouse move event (when CursorScreenPoint change)Booster
There's no such event for this in Electron, you'll have to store the position every time and compare it to the previous value and then trigger your own function if it changed.Valentijn
C
1

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://learn.microsoft.com/en-us/windows/desktop/inputdev/wm-mousemove).

browserWindow.hookWindowMessage(0x200, () => {
    // Your function here
})
Coray answered 9/1, 2019 at 9:23 Comment(2)
Using Windows 7, this only works on dragging and in the right bottom corner it shows 1535x863 instead of 1920x1080Brewton
This only sporadically triggered.Indeclinable

© 2022 - 2024 — McMap. All rights reserved.