Skip to content

Commit 7979265

Browse files
committed
docs(evdev): add grabbing-and-repeat.md
1 parent 6384a27 commit 7979265

1 file changed

Lines changed: 27 additions & 77 deletions

File tree

Lines changed: 27 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
---
2+
order: 2
3+
title: Grabbing and Repeat
4+
description:
5+
Grab input devices exclusively and configure keyboard repeat behavior.
6+
---
7+
18
# Grabbing Devices and Configuring Repeat
29

310
This tutorial covers exclusive device access and auto-repeat settings.
@@ -10,95 +17,38 @@ compositors).
1017

1118
```lua
1219
local evdev = require "evdev"
13-
local dev = assert(evdev.device("/dev/input/event3"))
14-
15-
local ok, err = dev:grab()
16-
if not ok then
17-
error("grab failed: " .. err)
18-
end
19-
20-
-- Now only we receive events from this device
21-
for event in dev:events() do
22-
if event.type == evdev.ecodes.EV_KEY and event.value == 1 then
23-
print("key pressed:", event.code)
24-
end
25-
end
20+
local Device = evdev.device.open
2621

27-
dev:ungrab() -- release the grab
28-
```
22+
local path = "/dev/input/event3"
23+
local dev = assert(Device(path))
2924

30-
## Checking if a device is open
25+
assert(dev:grab())
3126

32-
```lua
33-
if dev:is_open() then
34-
print("device is still connected")
27+
-- After grab, events are delivered here and blocked from other programs
28+
for e in dev:events() do
29+
print("key pressed:", e.code)
3530
end
3631
```
3732

38-
If the device gets unplugged and you try to read it, `read()` returns
39-
`nil, err`.
40-
41-
## Device info after opening
42-
43-
```lua
44-
print(dev.name)
45-
```
33+
> [!TIP]
34+
>
35+
> Call `dev:flush()` before grabbing to discard stale buffered events.
4636
4737
## Auto-repeat settings
4838

49-
Linux input devices have built-in key repeat (initial delay and repeat rate).
50-
You can read and write these:
39+
Linux input devices have built-in key repeat with two parameters: **delay** (ms)
40+
and the repeat **period** (ms between repeats).
5141

5242
```lua
53-
-- Read current settings
43+
-- Read current kernel repeat settings
5444
local delay, period = assert(dev:get_repeat())
55-
print("repeat delay:", delay) -- milliseconds before repeat starts
56-
print("repeat period:", period) -- milliseconds between repeats
57-
58-
-- Set both at once
59-
assert(dev:set_repeat(300, 40))
60-
61-
-- Verify
62-
print(assert(dev:get_repeat()))
63-
```
45+
print("repeat delay:", delay) -- ms before repeat starts
46+
print("repeat period:", period) -- ms between repeats
6447

65-
These are backed by the `EVIOCGREP` / `EVIOCSREP` ioctls and are live changes to
66-
the kernel's repeat parameters for that device.
48+
-- Set both at once (200 ms delay, 20 ms between repeats)
49+
assert(dev:set_repeat(200, 20))
6750

68-
## Flushing buffered events
69-
70-
Clear any pending events from the kernel buffer:
71-
72-
```lua
73-
dev:flush()
51+
delay, period = assert(dev:get_repeat())
52+
print("new delay:", delay)
53+
print("new period:", period)
7454
```
75-
76-
Useful before starting a grab so stale buffered events don't interfere.
77-
78-
## Complete grab example
79-
80-
```lua
81-
local evdev = require "evdev"
82-
local dev = assert(evdev.device("/dev/input/event3"))
83-
84-
assert(dev:grab())
85-
dev:flush()
86-
87-
local last_key
88-
local press_count = 0
89-
90-
for event in dev:events() do
91-
if event.type == evdev.ecodes.EV_KEY then
92-
if event.value == 1 then
93-
press_count = press_count + 1
94-
last_key = event.code
95-
print(string.format("[%d] %s pressed", press_count, last_key))
96-
elseif event.value == 2 then
97-
print(last_key, "repeat")
98-
end
99-
end
100-
end
101-
```
102-
103-
Remember to `ungrab()` when done, though `close()` also releases the grab
104-
automatically.

0 commit comments

Comments
 (0)