LazHIDControl: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 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 | 2 | ||
| 3 | 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 | 4 | ||
| 5 | 5 | - **Project:** [LazHIDControl on GitHub](https://github.com/TonyStone31/LazHIDControl) | |
| 6 | 6 | - **Developer:** Tony Stone | |
| 7 | 7 | - **License:** BSD Zero Clause (0BSD) | |
| 8 | 8 | - **Language:** Object Pascal | |
| 9 | 9 | - **Dependencies:** LCL and FCL. On Linux also the X11, Xtst and D-Bus libraries | |
| 10 | 10 | - **Platforms:** Linux, Windows, macOS | |
| 11 | 11 | ||
| 12 | - | ||
| 12 | + | [[File:LazHIDControl demo windows tab on macOS.png|thumb|380px|The demo program on macOS, listing the windows that are open]] | |
| 13 | 13 | ||
| 14 | 14 | ## Overview | |
| 15 | 15 | ||
| 16 | 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 | 17 | ||
| 18 | 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 | 19 | ||
| 20 | 20 | Text is sent as characters, not as key positions, so it comes out right on any keyboard layout. Unicode text is supported. | |
| 21 | 21 | ||
| 22 | 22 | ## Components | |
| 23 | 23 | ||
| 24 | 24 | Installing the package adds ten nonvisual components to the component palette. | |
| 25 | 25 | ||
| 26 | 26 | | Component | Description | | |
| 27 | 27 | | --- | --- | | |
| 28 | 28 | | `THIDHotkey` | Registers a global shortcut and fires `OnHotkey` from any application. | | |
| 29 | 29 | | `THIDKeyboard` | Types text, Unicode included, and sends keys with modifiers to the focused application. | | |
| 30 | 30 | | `THIDMouse` | Moves, clicks and scrolls. Coordinates can be relative to the screen, a control or a window. | | |
| 31 | 31 | | `THIDWindow` | Finds windows by title, class or process. Waits for them, activates, moves and resizes them. | | |
| 32 | 32 | | `THIDScreenshot` | Captures the desktop, a monitor, a window, a rectangle or a drag selection. | | |
| 33 | 33 | | `THIDClipboardMonitor` | Fires `OnChanged` when the clipboard content changes, without reading it. | | |
| 34 | 34 | | `THIDPowerMonitor` | Sleep and wake notifications, power source, battery percentage and charging state. | | |
| 35 | 35 | | `THIDSystemIdle` | Fires `OnIdle` and `OnActive` as session input stops and resumes. | | |
| 36 | 36 | | `THIDInputMonitor` | Reports key presses and mouse clicks anywhere on the system, for recording a task to play back. | | |
| 37 | 37 | | `THIDPermission` | Explains which permissions the desktop needs and requests them. | | |
| 38 | 38 | ||
| 39 | 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 | 40 | ||
| 41 | 41 | ### Using the components | |
| 42 | 42 | ||
| 43 | 43 | ```pascal | |
| 44 | 44 | // A THIDWindow and a THIDMouse on a form, WindowTitle set in the Object Inspector | |
| 45 | 45 | if Window.Find then | |
| 46 | 46 | begin | |
| 47 | 47 | Window.Activate; | |
| 48 | 48 | Mouse.RelativeWindow := Window; | |
| 49 | 49 | Mouse.MoveTo(200, 150); // 200,150 inside that window, wherever it is | |
| 50 | 50 | Mouse.Click; | |
| 51 | 51 | end; | |
| 52 | 52 | ``` | |
| 53 | 53 | ||
| 54 | 54 | ### Using the library directly | |
| 55 | 55 | ||
| 56 | 56 | The components are optional. The library can be called on its own: | |
| 57 | 57 | ||
| 58 | 58 | ```pascal | |
| 59 | 59 | uses HIDMouseAndKeyInput, LCLType; | |
| 60 | 60 | ||
| 61 | 61 | KeyInput.PressString('Hello World!'); | |
| 62 | 62 | KeyInput.Press(VK_RETURN); | |
| 63 | 63 | MouseInput.Move([], 100, 100, 500); // move there over 500 ms | |
| 64 | 64 | MouseInput.Click(mbLeft, []); | |
| 65 | 65 | ``` | |
| 66 | 66 | ||
| 67 | 67 | ### A text expander | |
| 68 | 68 | ||
| 69 | 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 | 70 | ||
| 71 | 71 | ```pascal | |
| 72 | 72 | procedure TForm1.HIDHotkey1Hotkey(Sender: TObject); | |
| 73 | 73 | begin | |
| 74 | 74 | HIDKeyboard1.TypeText(FormatDateTime('yyyy-mm-dd', Date)); | |
| 75 | 75 | end; | |
| 76 | 76 | ``` | |
| 77 | 77 | ||
| 78 | 78 | ## hidctl | |
| 79 | 79 | ||
| 80 | 80 | **hidctl** is a command-line tool built on the same library. It accepts the command names of xdotool, chained on one line: | |
| 81 | 81 | ||
| 82 | 82 | ```bash | |
| 83 | 83 | hidctl search --name "Text Editor" windowactivate key ctrl+a type "Hello" key Return | |
| 84 | 84 | ``` | |
| 85 | 85 | ||
| 86 | 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 | 87 | ||
| 88 | 88 | ```bash | |
| 89 | 89 | ./hidctl/hidctl --window "Some App" - <<EOF | |
| 90 | 90 | waitfor 5000 Some App | |
| 91 | 91 | at 300 200 | |
| 92 | 92 | click | |
| 93 | 93 | type Hello from a script | |
| 94 | 94 | shot /tmp/proof.png | |
| 95 | 95 | EOF | |
| 96 | 96 | ``` | |
| 97 | 97 | ||
| 98 | 98 | hidctl covers the pointer, the keyboard, finding and placing windows, screenshots, the clipboard, idle time and power status. | |
| 99 | 99 | ||
| 100 | 100 | ### One script on every desktop | |
| 101 | 101 | ||
| 102 | - | ||
| 103 | - | ||
| 104 | 102 | 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 | 103 | ||
| 106 | - | ||
| 104 | + | The test window after the script has run, one run on each system: | |
| 107 | 105 | ||
| 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 | + | ||
| 108 | 110 | 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 | 111 | ||
| 110 | 112 | Things that do not carry from one system to another, and that a portable script avoids: | |
| 111 | 113 | ||
| 112 | 114 | - Shortcuts that differ by system, such as Ctrl+A in a Mac text field. | |
| 113 | 115 | - 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 | 116 | - Wayland, where no program is told where windows are. Keys and screen coordinates work there, window-relative commands do not. | |
| 115 | 117 | ||
| 116 | 118 | ## Installation | |
| 117 | 119 | ||
| 118 | 120 | 1. Download or clone the [LazHIDControl repository](https://github.com/TonyStone31/LazHIDControl). | |
| 119 | 121 | 2. Open Lazarus. | |
| 120 | 122 | 3. Select **Package → Open Package File (.lpk)**. | |
| 121 | 123 | 4. Open `lazhidcontrol/lazhidcontrol.lpk`. | |
| 122 | 124 | 5. Compile the package. | |
| 123 | 125 | 6. Select **Use → Install** to put the components on the palette. | |
| 124 | 126 | ||
| 125 | 127 | For library-only use, **Use → Add to Project** is enough. | |
| 126 | 128 | ||
| 127 | 129 | From a shell: | |
| 128 | 130 | ||
| 129 | 131 | ```bash | |
| 130 | 132 | lazbuild --add-package-link lazhidcontrol/lazhidcontrol.lpk | |
| 131 | 133 | lazbuild examples/automation/AutomationDemo.lpi | |
| 132 | 134 | lazbuild hidctl/hidctl.lpi | |
| 133 | 135 | ``` | |
| 134 | 136 | ||
| 135 | 137 | ## Supported platforms | |
| 136 | - | ||
| 137 | - | ||
| 138 | 138 | ||
| 139 | 139 | The project documentation reports testing on: | |
| 140 | 140 | ||
| 141 | 141 | | Platform | Environment | | |
| 142 | 142 | | --- | --- | | |
| 143 | 143 | | Linux, X11 | Linux Mint with GTK2 and GTK3. Qt5 on Ubuntu 26.04 | | |
| 144 | 144 | | Windows | Windows 10 | | |
| 145 | 145 | | macOS | macOS 15 on Intel, Lazarus 4.8, FPC 3.2.4 | | |
| 146 | 146 | | Linux, Wayland | KDE Plasma 6 on Ubuntu 26.04, GTK3 | | |
| 147 | 147 | ||
| 148 | 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 | 149 | ||
| 150 | 150 | ### Wayland | |
| 151 | 151 | ||
| 152 | 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 | 153 | ||
| 154 | 154 | ### macOS | |
| 155 | 155 | ||
| 156 | 156 | [[File:LazHIDControl demo typing tab on macOS.png|thumb|380px|The demo's Typing tab on macOS]] | |
| 157 | 157 | ||
| 158 | 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 | 159 | ||
| 160 | 160 | ### Windows | |
| 161 | 161 | ||
| 162 | 162 | A program cannot send input to one running as administrator unless it runs as administrator too. | |
| 163 | 163 | ||
| 164 | 164 | ## History | |
| 165 | 165 | ||
| 166 | 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 | 167 | ||
| 168 | 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 | 169 | ||
| 170 | 170 | ## Documentation | |
| 171 | 171 | ||
| 172 | 172 | - [README](https://github.com/TonyStone31/LazHIDControl/blob/master/README.md) — overview, the components, what is tested on each system. | |
| 173 | 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 | 174 | - [hidctl](https://github.com/TonyStone31/LazHIDControl/blob/master/hidctl/README.md) — the command-line tool and the xdotool commands it supports. | |
| 175 | 175 | - [Issue tracker](https://github.com/TonyStone31/LazHIDControl/issues) — bug reports and feature requests. | |
| 176 | 176 | ||
| 177 | 177 | ## License | |
| 178 | 178 | ||
| 179 | 179 | LazHIDControl is released under the **BSD Zero Clause License (0BSD)**. | |
| 180 | 180 | ||
| 181 | 181 | ## See also | |
| 182 | 182 | ||
| 183 | 183 | - [[MouseAndKeyInput]] — the package it began from, which ships with Lazarus | |
| 184 | 184 | - [[LazInk]] | |
| 185 | 185 | - [[Lazarus Packages]] | |
| 186 | 186 | - [[Projects using Lazarus - Components and Libraries]] | |
| 187 | 187 | ||
| 188 | 188 | Category:Lazarus | |
| 189 | 189 | Category:Components |