|
| 1 | +--- |
| 2 | +description: "One Linux input event returned by `Device:read()`." |
| 3 | +--- |
| 4 | + |
| 5 | +# `device` |
| 6 | + |
| 7 | +One Linux input event returned by `Device:read()`. |
| 8 | + |
| 9 | +## Functions |
| 10 | + |
| 11 | +| Function | Description | |
| 12 | +| --------------------------------------------- | --------------------------------------------------------------------- | |
| 13 | +| [`close()`](#fn-close) | Close the device. | |
| 14 | +| [`events()`](#fn-events) | Return an iterator that waits for and yields input events one by one. | |
| 15 | +| [`fd()`](#fn-fd) | Return the underlying Linux file descriptor. | |
| 16 | +| [`flush()`](#fn-flush) | Drain queued events and return how many were discarded. | |
| 17 | +| [`get_repeat()`](#fn-get-repeat) | Return the current auto-repeat delay and period in milliseconds. | |
| 18 | +| [`grab()`](#fn-grab) | Take exclusive control of the input device. | |
| 19 | +| [`is_device(value)`](#fn-is-device) | Return whether a value is an `evdev.Device` instance. | |
| 20 | +| [`is_open()`](#fn-is-open) | Return whether this device handle still has an open file descriptor. | |
| 21 | +| [`open(path)`](#fn-open) | Open an input device by path. | |
| 22 | +| [`poll()`](#fn-poll) | Wait in the kernel until this device has input available. | |
| 23 | +| [`read()`](#fn-read) | Read one input event. Returns `nil` when no event is queued. | |
| 24 | +| [`set_repeat(delay, period)`](#fn-set-repeat) | Set the auto-repeat delay and period in milliseconds. | |
| 25 | +| [`ungrab()`](#fn-ungrab) | Release exclusive control of the input device. | |
| 26 | + |
| 27 | +<a id="fn-close"></a> |
| 28 | + |
| 29 | +### `close()` |
| 30 | + |
| 31 | +Close the device. |
| 32 | + |
| 33 | +**Return**: |
| 34 | + |
| 35 | +- `ok` (`boolean`): `true` when the device closes successfully. |
| 36 | +- `err` (`string?`): Error message on failure. |
| 37 | + |
| 38 | +**Example**: |
| 39 | + |
| 40 | +```lua |
| 41 | +local Device = evdev.device.open |
| 42 | +local dev = assert(Device("/dev/input/eventX")) |
| 43 | + |
| 44 | +print(dev:is_open()) --> true |
| 45 | +dev:close() |
| 46 | +print(dev:is_open()) --> false |
| 47 | +``` |
| 48 | + |
| 49 | +<a id="fn-events"></a> |
| 50 | + |
| 51 | +### `events()` |
| 52 | + |
| 53 | +Return an iterator that waits for and yields input events one by one. |
| 54 | + |
| 55 | +**Return**: |
| 56 | + |
| 57 | +- `evdev.event?` (`fun():`) |
| 58 | + |
| 59 | +**Example**: |
| 60 | + |
| 61 | +```lua |
| 62 | +local Device = evdev.device.open |
| 63 | +local dev = assert(Device("/dev/input/eventX")) |
| 64 | + |
| 65 | +for e in dev:events() do |
| 66 | + if e.type == ecodes.EV_KEY then |
| 67 | + print(e.code, e.value) |
| 68 | + end |
| 69 | +end |
| 70 | +``` |
| 71 | + |
| 72 | +<a id="fn-fd"></a> |
| 73 | + |
| 74 | +### `fd()` |
| 75 | + |
| 76 | +Return the underlying Linux file descriptor. |
| 77 | + |
| 78 | +**Return**: |
| 79 | + |
| 80 | +- `fd` (`evdev.fd?`): Linux file descriptor. |
| 81 | + |
| 82 | +**Example**: |
| 83 | + |
| 84 | +```lua |
| 85 | +local Device = evdev.device.open |
| 86 | +local dev = assert(Device("/dev/input/eventX")) |
| 87 | + |
| 88 | +local fd = dev:fd() |
| 89 | +print(fd) |
| 90 | +``` |
| 91 | + |
| 92 | +<a id="fn-flush"></a> |
| 93 | + |
| 94 | +### `flush()` |
| 95 | + |
| 96 | +Drain queued events and return how many were discarded. |
| 97 | + |
| 98 | +This is useful after grabbing a device when you want to ignore any stale events |
| 99 | +that were already queued. |
| 100 | + |
| 101 | +**Return**: |
| 102 | + |
| 103 | +- `count` (`integer?`): Number of discarded events. |
| 104 | +- `err` (`string?`): Error message on failure. |
| 105 | + |
| 106 | +**Example**: |
| 107 | + |
| 108 | +```lua |
| 109 | +local Device = evdev.device.open |
| 110 | +local dev = assert(Device("/dev/input/eventX")) |
| 111 | + |
| 112 | +assert(dev:grab()) |
| 113 | +os.execute("sleep 3") |
| 114 | + |
| 115 | +-- Move the mouse or press keys during the sleep. |
| 116 | + |
| 117 | +local dropped = assert(dev:flush()) |
| 118 | +print("discarded", dropped, "stale events") |
| 119 | +``` |
| 120 | + |
| 121 | +<a id="fn-get-repeat"></a> |
| 122 | + |
| 123 | +### `get_repeat()` |
| 124 | + |
| 125 | +Return the current auto-repeat delay and period in milliseconds. |
| 126 | + |
| 127 | +**Return**: |
| 128 | + |
| 129 | +- `delay` (`integer?`): Initial delay before repeating (milliseconds). |
| 130 | +- `period` (`integer?`): Interval between repeats (milliseconds). |
| 131 | +- `err` (`string?`): Error message on failure. |
| 132 | + |
| 133 | +**Example**: |
| 134 | + |
| 135 | +```lua |
| 136 | +local Device = evdev.device.open |
| 137 | +local dev = assert(Device("/dev/input/eventX")) |
| 138 | + |
| 139 | +local delay, period, err = dev:get_repeat() |
| 140 | +assert(delay, err) |
| 141 | +print(delay, period) |
| 142 | +``` |
| 143 | + |
| 144 | +<a id="fn-grab"></a> |
| 145 | + |
| 146 | +### `grab()` |
| 147 | + |
| 148 | +Take exclusive control of the input device. |
| 149 | + |
| 150 | +**Return**: |
| 151 | + |
| 152 | +- `ok` (`true?`): `true` when the device is grabbed successfully. |
| 153 | +- `err` (`string?`): Error message on failure. |
| 154 | + |
| 155 | +**Example**: |
| 156 | + |
| 157 | +```lua |
| 158 | +local Device = evdev.device.open |
| 159 | +local dev = assert(Device("/dev/input/eventX")) |
| 160 | +assert(dev:grab()) |
| 161 | +``` |
| 162 | + |
| 163 | +<a id="fn-is-device"></a> |
| 164 | + |
| 165 | +### `is_device(value)` |
| 166 | + |
| 167 | +Return whether a value is an `evdev.Device` instance. |
| 168 | + |
| 169 | +**Parameters**: |
| 170 | + |
| 171 | +- `value` (`any`) |
| 172 | + |
| 173 | +**Return**: |
| 174 | + |
| 175 | +- **value** (`boolean`) |
| 176 | + |
| 177 | +**Example**: |
| 178 | + |
| 179 | +```lua |
| 180 | +local Device = evdev.device.open |
| 181 | +local is_device = evdev.device.is_device |
| 182 | + |
| 183 | +local dev = assert(Device("/dev/input/eventX")) |
| 184 | +print(is_device(dev)) --> true |
| 185 | +print(is_device({})) --> false |
| 186 | +``` |
| 187 | + |
| 188 | +<a id="fn-is-open"></a> |
| 189 | + |
| 190 | +### `is_open()` |
| 191 | + |
| 192 | +Return whether this device handle still has an open file descriptor. |
| 193 | + |
| 194 | +**Return**: |
| 195 | + |
| 196 | +- `isOpen` (`boolean`): `true` when the device is still open. |
| 197 | + |
| 198 | +**Example**: |
| 199 | + |
| 200 | +```lua |
| 201 | +local Device = evdev.device.open |
| 202 | +local dev = assert(Device("/dev/input/eventX")) |
| 203 | + |
| 204 | +if dev:is_open() |
| 205 | + then dev:close() |
| 206 | +end |
| 207 | +``` |
| 208 | + |
| 209 | +<a id="fn-open"></a> |
| 210 | + |
| 211 | +### `open(path)` |
| 212 | + |
| 213 | +Open an input device by path. |
| 214 | + |
| 215 | +**Parameters**: |
| 216 | + |
| 217 | +- `path` (`evdev.path`) |
| 218 | + |
| 219 | +**Return**: |
| 220 | + |
| 221 | +- `dev` (`evdev.Device?`): Open input device. |
| 222 | +- `err` (`string?`): Error message on failure. |
| 223 | + |
| 224 | +**Example**: |
| 225 | + |
| 226 | +```lua |
| 227 | +local Device = evdev.device.open |
| 228 | +local dev = assert(Device("/dev/input/eventX")) |
| 229 | +``` |
| 230 | + |
| 231 | +<a id="fn-poll"></a> |
| 232 | + |
| 233 | +### `poll()` |
| 234 | + |
| 235 | +Wait in the kernel until this device has input available. |
| 236 | + |
| 237 | +This does not spin the CPU. It returns when `read()` can fetch at least one |
| 238 | +queued event. |
| 239 | + |
| 240 | +**Return**: |
| 241 | + |
| 242 | +- `ready` (`boolean?`): `true` when input is ready to read. |
| 243 | +- `err` (`string?`): Error message on failure. |
| 244 | + |
| 245 | +**Example**: |
| 246 | + |
| 247 | +```lua |
| 248 | +local Device = evdev.device.open |
| 249 | +local ecodes = evdev.ecodes |
| 250 | + |
| 251 | +local dev = assert(Device("/dev/input/eventX")) |
| 252 | + |
| 253 | +-- This is the manual form of `dev:events()`. |
| 254 | +while true do |
| 255 | + if assert(dev:poll()) then |
| 256 | + local e = assert(dev:read()) |
| 257 | + if e.type == ecodes.EV_KEY then |
| 258 | + print(e.code, e.value) |
| 259 | + end |
| 260 | + end |
| 261 | +end |
| 262 | +``` |
| 263 | + |
| 264 | +<a id="fn-read"></a> |
| 265 | + |
| 266 | +### `read()` |
| 267 | + |
| 268 | +Read one input event. Returns `nil` when no event is queued. |
| 269 | + |
| 270 | +**Return**: |
| 271 | + |
| 272 | +- `event` (`evdev.event?`): Next queued input event. |
| 273 | +- `err` (`string?`): Error message on failure. |
| 274 | + |
| 275 | +**Example**: |
| 276 | + |
| 277 | +```lua |
| 278 | +local Device = evdev.device.open |
| 279 | +local dev = assert(Device("/dev/input/eventX")) |
| 280 | + |
| 281 | +-- This is the manual form of `dev:events()`. |
| 282 | +while true do |
| 283 | + if assert(dev:poll()) then |
| 284 | + local e = assert(dev:read()) |
| 285 | + if e.type == ecodes.EV_KEY then |
| 286 | + print(e.code, e.value) |
| 287 | + end |
| 288 | + end |
| 289 | +end |
| 290 | +``` |
| 291 | + |
| 292 | +<a id="fn-set-repeat"></a> |
| 293 | + |
| 294 | +### `set_repeat(delay, period)` |
| 295 | + |
| 296 | +Set the auto-repeat delay and period in milliseconds. |
| 297 | + |
| 298 | +**Parameters**: |
| 299 | + |
| 300 | +- `delay` (`integer`): Initial delay before repeating (milliseconds). |
| 301 | +- `period` (`integer`): Interval between repeats (milliseconds). |
| 302 | + |
| 303 | +**Return**: |
| 304 | + |
| 305 | +- `ok` (`true?`): `true` when the repeat settings are updated successfully. |
| 306 | +- `err` (`string?`): Error message on failure. |
| 307 | + |
| 308 | +**Example**: |
| 309 | + |
| 310 | +```lua |
| 311 | +local Device = evdev.device.open |
| 312 | +local dev = assert(Device("/dev/input/eventX")) |
| 313 | + |
| 314 | +local delay, period, err = dev:get_repeat() |
| 315 | +assert(delay, err) |
| 316 | +print(delay, period) |
| 317 | + |
| 318 | +assert(dev:set_repeat(300, 40)) |
| 319 | +print(dev:get_repeat()) |
| 320 | +``` |
| 321 | + |
| 322 | +<a id="fn-ungrab"></a> |
| 323 | + |
| 324 | +### `ungrab()` |
| 325 | + |
| 326 | +Release exclusive control of the input device. |
| 327 | + |
| 328 | +**Return**: |
| 329 | + |
| 330 | +- `ok` (`true?`): `true` when the grab is released successfully. |
| 331 | +- `err` (`string?`): Error message on failure. |
| 332 | + |
| 333 | +**Example**: |
| 334 | + |
| 335 | +```lua |
| 336 | +local Device = evdev.device.open |
| 337 | +local dev = assert(Device("/dev/input/eventX")) |
| 338 | +assert(dev:grap()) |
| 339 | +assert(dev:ungrab()) |
| 340 | +``` |
0 commit comments