| 1 |
Getting started |
| 2 |
--------------- |
| 3 |
|
| 4 |
|
| 5 |
Listing accessible event devices |
| 6 |
================================ |
| 7 |
|
| 8 |
:: |
| 9 |
|
| 10 |
>>> import evdev |
| 11 |
|
| 12 |
>>> devices = [evdev.InputDevice(path) for path in evdev.list_devices()] |
| 13 |
>>> for device in devices: |
| 14 |
... print(device.path, device.name, device.phys) |
| 15 |
/dev/input/event1 USB Keyboard usb-0000:00:12.1-2/input0 |
| 16 |
/dev/input/event0 USB Optical Mouse usb-0000:00:12.0-2/input0 |
| 17 |
|
| 18 |
.. note:: |
| 19 |
|
| 20 |
If no devices are shown, ensure your user belongs to the correct group |
| 21 |
(usually ``input``) to obtain read and write access. |
| 22 |
|
| 23 |
|
| 24 |
Reading events from a device |
| 25 |
============================ |
| 26 |
|
| 27 |
:: |
| 28 |
|
| 29 |
>>> import evdev |
| 30 |
|
| 31 |
>>> device = evdev.InputDevice("/dev/input/event1") |
| 32 |
>>> print(device) |
| 33 |
device /dev/input/event1, name "USB Keyboard", phys "usb-0000:00:12.1-2/input0" |
| 34 |
|
| 35 |
>>> for event in device.read_loop(): |
| 36 |
... if event.type == evdev.ecodes.EV_KEY: |
| 37 |
... print(evdev.categorize(event)) |
| 38 |
... # pressing "a" and holding "space" |
| 39 |
key event at 1337016188.396030, 30 (KEY_A), down |
| 40 |
key event at 1337016188.492033, 30 (KEY_A), up |
| 41 |
key event at 1337016189.772129, 57 (KEY_SPACE), down |
| 42 |
key event at 1337016190.275396, 57 (KEY_SPACE), hold |
| 43 |
key event at 1337016190.284160, 57 (KEY_SPACE), up |
| 44 |
|
| 45 |
|
| 46 |
Accessing event codes |
| 47 |
===================== |
| 48 |
|
| 49 |
The ``evdev.ecodes`` module provides reverse and forward mappings between the |
| 50 |
names and values of the event subsystem constants. |
| 51 |
|
| 52 |
:: |
| 53 |
|
| 54 |
>>> from evdev import ecodes |
| 55 |
|
| 56 |
>>> ecodes.KEY_A |
| 57 |
... 30 |
| 58 |
>>> ecodes.ecodes["KEY_A"] |
| 59 |
... 30 |
| 60 |
>>> ecodes.KEY[30] |
| 61 |
... "KEY_A" |
| 62 |
>>> ecodes.bytype[ecodes.EV_KEY][30] |
| 63 |
... "KEY_A" |
| 64 |
|
| 65 |
# A single value may correspond to multiple event codes. |
| 66 |
>>> ecodes.KEY[152] |
| 67 |
... ["KEY_COFFEE", "KEY_SCREENLOCK"] |
| 68 |
|
| 69 |
|
| 70 |
Listing and monitoring input devices |
| 71 |
==================================== |
| 72 |
|
| 73 |
The *python-evdev* package also comes with a small command-line program for |
| 74 |
listing and monitoring input devices: |
| 75 |
|
| 76 |
.. code-block:: bash |
| 77 |
|
| 78 |
$ python3 -m evdev.evtest |
| 79 |
|
| 80 |
ID Device Name Phys Uniq |
| 81 |
------------------------------------------------------------------------------------------- |
| 82 |
01 /dev/input/event11 Logitech MX Keys usb-0000:00:14.0-5.4.4/input2:2 9a-b0-2c-b4 |
| 83 |
02 /dev/input/event12 Logitech MX Master 3 usb-0000:00:14.0-5.4.4/input2:1 f8-b5-f2-aa |
| 84 |
|
| 85 |
Select devices [0-02]: 01 |
| 86 |
Listening for events (press ctrl-c to exit) ... |
| 87 |
time 1787478743.772676 type 4 (EV_MSC), code 4 (MSC_SCAN), value 458976 |
| 88 |
time 1787478743.772676 type 1 (EV_KEY), code 29 (KEY_LEFTCTRL), value 1 |
| 89 |
time 1787478743.772676 ------------- SYN_REPORT ------------- |
| 90 |
time 1787478743.872566 type 4 (EV_MSC), code 4 (MSC_SCAN), value 458758 |
| 91 |
time 1787478743.872566 type 1 (EV_KEY), code 46 (KEY_C), value 1 |
| 92 |
time 1787478743.872566 ------------- SYN_REPORT ------------- |