Skip to content

Commit 9341911

Browse files
committed
docs: document image search and capture APIs.
1 parent 0069e03 commit 9341911

1 file changed

Lines changed: 36 additions & 10 deletions

File tree

README.md

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ var mouse = robot.getMousePos();
8383
var hex = robot.getPixelColor(mouse.x, mouse.y);
8484
console.log("#" + hex + " at x:" + mouse.x + " y:" + mouse.y);
8585
```
86+
87+
##### Image Search
88+
89+
```JavaScript
90+
const robot = require("robotjs");
91+
92+
const screen = robot.screen.capture();
93+
const target = robot.image.load("./target.bmp");
94+
const match = screen.findImage(target, { tolerance: 0.1 });
95+
96+
if (match) {
97+
screen.click(match, target);
98+
}
99+
```
100+
101+
Captured and loaded images also provide `findImages`, `countImage`, `findColor`,
102+
`findColors`, `countColor`, `colorAt`, and `save`. Image searches return the
103+
target's top-left capture coordinates. `click` converts those coordinates to
104+
screen coordinates and clicks the target's center.
86105
Read the [Wiki](https://github.com/octalmage/robotjs/wiki) for more information!
87106

88107
## [API](http://robotjs.dev/docs/syntax)
@@ -94,14 +113,18 @@ The RobotJS API is hosted at <https://robotjs.dev/docs/syntax>.
94113
Please ensure you have the required dependencies before installing:
95114

96115
* Windows
97-
* windows-build-tools npm package (`npm install --global --production windows-build-tools` from an elevated PowerShell or CMD.exe)
98-
* Mac
116+
* A supported Visual Studio C++ toolchain.
117+
* macOS
99118
* Xcode Command Line Tools.
100119
* Linux
101-
* Python (v2.7 recommended, v3.x.x is not supported).
120+
* Python 3.
102121
* make.
103122
* A C/C++ compiler like GCC.
104-
* libxtst-dev and libpng++-dev (`sudo apt-get install libxtst-dev libpng++-dev`).
123+
* libxtst-dev (`sudo apt-get install libxtst-dev`).
124+
125+
BMP image loading and saving is always available. PNG support is optional for
126+
source builds. Set `ROBOTJS_ENABLE_PNG=1` and install `libpng` and `pkg-config`
127+
before building on macOS or Linux. Check `robot.image.supportsPNG` at runtime.
105128

106129
Install node-gyp using npm:
107130

@@ -122,7 +145,7 @@ See the [node-gyp readme](https://github.com/nodejs/node-gyp#installation) for m
122145
* √ Control the mouse by changing the mouse position, left/right clicking, and dragging.
123146
* √ Control the keyboard by pressing keys, holding keys down, and typing words.
124147
* √ Read pixel color from the screen and capture the screen.
125-
* Find an image on screen, read pixels from an image.
148+
* Find images and colors in captures, and load or save bitmap files.
126149
* Possibly include window management?
127150

128151
## Progress
@@ -131,26 +154,29 @@ See the [node-gyp readme](https://github.com/nodejs/node-gyp#installation) for m
131154
| ------------- |-------------: | ------- |
132155
| Mouse | 100% | All planned features implemented. |
133156
| Keyboard | 100% | All planned features implemented. |
134-
| Screen | 85% | Image search, pixel search. |
135-
| Bitmap | 0% | Saving/opening, png support. |
157+
| Screen | 100% | Screen capture, image search, and pixel search. |
158+
| Bitmap | 100% | BMP I/O and optional PNG support. |
136159

137160
## FAQ
138161

139162
#### Does RobotJS support global hotkeys?
140163

141164
Not currently, and I don't know if it ever will. I personally use [Electron](http://electron.atom.io/)/[NW.js](http://nwjs.io/) for global hotkeys, and this works well. Later on I might add hotkey support or create a separate module. See [#55](https://github.com/octalmage/robotjs/issues/55) for details.
142165

143-
#### Can I take a screenshot with RobotJS?
166+
#### Can I take a screenshot with RobotJS?
144167

145-
Soon! This is a bit more complicated than the rest of the features, so I saved it for last. Luckily the code is already there, I just need to write the bindings, and I've already started. Subscribe to [#13](https://github.com/octalmage/robotjs/issues/13) for updates.
168+
Yes. `robot.screen.capture()` captures the main display. Pass
169+
`x, y, width, height` to capture a specific rectangle.
146170

147171
#### Why is &#60;insert key&#62; missing from the keyboard functions?
148172

149173
We've been implementing keys as we need them. Feel free to create an issue or submit a pull request!
150174

151175
#### How about multi-monitor support?
152176

153-
The library doesn't have explicit multi-monitor support, so anything that works is kind of on accident. Subscribe to [#88](https://github.com/octalmage/robotjs/issues/88) for updates.
177+
Use `robot.getDisplays()` to inspect displays and pass a rectangle from one
178+
display to `robot.screen.capture(x, y, width, height)`. A capture rectangle
179+
cannot span multiple displays. Linux reports the current X11 screen.
154180

155181
For any other questions please [submit an issue](https://github.com/octalmage/robotjs/issues/new).
156182

0 commit comments

Comments
 (0)