Robot TS 🤖
This library is inspired by older unmaintained libraries like octalmage/robotjs and Robot/robot-js. The goal is to provide cross-platform controls for various devices such as keyboard, mouse, and screen for Node.js applications.
You can find C++ implementation CMake static library
Supported system:
- MacOS
- Windows (not tested yet)
In case of Linux, please, create issue and leave a star
Known issues:
- Work in progress. If you need specific features, please, create an issue and I will prioritize it.
- I never tested this on Windows.
🙏 - It seems that special keys bindings are not implemented correctly.
- Upper case is converted to lower case when typing. (it will be possible to use shift key when special keys are fixed though)
Installation:
Make sure that you can build C++ projects on your machine and that you have CMake installed.
- On MacOS:
brew install cmake - On Windows:
choco install cmake
Install Node dependencies:
yarn add robot-tsMouse 🖱️
The Mouse class provides a static interface for controlling the mouse cursor, simulating mouse clicks, and scrolling.
Public Methods
-
static move(point: Point): voidMoves the mouse cursor to the specified point (x, y). -
static moveSmooth(point: Point, speed?: number): voidMoves the mouse cursor smoothly to the specified point (x, y) at the given speed. -
static drag(point: Point, speed?: number): voidDrags the mouse cursor to the specified point (x, y) at the given speed. -
static getPosition(): PointReturns the current position of the mouse cursor as aPoint. -
static toggleButton(down: boolean, button: MouseButton, doubleClick?: boolean): voidPresses or releases the specified mouse button depending on thedownargument. IfdoubleClickis set to true, it will perform a double click. -
static click(button: MouseButton): voidSimulates a single click using the specified mouse button. -
static doubleClick(button: MouseButton): voidSimulates a double click using the specified mouse button. -
static scrollBy(y: number, x?: number): voidScrolls the mouse wheel by the specified x and y distances.
Example Usage
import { Mouse, Point } from "./bindings";
Mouse.moveSmooth(new Point(100, 200));Keyboard ⌨️
The Keyboard class provides a static interface for simulating keyboard key presses, releases, and typing.
Public Methods
-
static type(query: string): voidTypes the given text as a string. -
static typeHumanLike(query: string): voidTypes the given text as a string with a human-like typing speed. -
static click(asciiChar: string): voidSimulates a key press and release for the specified ASCII character. -
static click(specialKey: SpecialKey): voidSimulates a key press and release for the specified special key. -
static press(asciiChar: string): voidSimulates a key press for the specified ASCII character. -
static press(specialKey: SpecialKey): voidSimulates a key press for the specified special key. -
static release(asciiChar: string): voidSimulates a key release for the specified ASCII character. -
static release(specialKey: SpecialKey): voidSimulates a key release for the specified special key.
Example Usage
import { Keyboard } from "./bindings";
Keyboard.typeHumanLike("hello, world");Screen 🖥️
The Screen class provides functionality to capture the screen, get pixel colors, and save the captured screen as a PNG image.
Public Methods
-
getPixelColor(x: number, y: number): PixelReturns the color of the pixel at the specified (x, y) coordinates as aPixelstructure. -
getScreenSize(): DisplaySizeReturns the size of the screen as aDisplaySizestructure containing the width and height. -
capture(x?: number, y?: number, width?: number, height?: number): voidCaptures a rectangular area of the screen defined by the specified (x, y) coordinates and dimensions (width, height). -
getPixels(): Pixel[]Returns an array ofPixelstructures representing the captured screen. -
saveAsPNG(filename: string): voidSaves the captured screen as a PNG image with the specified filename.
Structures
DisplaySize
DisplaySize is a structure that represents the size of a display with integer dimensions (width, height).
Attributes
-
width: numberThe width of the display. -
height: numberThe height of the display.
Pixel
Pixel is a structure that represents the color of a pixel with unsigned char values for red, green, and blue channels.
Attributes
-
r: numberThe red channel value of the pixel. -
g: numberThe green channel value of the pixel. -
b: numberThe blue channel value of the pixel.
Example Usage
import { Screen } from "./bindings";
const screen = new Screen();
screen.capture(0, 0, 800, 600);
const pixel = screen.getPixelColor(100, 200);
screen.saveAsPNG("screenshot.png");