A Node.js binding for the Rust enigo library, providing cross-platform keyboard and mouse automation.
Installation
npm install node-enigo
Usage
const enigo = require('node-enigo');// Type text (Promise-based, non-blocking)await enigo.typeText('Hello, world!');// Move mouse to absolute position (100, 100)await enigo.moveMouse(100, 100);
Moves the mouse cursor to the specified coordinates. By default, it uses absolute positioning, but relative movement can be enabled with the third parameter.
Parameters:
x (number): The x-coordinate to move to (or the x-distance to move by, if using relative movement)
y (number): The y-coordinate to move to (or the y-distance to move by, if using relative movement)
isRelative (boolean, optional): If true, the movement will be relative to the current cursor position. Default is false (absolute positioning).
Returns:
A Promise that resolves to a string with the result of the operation
Example with absolute movement:
const enigo = require('node-enigo');async function example() { try { // Move mouse to absolute position (100, 100) const result = await enigo.moveMouse(100, 100); console.log(result); } catch (error) { console.error('Error:', error); }}example();
Performs a mouse button action such as click, press, or release.
Parameters:
buttonName (string): The name of the button to use. Valid values are:
"left": Left mouse button
"right": Right mouse button
"middle": Middle mouse button
"scrollUp" or "scroll_up": Scroll up button
"scrollDown" or "scroll_down": Scroll down button
"scrollLeft" or "scroll_left": Scroll left button
"scrollRight" or "scroll_right": Scroll right button
action (string): The action to perform. Valid values are:
"click": Press and release the button
"press": Press and hold the button
"release": Release the button
Returns:
A Promise that resolves to a string with the result of the operation
Example with button click:
const enigo = require('node-enigo');async function example() { try { // Click the left mouse button const result = await enigo.mouseButton('left', 'click'); console.log(result); } catch (error) { console.error('Error:', error); }}example();
Example with press and release:
const enigo = require('node-enigo');async function example() { try { // Press and hold the right mouse button await enigo.mouseButton('right', 'press'); // Perform some other action while the button is held down await enigo.moveMouse(50, 50, true); // Release the right mouse button const result = await enigo.mouseButton('right', 'release'); console.log(result); } catch (error) { console.error('Error:', error); }}example();
Symbol keys: "-", "=", "[", "]", "\", ";", "'", ",", ".", "/", etc.
action (string): The action to perform. Valid values are:
"click": Press and release the key
"press": Press and hold the key
"release": Release the key
Returns:
A Promise that resolves to a string with the result of the operation
Example with key click:
const enigo = require('node-enigo');async function example() { try { // Press and release the Enter key const result = await enigo.key('return', 'click'); console.log(result); } catch (error) { console.error('Error:', error); }}example();
Example with key press and release:
const enigo = require('node-enigo');async function example() { try { // Press and hold the Shift key await enigo.key('shift', 'press'); // Type a character (will be uppercase due to Shift being held) await enigo.typeText('a'); // Release the Shift key const result = await enigo.key('shift', 'release'); console.log(result); } catch (error) { console.error('Error:', error); }}example();
keys(keyArray: string[]): Promise<string>
Simulates pressing multiple keys simultaneously (a keyboard shortcut). This function presses all modifier keys, then presses and releases non-modifier keys, and finally releases all modifier keys.
Parameters:
keyArray (string[]): An array of key names to press simultaneously. The same key names as in the key() function are supported.
Returns:
A Promise that resolves to a string with the result of the operation
Example with copy shortcut:
const enigo = require('node-enigo');async function example() { try { // Select some text first await enigo.keys(['ctrl', 'a']); // Select all // Copy the selected text (Ctrl+C on Windows/Linux, Cmd+C on macOS) const modifierKey = process.platform === 'darwin' ? 'cmd' : 'ctrl'; const result = await enigo.keys([modifierKey, 'c']); console.log(result); } catch (error) { console.error('Error:', error); }}example();
Example with more complex keyboard shortcut:
const enigo = require('node-enigo');async function example() { try { // Press Ctrl+Shift+Esc on Windows (Task Manager) const result = await enigo.keys(['ctrl', 'shift', 'escape']); console.log(result); } catch (error) { console.error('Error:', error); }}example();
Supported Platforms
macOS: x64, arm64
Windows: x64, ia32
Linux: x64, arm64
Permissions
macOS
On macOS, you need to grant accessibility permissions to the application running this code:
Go to System Preferences > Security & Privacy > Privacy > Accessibility
Click the lock icon to make changes (you'll need to enter your password)
Add your application to the list
Make sure the checkbox next to the application is checked
Without these permissions, the keyboard and mouse automation will not work.
Windows
On Windows, the application respects the user's scaling settings.
Linux
On Linux, make sure X11 permissions are set correctly.