How to Capture Mouse Move Events from Electron Main Process (Even When Mouse is Outside the Window)
Electron has revolutionized desktop application development by allowing developers to build cross-platform apps using web technologies (HTML, CSS, and JavaScript). A common requirement in desktop apps is tracking user input, such as mouse movements. While capturing mouse events within an Electron window is straightforward using the renderer process (via standard DOM events like mousemove), capturing events outside the app window or globally requires a different approach.
In this blog, we’ll explore how to capture mouse move events directly from the Electron main process, even when the mouse is outside the application window. We’ll cover the challenges involved, introduce tools to overcome them, and provide a step-by-step implementation guide. This is especially useful for use cases like screen recording tools, accessibility apps, or custom input managers that need to monitor user interactions system-wide.
Main Process: Manages the app lifecycle, window creation, and system-level interactions. It has access to Node.js APIs and Electron’s core modules (e.g., BrowserWindow, app).
Renderer Process: Runs in a Chromium instance and handles the app’s UI (DOM, web APIs). It can communicate with the main process via IPC (Inter-Process Communication).
By default, mouse events like mousemove are only accessible in the renderer process when the mouse is within the app window. To capture events outside the window, we need to tap into system-level input events, which requires native code integration—something the main process can handle with the right tools.
Electron’s built-in APIs do not natively support global mouse event tracking. Here’s why:
Renderer Process Limitations: DOM events like mousemove are scoped to the app window. Once the mouse leaves the window, these events stop firing.
Main Process Restrictions: The main process lacks direct access to low-level input events. It can query the current mouse position (e.g., via screen.getCursorScreenPoint()), but this only gives a snapshot, not continuous event updates.
To solve this, we need a way to listen to global input events at the operating system level. This requires a native Node.js module that bridges Electron to the OS’s input subsystem.
A cross-platform library for listening to global keyboard and mouse events. It supports Windows, macOS, and Linux, and provides real-time event callbacks for mouse movements, clicks, scrolls, etc.—even when the app is minimized or in the background.
While robotjs can retrieve the current mouse position (via robotjs.getMousePos()), it does not natively support event listening. It’s better for simulating input rather than capturing it.
For our use case, iohook is the ideal choice, as it focuses on global event listening.
If you don’t already have an Electron project, create one with these commands:
# Initialize a new projectmkdir electron-mouse-tracker && cd electron-mouse-trackernpm init -y# Install Electron as a dev dependencynpm install electron --save-dev
Update package.json to include Electron start scripts:
iohook is a native module, so it must be rebuilt to work with Electron’s Node.js version (Electron uses a custom Node.js runtime). We’ll use electron-rebuild to handle this.
Note: If you encounter errors (e.g., "Module version mismatch"), ensure electron-rebuild targets your installed Electron version. You can specify the version explicitly:
To display the mouse coordinates in the app window, create an index.html file and a preload.js script for IPC communication between the main and renderer processes.
// Listen for mouse move events from the main processwindow.electron.onMouseMove(({ x, y }) => { const coordsElement = document.getElementById('mouse-coords'); coordsElement.textContent = `X: ${x}, Y: ${y}`;});
Global input tracking is privacy-sensitive. Always inform users why your app needs this capability (e.g., "This app tracks mouse movements to enable screen recording features").
Capturing global mouse move events from the Electron main process is achievable with native modules like iohook. By following this guide, you can track mouse movements outside your app window for use cases like accessibility tools, screen recorders, or custom input managers.
Key takeaways:
Use iohook for cross-platform global input event listening.
Rebuild native modules with electron-rebuild to ensure compatibility with Electron.