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 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11**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.
22
33The 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.
44
55- **Project:** [LazHIDControl on GitHub](https://github.com/TonyStone31/LazHIDControl)
66- **Developer:** Tony Stone
77- **License:** BSD Zero Clause (0BSD)
88- **Language:** Object Pascal
99- **Dependencies:** LCL and FCL. On Linux also the X11, Xtst and D-Bus libraries
1010- **Platforms:** Linux, Windows, macOS
1111
12-[[File:LazHIDControl same script on Linux GTK3.png|thumb|380px|The test window after one hidctl script has run, on Linux with GTK3]]
12+[[File:LazHIDControl demo windows tab on macOS.png|thumb|380px|The demo program on macOS, listing the windows that are open]]
1313
1414## Overview
1515
1616LazHIDControl 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.
1717
1818The 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.
1919
2020Text is sent as characters, not as key positions, so it comes out right on any keyboard layout. Unicode text is supported.
2121
2222## Components
2323
2424Installing the package adds ten nonvisual components to the component palette.
2525
2626| Component | Description |
2727| --- | --- |
2828| `THIDHotkey` | Registers a global shortcut and fires `OnHotkey` from any application. |
2929| `THIDKeyboard` | Types text, Unicode included, and sends keys with modifiers to the focused application. |
3030| `THIDMouse` | Moves, clicks and scrolls. Coordinates can be relative to the screen, a control or a window. |
3131| `THIDWindow` | Finds windows by title, class or process. Waits for them, activates, moves and resizes them. |
3232| `THIDScreenshot` | Captures the desktop, a monitor, a window, a rectangle or a drag selection. |
3333| `THIDClipboardMonitor` | Fires `OnChanged` when the clipboard content changes, without reading it. |
3434| `THIDPowerMonitor` | Sleep and wake notifications, power source, battery percentage and charging state. |
3535| `THIDSystemIdle` | Fires `OnIdle` and `OnActive` as session input stops and resumes. |
3636| `THIDInputMonitor` | Reports key presses and mouse clicks anywhere on the system, for recording a task to play back. |
3737| `THIDPermission` | Explains which permissions the desktop needs and requests them. |
3838
3939Every component reports errors through `OnError` rather than dialogs, and does nothing in the designer or at form load beyond what was enabled.
4040
4141### Using the components
4242
4343```pascal
4444// A THIDWindow and a THIDMouse on a form, WindowTitle set in the Object Inspector
4545if Window.Find then
4646begin
4747 Window.Activate;
4848 Mouse.RelativeWindow := Window;
4949 Mouse.MoveTo(200, 150); // 200,150 inside that window, wherever it is
5050 Mouse.Click;
5151end;
5252```
5353
5454### Using the library directly
5555
5656The components are optional. The library can be called on its own:
5757
5858```pascal
5959uses HIDMouseAndKeyInput, LCLType;
6060
6161KeyInput.PressString('Hello World!');
6262KeyInput.Press(VK_RETURN);
6363MouseInput.Move([], 100, 100, 500); // move there over 500 ms
6464MouseInput.Click(mbLeft, []);
6565```
6666
6767### A text expander
6868
6969A `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:
7070
7171```pascal
7272procedure TForm1.HIDHotkey1Hotkey(Sender: TObject);
7373begin
7474 HIDKeyboard1.TypeText(FormatDateTime('yyyy-mm-dd', Date));
7575end;
7676```
7777
7878## hidctl
7979
8080**hidctl** is a command-line tool built on the same library. It accepts the command names of xdotool, chained on one line:
8181
8282```bash
8383hidctl search --name "Text Editor" windowactivate key ctrl+a type "Hello" key Return
8484```
8585
8686It 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:
8787
8888```bash
8989./hidctl/hidctl --window "Some App" - <<EOF
9090waitfor 5000 Some App
9191at 300 200
9292click
9393type Hello from a script
9494shot /tmp/proof.png
9595EOF
9696```
9797
9898hidctl covers the pointer, the keyboard, finding and placing windows, screenshots, the clipboard, idle time and power status.
9999
100100### One script on every desktop
101101
102-[[File:LazHIDControl same script on Windows.png|thumb|300px|The same script on Windows 10]]
103-
104102The 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.
105103
106-[[File:LazHIDControl same script on macOS.png|thumb|300px|The same script on macOS 15]]
104+The test window after the script has run, one run on each system:
107105
106+| Linux, GTK3 | Windows 10 | macOS 15 |
107+| --- | --- | --- |
108+| [[File:LazHIDControl same script on Linux GTK3.png|frameless|230px]] | [[File:LazHIDControl same script on Windows.png|frameless|230px]] | [[File:LazHIDControl same script on macOS.png|frameless|230px]] |
109+
108110This 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.
109111
110112Things that do not carry from one system to another, and that a portable script avoids:
111113
112114- Shortcuts that differ by system, such as Ctrl+A in a Mac text field.
113115- 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.
114116- Wayland, where no program is told where windows are. Keys and screen coordinates work there, window-relative commands do not.
115117
116118## Installation
117119
1181201. Download or clone the [LazHIDControl repository](https://github.com/TonyStone31/LazHIDControl).
1191212. Open Lazarus.
1201223. Select **Package → Open Package File (.lpk)**.
1211234. Open `lazhidcontrol/lazhidcontrol.lpk`.
1221245. Compile the package.
1231256. Select **Use → Install** to put the components on the palette.
124126
125127For library-only use, **Use → Add to Project** is enough.
126128
127129From a shell:
128130
129131```bash
130132lazbuild --add-package-link lazhidcontrol/lazhidcontrol.lpk
131133lazbuild examples/automation/AutomationDemo.lpi
132134lazbuild hidctl/hidctl.lpi
133135```
134136
135137## 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]]
138138
139139The project documentation reports testing on:
140140
141141| Platform | Environment |
142142| --- | --- |
143143| Linux, X11 | Linux Mint with GTK2 and GTK3. Qt5 on Ubuntu 26.04 |
144144| Windows | Windows 10 |
145145| macOS | macOS 15 on Intel, Lazarus 4.8, FPC 3.2.4 |
146146| Linux, Wayland | KDE Plasma 6 on Ubuntu 26.04, GTK3 |
147147
148148Typing, 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.
149149
150150### Wayland
151151
152152Wayland 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.
153153
154154### macOS
155155
156156[[File:LazHIDControl demo typing tab on macOS.png|thumb|380px|The demo's Typing tab on macOS]]
157157
158158macOS 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.
159159
160160### Windows
161161
162162A program cannot send input to one running as administrator unless it runs as administrator too.
163163
164164## History
165165
166166LazHIDControl 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.
167167
168168In 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.
169169
170170## Documentation
171171
172172- [README](https://github.com/TonyStone31/LazHIDControl/blob/master/README.md) — overview, the components, what is tested on each system.
173173- [Manual](https://github.com/TonyStone31/LazHIDControl/blob/master/MANUAL.md) — each component in detail, the library beneath them, and notes for each platform.
174174- [hidctl](https://github.com/TonyStone31/LazHIDControl/blob/master/hidctl/README.md) — the command-line tool and the xdotool commands it supports.
175175- [Issue tracker](https://github.com/TonyStone31/LazHIDControl/issues) — bug reports and feature requests.
176176
177177## License
178178
179179LazHIDControl is released under the **BSD Zero Clause License (0BSD)**.
180180
181181## See also
182182
183183- [[MouseAndKeyInput]] — the package it began from, which ships with Lazarus
184184- [[LazInk]]
185185- [[Lazarus Packages]]
186186- [[Projects using Lazarus - Components and Libraries]]
187187
188188Category:Lazarus
189189Category:Components

Back to history