|
| 1 | +--- |
| 2 | +order: 1 |
| 3 | +title: Quick Start |
| 4 | +description: Discover Linux input devices and read your first evdev events. |
| 5 | +--- |
| 6 | + |
| 7 | +# Quick Start |
| 8 | + |
| 9 | +This tutorial walks through discovering input devices and reading events. |
| 10 | + |
| 11 | +## Discover input devices |
| 12 | + |
| 13 | +First, find available input devices on your system: |
| 14 | + |
| 15 | +```lua |
| 16 | +local evdev = require "evdev" |
| 17 | + |
| 18 | +local devs = assert(evdev.devices.list_devices()) |
| 19 | +for _, dev in ipairs(devs) do |
| 20 | + print(dev.path, dev.name) |
| 21 | +end |
| 22 | +``` |
| 23 | + |
| 24 | +`list_devices()` scans `/dev/input/event*` and reads each device's metadata. It |
| 25 | +also resolves symlinks from `/dev/input/by-id/` and `/dev/input/by-path/` and |
| 26 | +attaches them as `id_aliases` and `path_aliases` fields. |
| 27 | + |
| 28 | +## Inspect a device |
| 29 | + |
| 30 | +If you know the path, inspect one device directly: |
| 31 | + |
| 32 | +```lua |
| 33 | +local path = "/dev/input/event1" |
| 34 | +local dev = assert(evdev.devices.device_info(path)) |
| 35 | +print(dev.name) |
| 36 | +``` |
| 37 | + |
| 38 | +::: details Device info |
| 39 | + |
| 40 | +<!-- @include: ../_snippets/device-info.md --> |
| 41 | + |
| 42 | +::: |
| 43 | + |
| 44 | +## Find devices by `name` or `path` |
| 45 | + |
| 46 | +Search for a device by its name or `/dev/input/` path: |
| 47 | + |
| 48 | +::: code-group |
| 49 | + |
| 50 | +```lua [by-name.lua] |
| 51 | +local name = "AT Translated Set 2 keyboard" |
| 52 | +local kb = evdev.devices.find(name) |
| 53 | +local devs = evdev.devices.find_all(name) |
| 54 | +``` |
| 55 | + |
| 56 | +```lua [by-event-path.lua] |
| 57 | +local path = "/dev/input/event0" |
| 58 | +local dev = evdev.devices.find(path) |
| 59 | +local devs = evdev.devices.find_all(path) |
| 60 | +``` |
| 61 | + |
| 62 | +```lua [by-path.lua] |
| 63 | +local path = "/dev/input/by-path/pci-example-00:00.0-event-kbd" |
| 64 | +local kb = evdev.devices.find(path) |
| 65 | +local devs = evdev.devices.find_all(path) |
| 66 | +``` |
| 67 | + |
| 68 | +```lua [by-id.lua] |
| 69 | +local path = "/dev/input/by-id/usb-Example_Vendor_1234-event-mouse" |
| 70 | +local mouse = evdev.devices.find(path) |
| 71 | +local devs = evdev.devices.find_all(path) |
| 72 | +``` |
| 73 | + |
| 74 | +::: |
| 75 | + |
| 76 | +> [!NOTE] |
| 77 | +> |
| 78 | +> `find()` returns the first match or `nil`. `find_all()` returns a list of all |
| 79 | +> matching devices. |
| 80 | +
|
| 81 | +## Open a device and read events |
| 82 | + |
| 83 | +Once you know which device you want, open it and read events using the |
| 84 | +`events()` iterator: |
| 85 | + |
| 86 | +```lua |
| 87 | +local path = "/dev/input/event3" |
| 88 | +local Device = evdev.device.open |
| 89 | + |
| 90 | +local dev = assert(Device(path)) |
| 91 | +print("Opened:", dev.name) |
| 92 | + |
| 93 | +for e in dev:events() do |
| 94 | + print(e.code, e.value) |
| 95 | +end |
| 96 | +``` |
| 97 | + |
| 98 | +The iterator polls and reads in a loop, yielding each event as it arrives. It |
| 99 | +runs until the device is closed or an error occurs. |
| 100 | + |
| 101 | +::: details Event fields |
| 102 | + |
| 103 | +<!-- @include: ../_snippets/event-fields.md --> |
| 104 | + |
| 105 | +::: |
0 commit comments