The official Free Pascal wiki is back.

This copy is only for testing and development, and it may be out of date. Please read and edit the wiki at wiki.freepascal.org.

Go to the official wiki →

LazHIDControl: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: LazHIDControl

OldNewLine
1+**LazHIDControl** is an open-source desktop automation package for the [[Lazarus]] IDE and [[Free Pascal]]. A program that uses it can type text, press keys, move and click the mouse, register global hotkeys, find and control other programs' windows, take screenshots, and watch the clipboard, the power supply and session idle time.
2+
3+The project has three parts: ten nonvisual components for the form designer, the library underneath them, and **hidctl**, a command-line tool that drives any program from a script.
4+
5+- **Project:** [LazHIDControl on GitHub](https://github.com/TonyStone31/LazHIDControl)
6+- **Developer:** Tony Stone
7+- **License:** BSD Zero Clause (0BSD)
8+- **Language:** Object Pascal
9+- **Dependencies:** LCL and FCL. On Linux also the X11, Xtst and D-Bus libraries
10+- **Platforms:** Linux, Windows, macOS
11+
12+[[File:LazHIDControl same script on Linux GTK3.png|thumb|380px|The test window after one hidctl script has run, on Linux with GTK3]]
13+
14+## Overview
15+
16+LazHIDControl grew out of [[MouseAndKeyInput]], the package that ships with Lazarus for sending mouse and key input. It covers the same ground and adds window control, global hotkeys, screenshots, system monitoring and a scripting tool.
17+
18+The same code runs on Linux, Windows and macOS. Coordinates count from the inside of the target window, below its title bar, so a click lands in the same place on every system.
19+
20+Text is sent as characters, not as key positions, so it comes out right on any keyboard layout. Unicode text is supported.
21+
22+## Components
23+
24+Installing the package adds ten nonvisual components to the component palette.
25+
26+| Component | Description |
27+| --- | --- |
28+| `THIDHotkey` | Registers a global shortcut and fires `OnHotkey` from any application. |
29+| `THIDKeyboard` | Types text, Unicode included, and sends keys with modifiers to the focused application. |
30+| `THIDMouse` | Moves, clicks and scrolls. Coordinates can be relative to the screen, a control or a window. |
31+| `THIDWindow` | Finds windows by title, class or process. Waits for them, activates, moves and resizes them. |
32+| `THIDScreenshot` | Captures the desktop, a monitor, a window, a rectangle or a drag selection. |
33+| `THIDClipboardMonitor` | Fires `OnChanged` when the clipboard content changes, without reading it. |
34+| `THIDPowerMonitor` | Sleep and wake notifications, power source, battery percentage and charging state. |
35+| `THIDSystemIdle` | Fires `OnIdle` and `OnActive` as session input stops and resumes. |
36+| `THIDInputMonitor` | Reports key presses and mouse clicks anywhere on the system, for recording a task to play back. |
37+| `THIDPermission` | Explains which permissions the desktop needs and requests them. |
38+
39+Every component reports errors through `OnError` rather than dialogs, and does nothing in the designer or at form load beyond what was enabled.
40+
41+### Using the components
42+
43+```pascal
44+// A THIDWindow and a THIDMouse on a form, WindowTitle set in the Object Inspector
45+if Window.Find then
46+begin
47+ Window.Activate;
48+ Mouse.RelativeWindow := Window;
49+ Mouse.MoveTo(200, 150); // 200,150 inside that window, wherever it is
50+ Mouse.Click;
51+end;
52+```
53+
54+### Using the library directly
55+
56+The components are optional. The library can be called on its own:
57+
58+```pascal
59+uses HIDMouseAndKeyInput, LCLType;
60+
61+KeyInput.PressString('Hello World!');
62+KeyInput.Press(VK_RETURN);
63+MouseInput.Move([], 100, 100, 500); // move there over 500 ms
64+MouseInput.Click(mbLeft, []);
65+```
66+
67+### A text expander
68+
69+A `THIDHotkey` with `ShortCut` set to Ctrl+Alt+D and a `THIDKeyboard` beside it make a small text expander. The date is typed into whatever program has the focus:
70+
71+```pascal
72+procedure TForm1.HIDHotkey1Hotkey(Sender: TObject);
73+begin
74+ HIDKeyboard1.TypeText(FormatDateTime('yyyy-mm-dd', Date));
75+end;
76+```
77+
78+## hidctl
79+
80+**hidctl** is a command-line tool built on the same library. It accepts the command names of xdotool, chained on one line:
81+
82+```bash
83+hidctl search --name "Text Editor" windowactivate key ctrl+a type "Hello" key Return
84+```
85+
86+It also runs scripts, one command on each line, with coordinates relative to the target window. A script keeps working when the window opens somewhere else on the screen:
87+
88+```bash
89+./hidctl/hidctl --window "Some App" - <<EOF
90+waitfor 5000 Some App
91+at 300 200
92+click
93+type Hello from a script
94+shot /tmp/proof.png
95+EOF
96+```
97+
98+hidctl covers the pointer, the keyboard, finding and placing windows, screenshots, the clipboard, idle time and power status.
99+
100+### One script on every desktop
101+
102+[[File:LazHIDControl same script on Windows.png|thumb|300px|The same script on Windows 10]]
103+
104+The same hidctl script file drives the same program on Linux, Windows and macOS and gets the same result. The project's own test types Unicode text, selects with Shift, ticks a box, clicks a button, picks a list item with the arrow keys, presses a key combination, clicks, right-clicks, double-clicks, drags and scrolls. The program under test writes down what reached it, and that is compared line for line.
105+
106+[[File:LazHIDControl same script on macOS.png|thumb|300px|The same script on macOS 15]]
107+
108+This makes hidctl useful for documentation as well as testing. One script can walk a program through its screens and save a screenshot at each step, on each system.
109+
110+Things that do not carry from one system to another, and that a portable script avoids:
111+
112+- Shortcuts that differ by system, such as Ctrl+A in a Mac text field.
113+- Things placed by their size. List rows and menu items have different heights on each widget set, so they are better chosen with the arrow keys.
114+- Wayland, where no program is told where windows are. Keys and screen coordinates work there, window-relative commands do not.
115+
116+## Installation
117+
118+1. Download or clone the [LazHIDControl repository](https://github.com/TonyStone31/LazHIDControl).
119+2. Open Lazarus.
120+3. Select **Package → Open Package File (.lpk)**.
121+4. Open `lazhidcontrol/lazhidcontrol.lpk`.
122+5. Compile the package.
123+6. Select **Use → Install** to put the components on the palette.
124+
125+For library-only use, **Use → Add to Project** is enough.
126+
127+From a shell:
128+
129+```bash
130+lazbuild --add-package-link lazhidcontrol/lazhidcontrol.lpk
131+lazbuild examples/automation/AutomationDemo.lpi
132+lazbuild hidctl/hidctl.lpi
133+```
134+
135+## Supported platforms
136+
137+[[File:LazHIDControl demo windows tab on macOS.png|thumb|380px|The demo's Windows tab on macOS, listing the Mac's windows]]
138+
139+The project documentation reports testing on:
140+
141+| Platform | Environment |
142+| --- | --- |
143+| Linux, X11 | Linux Mint with GTK2 and GTK3. Qt5 on Ubuntu 26.04 |
144+| Windows | Windows 10 |
145+| macOS | macOS 15 on Intel, Lazarus 4.8, FPC 3.2.4 |
146+| Linux, Wayland | KDE Plasma 6 on Ubuntu 26.04, GTK3 |
147+
148+Typing, keys with modifiers, the mouse, screenshots, clipboard monitoring, idle time and power status are reported working on all four. Global hotkeys and window control work on X11, Windows and macOS.
149+
150+### Wayland
151+
152+Wayland does not tell a program where windows are, so other programs' windows cannot be found or moved there, and the mouse works in screen coordinates only. Global hotkeys are not available. Typing, the mouse and screenshots go through the desktop's portals, which ask the user once.
153+
154+### macOS
155+
156+[[File:LazHIDControl demo typing tab on macOS.png|thumb|380px|The demo's Typing tab on macOS]]
157+
158+macOS asks the user before a program may control other programs, under **System Settings → Privacy & Security**: *Accessibility* for typing, the mouse and moving windows, *Input Monitoring* for watching keys, and *Screen Recording* for screenshots of other programs' windows. `THIDPermission` explains and requests these.
159+
160+### Windows
161+
162+A program cannot send input to one running as administrator unless it runs as administrator too.
163+
164+## History
165+
166+LazHIDControl began from LazMouseAndKeyInput, the package Tom Gregorovic wrote in 2008 that ships with Lazarus in `components/mouseandkeyinput`, with its Cocoa units by Sammarco Francesco. Global hotkeys adapted from the Codebot library by Anthony Walter were added to it. See [[MouseAndKeyInput]] for that package.
167+
168+In October 2026 the keyboard, mouse and hotkey units were written new, so the package could be released under the 0BSD license. Releases before October 2026 were GPL-2.0-or-later, and copies of those stay under it.
169+
170+## Documentation
171+
172+- [README](https://github.com/TonyStone31/LazHIDControl/blob/master/README.md) — overview, the components, what is tested on each system.
173+- [Manual](https://github.com/TonyStone31/LazHIDControl/blob/master/MANUAL.md) — each component in detail, the library beneath them, and notes for each platform.
174+- [hidctl](https://github.com/TonyStone31/LazHIDControl/blob/master/hidctl/README.md) — the command-line tool and the xdotool commands it supports.
175+- [Issue tracker](https://github.com/TonyStone31/LazHIDControl/issues) — bug reports and feature requests.
176+
177+## License
178+
179+LazHIDControl is released under the **BSD Zero Clause License (0BSD)**.
180+
181+## See also
182+
183+- [[MouseAndKeyInput]] — the package it began from, which ships with Lazarus
184+- [[LazInk]]
185+- [[Lazarus Packages]]
186+- [[Projects using Lazarus - Components and Libraries]]
187+
188+Category:Lazarus
189+Category:Components

Back to history