Methods
Keyboard
Keys
keyToggle and keyTap support for following keys.
Note: The names of the letter and number keys are the same as that single letter or digit. For example: b is the “b” key and 5 is the “5” key.
| Key | Description | Notes |
|---|---|---|
| backspace | ||
| delete | ||
| enter | ||
| tab | ||
| escape | ||
| up | Up arrow key | |
| down | Down arrow key | |
| right | Right arrow key | |
| left | Left arrow key | |
| home | ||
| end | ||
| pageup | ||
| pagedown | ||
| f1 | ||
| f2 | ||
| f3 | ||
| f4 | ||
| f5 | ||
| f6 | ||
| f7 | ||
| f8 | ||
| f9 | ||
| f10 | ||
| f11 | ||
| f12 | ||
| f13 | ||
| f14 | ||
| f15 | ||
| f16 | ||
| f17 | ||
| f18 | ||
| f19 | ||
| f20 | ||
| f21 | ||
| f22 | ||
| f23 | ||
| f24 | ||
| capslock | ||
| command | ||
| alt | ||
| right_alt | ||
| control | ||
| left_control | ||
| right_control | ||
| shift | ||
| right_shift | ||
| space | ||
| printscreen | No Mac support | |
| menu | Windows only | |
| insert | No Mac support | |
| audio_mute | Mute the volume | |
| audio_vol_down | Lower the volume | |
| audio_vol_up | Increase the volume | |
| audio_play | Play | |
| audio_stop | Stop | |
| audio_pause | Pause | |
| audio_prev | Previous Track | |
| audio_next | Next Track | |
| audio_rewind | Linux only | |
| audio_forward | Linux only | |
| audio_repeat | Linux only | |
| audio_random | Linux only | |
| numpad_lock | ||
| numpad_0 | No Linux support | |
| numpad_1 | No Linux support | |
| numpad_2 | No Linux support | |
| numpad_3 | No Linux support | |
| numpad_4 | No Linux support | |
| numpad_5 | No Linux support | |
| numpad_6 | No Linux support | |
| numpad_7 | No Linux support | |
| numpad_8 | No Linux support | |
| numpad_9 | No Linux support | |
| numpad_+ | ||
| numpad_- | ||
| numpad_* | ||
| numpad_/ | ||
| numpad_. | ||
| lights_mon_up | Turn up monitor brightness | No Windows support |
| lights_mon_down | Turn down monitor brightness | No Windows support |
| lights_kbd_toggle | Toggle keyboard backlight on/off | No Windows support |
| lights_kbd_up | Turn up keyboard backlight brightness | No Windows support |
| lights_kbd_down | Turn down keyboard backlight brightness | No Windows support |
Key list up to date as of RobotJS v0.8.x.
setKeyboardDelay(ms)
Sets the delay in milliseconds to sleep after a keyboard event. This is 10ms by default.
Arguments
| Argument | Description | Default |
|---|---|---|
ms |
Time to sleep in milliseconds. | None |
keyTap(key, [modifier])
Press a single key.
Arguments
| Argument | Description | Default |
|---|---|---|
key |
See keys. | None |
modifier |
String or an array. Accepts alt, right_alt, command, control, left_control, right_control, shift, and right_shift. |
None |
keyToggle(key, down, [modifier])
Hold down or release a key.
Arguments
| Argument | Description | Default |
|---|---|---|
key |
See keys. | None |
down |
Accepts down or up. | None |
modifier |
String or an array. Accepts alt, right_alt, command, control, left_control, right_control, shift, and right_shift. |
None |
unicodeTap(value)
Presses and releases one Unicode code point.
Arguments
| Argument | Description | Default |
|---|---|---|
value |
Numeric Unicode code point. | None |
typeString(string)
Types a string. On Linux, RobotJS 0.8 automatically applies the Shift or AltGr modifier required by the active keyboard layout.
Arguments
| Argument | Description | Default |
|---|---|---|
string |
The string to send. | None |
typeStringDelayed(string, cpm)
Types a string at a controlled rate.
Arguments
| Argument | Description | Default |
|---|---|---|
string |
The string to send. | None |
cpm |
Characters per minute. | None |
Mouse
setMouseDelay(ms)
Sets the delay in milliseconds to sleep after a mouse event. This is 10ms by default.
Arguments
| Argument | Description | Default |
|---|---|---|
ms |
Time to sleep in milliseconds. | None |
moveMouse(x, y)
Moves mouse to x, y instantly, with the mouse button up.
Arguments
| Argument | Description | Default |
|---|---|---|
x |
None | |
y |
None |
Examples
const robot = require("robotjs");
// Move the mouse to 100, 100 on the screen.
robot.moveMouse(100, 100);moveMouseSmooth(x, y, [speed])
Moves the mouse to x, y along a human-like path, with the mouse button up.
Arguments
| Argument | Description | Default |
|---|---|---|
x |
None | |
y |
None | |
speed |
Maximum delay between movements, in milliseconds. | 3.0 |
mouseClick([button], [double])
Clicks the mouse.
Arguments
| Argument | Description | Default |
|---|---|---|
button |
Accepts left, right, or middle. | left |
double |
Set to true to perform a double click. | false |
Examples
const robot = require("robotjs");
robot.mouseClick();mouseToggle([down], [button])
Toggles mouse button.
Arguments
| Argument | Description | Default |
|---|---|---|
down |
Accepts down or up. | down |
button |
Accepts left, right, or middle. | left |
Examples
const robot = require("robotjs");
robot.mouseToggle("down");
setTimeout(() => {
robot.mouseToggle("up");
}, 2000);dragMouse(x, y)
Moves mouse to x, y instantly, with the mouse button held down.
Arguments
| Argument | Description | Default |
|---|---|---|
x |
None | |
y |
None |
Examples
const robot = require("robotjs");
// Mouse down at 0, 0, then drag to 100, 100 and release.
robot.moveMouse(0, 0);
robot.mouseToggle("down");
robot.dragMouse(100, 100);
robot.mouseToggle("up");getMousePos()
Gets the mouse coordinates.
Return
Returns an object with keys x and y.
Examples
const robot = require("robotjs");
const mouse = robot.getMousePos();
console.log(`Mouse is at x:${mouse.x} y:${mouse.y}`);scrollMouse(x, y)
Scrolls the mouse in any direction.
Arguments
| Argument | Description | Default |
|---|---|---|
x |
The magnitude and direction of the scroll left and right. Left is negative. | None |
y |
The magnitude and direction of the scroll up and down. Down is negative. | None |
Examples
const robot = require("robotjs");
robot.scrollMouse(50, 0);
setTimeout(() => {
robot.scrollMouse(50, 0);
}, 2000);Screen
getPixelColor(x, y)
Gets one pixel from the main display. Use an Image search when reading a larger area.
Arguments
| Argument | Description | Default |
|---|---|---|
x |
Main-display x coordinate. | None |
y |
Main-display y coordinate. | None |
Return
Returns the pixel’s six-digit hexadecimal RGB color without a leading #.
getScreenSize()
Gets the main display’s logical width and height.
Return
Returns an object with .width and .height.
getDisplays()
Gets all active displays. Coordinates are signed desktop coordinates, so a display above or to the left of the main display can have a negative origin.
Return
Returns an array of display objects.
| Property | Description |
|---|---|
id |
Platform display identifier. |
x |
Display origin on the desktop x axis. |
y |
Display origin on the desktop y axis. |
width |
Logical display width. |
height |
Logical display height. |
isMain |
true for the main display. |
updateScreenMetrics()
Refreshes RobotJS’s cached screen metrics after the display arrangement or resolution changes.
screen.capture([x], [y], [width], [height])
Captures the main display or a rectangle contained by one active display.
Arguments
| Argument | Description | Default |
|---|---|---|
x |
Signed desktop x coordinate. | Main display origin |
y |
Signed desktop y coordinate. | Main display origin |
width |
Logical capture width. | Main display width |
height |
Logical capture height. | Main display height |
Pass either no arguments or all four arguments. A capture rectangle must stay inside one display; it cannot span displays.
Return
Returns an Image. Captures include screenX, screenY, scaleX, and scaleY, so image coordinates can be converted back to logical screen coordinates on high-density displays.
Example
const robot = require("robotjs");
const secondary = robot.getDisplays().find((display) => !display.isMain);
if (secondary) {
const image = robot.screen.capture(
secondary.x,
secondary.y,
secondary.width,
secondary.height
);
image.save("./secondary-display.bmp");
}Image
An Image is returned by screen.capture or robot.image.load. robot.Bitmap remains an alias for robot.Image.
robot.image.load(path)
Loads a .bmp or .png file and returns an Image. BMP is always supported. PNG requires a PNG-enabled build; check robot.image.supportsPNG first.
robot.image.save(image, path)
Saves an Image. The .bmp or .png file extension selects the output format. Returns true on success.
robot.image.supportsPNG
A boolean indicating whether this RobotJS build can load and save PNG files. See Building to enable PNG support in a source build.
Properties
| Property | Description |
|---|---|
width |
Image width in pixels. |
height |
Image height in pixels. |
image |
Raw pixel data as a Node.js Buffer. |
byteWidth |
Number of bytes in each row, including padding. |
bitsPerPixel |
Pixel depth, either 24 or 32. |
bytesPerPixel |
Number of bytes per pixel, either 3 or 4. |
screenX |
Logical screen x origin; present on screen captures. |
screenY |
Logical screen y origin; present on screen captures. |
scaleX |
Horizontal capture pixels per logical screen unit; present on screen captures. |
scaleY |
Vertical capture pixels per logical screen unit; present on screen captures. |
Search options
Color and image search methods accept the same optional object.
| Option | Description | Default |
|---|---|---|
x |
Search rectangle x origin in image pixels. | 0 |
y |
Search rectangle y origin in image pixels. | 0 |
width |
Search rectangle width. | Remaining image width |
height |
Search rectangle height. | Remaining image height |
tolerance |
Maximum normalized per-channel color difference, from 0.0 to 1.0. |
0.0 (exact) |
The search rectangle must remain inside the Image.
colorAt(x, y)
Returns the six-digit hexadecimal RGB color at an image point without a leading #.
findColor(color, [options])
Returns the first matching { x, y } image point, or null. color accepts RRGGBB or #RRGGBB.
findColors(color, [options])
Returns every matching { x, y } image point, or an empty array.
countColor(color, [options])
Returns the number of matching pixels.
findImage(needle, [options])
Returns the top-left { x, y } image point of the first matching Image, or null.
findImages(needle, [options])
Returns the top-left image points of every match, including overlapping matches, or an empty array.
countImage(needle, [options])
Returns the number of image matches, including overlapping matches.
Example
const robot = require("robotjs");
const screen = robot.screen.capture();
const target = robot.image.load("./target.bmp");
const match = screen.findImage(target, { tolerance: 0.1 });
if (match) {
screen.click(match, target);
}Search methods return image-space coordinates. A screen capture’s click helpers account for its display origin and pixel density.
save(path)
Saves this Image as .bmp or .png and returns true on success. This is equivalent to robot.image.save(image, path).
toScreenPoint(point, [target])
Converts an image-space point to a logical screen point. When target supplies width and height, the returned point is centered within that target.
click(point, [target], [button], [double])
Moves the mouse to an image point and clicks it. Pass the matched target as target to click its center. button accepts left, right, or middle; double defaults to false. Returns the logical screen point that was clicked.
clickImage(target, [options], [button], [double])
Finds target and clicks its center. Returns the match’s top-left image point, or null without moving or clicking when no match is found.