| 1 |
Tutorial |
| 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 Dell Dell USB Keyboard usb-0000:00:12.1-2/input0 |
| 16 |
/dev/input/event0 Dell USB Optical Mouse usb-0000:00:12.0-2/input0 |
| 17 |
|
| 18 |
|
| 19 |
Listing device capabilities |
| 20 |
=========================== |
| 21 |
|
| 22 |
:: |
| 23 |
|
| 24 |
>>> import evdev |
| 25 |
|
| 26 |
>>> device = evdev.InputDevice("/dev/input/event0") |
| 27 |
>>> print(device) |
| 28 |
device /dev/input/event0, name "Dell USB Optical Mouse", phys "usb-0000:00:12.0-2/input0" |
| 29 |
|
| 30 |
>>> device.capabilities() |
| 31 |
... { 0: [0, 1, 2], 1: [272, 273, 274, 275], 2: [0, 1, 6, 8], 4: [4] } |
| 32 |
|
| 33 |
>>> device.capabilities(verbose=True) |
| 34 |
... { ("EV_SYN", 0): [("SYN_REPORT", 0), ("SYN_CONFIG", 1), ("SYN_MT_REPORT", 2)], |
| 35 |
... ("EV_KEY", 1): [("BTN_MOUSE", 272), ("BTN_RIGHT", 273), ("BTN_MIDDLE", 274), ("BTN_SIDE", 275)], ... |
| 36 |
|
| 37 |
|
| 38 |
Listing device capabilities (devices with absolute axes) |
| 39 |
======================================================== |
| 40 |
|
| 41 |
:: |
| 42 |
|
| 43 |
>>> import evdev |
| 44 |
|
| 45 |
>>> device = evdev.InputDevice("/dev/input/event7") |
| 46 |
>>> print(device) |
| 47 |
device /dev/input/event7, name "Wacom Bamboo 2FG 4x5 Finger", phys "" |
| 48 |
|
| 49 |
>>> device.capabilities() |
| 50 |
... { 1: [272, 273, 277, 278, 325, 330, 333] , |
| 51 |
... 3: [(0, AbsInfo(min=0, max=15360, fuzz=128, flat=0)), |
| 52 |
... (1, AbsInfo(min=0, max=10240, fuzz=128, flat=0))] } |
| 53 |
|
| 54 |
>>> device.capabilities(verbose=True) |
| 55 |
... { ("EV_KEY", 1): [("BTN_MOUSE", 272), ("BTN_RIGHT", 273), ...], |
| 56 |
... ("EV_ABS", 3): [(("ABS_X", 0), AbsInfo(min=0, max=15360, fuzz=128, flat=0)), |
| 57 |
... (("ABS_Y", 1), AbsInfo(min=0, max=10240, fuzz=128, flat=0)),] } |
| 58 |
|
| 59 |
>>> device.capabilities(absinfo=False) |
| 60 |
... { 1: [272, 273, 277, 278, 325, 330, 333], |
| 61 |
... 3: [0, 1, 47, 53, 54, 57] } |
| 62 |
|
| 63 |
|
| 64 |
Getting and setting LED states |
| 65 |
============================== |
| 66 |
|
| 67 |
:: |
| 68 |
|
| 69 |
>>> dev.leds(verbose=True) |
| 70 |
... [("LED_NUML", 0), ("LED_CAPSL", 1)] |
| 71 |
|
| 72 |
>>> dev.leds() |
| 73 |
... [0, 1] |
| 74 |
|
| 75 |
>>> dev.set_led(ecodes.LED_NUML, 1) # enable numlock |
| 76 |
>>> dev.set_led(ecodes.LED_NUML, 0) # disable numlock |
| 77 |
|
| 78 |
|
| 79 |
Getting currently active keys |
| 80 |
============================= |
| 81 |
|
| 82 |
:: |
| 83 |
|
| 84 |
>>> dev.active_keys(verbose=True) |
| 85 |
... [("KEY_3", 4), ("KEY_LEFTSHIFT", 42)] |
| 86 |
|
| 87 |
>>> dev.active_keys() |
| 88 |
... [4, 42] |
| 89 |
|
| 90 |
|
| 91 |
Reading events |
| 92 |
============== |
| 93 |
|
| 94 |
Reading events from a single device in an endless loop. |
| 95 |
|
| 96 |
:: |
| 97 |
|
| 98 |
>>> from evdev import InputDevice, categorize, ecodes |
| 99 |
>>> dev = InputDevice("/dev/input/event1") |
| 100 |
|
| 101 |
>>> print(dev) |
| 102 |
device /dev/input/event1, name "Dell Dell USB Keyboard", phys "usb-0000:00:12.1-2/input0" |
| 103 |
|
| 104 |
>>> for event in dev.read_loop(): |
| 105 |
... if event.type == ecodes.EV_KEY: |
| 106 |
... print(categorize(event)) |
| 107 |
... # pressing "a" and holding "space" |
| 108 |
key event at 1337016188.396030, 30 (KEY_A), down |
| 109 |
key event at 1337016188.492033, 30 (KEY_A), up |
| 110 |
key event at 1337016189.772129, 57 (KEY_SPACE), down |
| 111 |
key event at 1337016190.275396, 57 (KEY_SPACE), hold |
| 112 |
key event at 1337016190.284160, 57 (KEY_SPACE), up |
| 113 |
|
| 114 |
|
| 115 |
Reading events (using :mod:`asyncio`) |
| 116 |
====================================== |
| 117 |
|
| 118 |
:: |
| 119 |
|
| 120 |
>>> import asyncio |
| 121 |
>>> from evdev import InputDevice |
| 122 |
|
| 123 |
>>> dev = InputDevice("/dev/input/event1") |
| 124 |
|
| 125 |
>>> async def main(dev): |
| 126 |
... async for ev in dev.async_read_loop(): |
| 127 |
... print(repr(ev)) |
| 128 |
|
| 129 |
>>> asyncio.run(main(dev)) |
| 130 |
InputEvent(1527363738, 348740, 4, 4, 458792) |
| 131 |
InputEvent(1527363738, 348740, 1, 28, 0) |
| 132 |
InputEvent(1527363738, 348740, 0, 0, 0) |
| 133 |
|
| 134 |
|
| 135 |
Reading events from multiple devices (using :mod:`select`) |
| 136 |
========================================================== |
| 137 |
|
| 138 |
:: |
| 139 |
|
| 140 |
>>> from evdev import InputDevice |
| 141 |
>>> from select import select |
| 142 |
|
| 143 |
# A mapping of file descriptors (integers) to InputDevice instances. |
| 144 |
>>> devices = map(InputDevice, ("/dev/input/event1", "/dev/input/event2")) |
| 145 |
>>> devices = {dev.fd: dev for dev in devices} |
| 146 |
|
| 147 |
>>> for dev in devices.values(): print(dev) |
| 148 |
device /dev/input/event1, name "Dell Dell USB Keyboard", phys "usb-0000:00:12.1-2/input0" |
| 149 |
device /dev/input/event2, name "Logitech USB Laser Mouse", phys "usb-0000:00:12.0-2/input0" |
| 150 |
|
| 151 |
>>> while True: |
| 152 |
... r, w, x = select(devices, [], []) |
| 153 |
... for fd in r: |
| 154 |
... for event in devices[fd].read(): |
| 155 |
... print(event) |
| 156 |
event at 1351116708.002230, code 01, type 02, val 01 |
| 157 |
event at 1351116708.002234, code 00, type 00, val 00 |
| 158 |
event at 1351116708.782231, code 04, type 04, val 458782 |
| 159 |
event at 1351116708.782237, code 02, type 01, val 01 |
| 160 |
|
| 161 |
|
| 162 |
Reading events from multiple devices (using :mod:`selectors`) |
| 163 |
============================================================= |
| 164 |
|
| 165 |
This can also be achieved using the :mod:`selectors` module in Python 3.4: |
| 166 |
|
| 167 |
:: |
| 168 |
|
| 169 |
from evdev import InputDevice |
| 170 |
from selectors import DefaultSelector, EVENT_READ |
| 171 |
|
| 172 |
selector = DefaultSelector() |
| 173 |
|
| 174 |
mouse = InputDevice("/dev/input/event1") |
| 175 |
keybd = InputDevice("/dev/input/event2") |
| 176 |
|
| 177 |
# This works because InputDevice has a `fileno()` method. |
| 178 |
selector.register(mouse, EVENT_READ) |
| 179 |
selector.register(keybd, EVENT_READ) |
| 180 |
|
| 181 |
while True: |
| 182 |
for key, mask in selector.select(): |
| 183 |
device = key.fileobj |
| 184 |
for event in device.read(): |
| 185 |
print(event) |
| 186 |
|
| 187 |
|
| 188 |
Reading events from multiple devices (using :mod:`asyncio`) |
| 189 |
=========================================================== |
| 190 |
|
| 191 |
Yet another possibility is the :mod:`asyncio` module from Python 3.4: |
| 192 |
|
| 193 |
:: |
| 194 |
|
| 195 |
import asyncio, evdev |
| 196 |
|
| 197 |
@asyncio.coroutine |
| 198 |
def print_events(device): |
| 199 |
while True: |
| 200 |
events = yield from device.async_read() |
| 201 |
for event in events: |
| 202 |
print(device.path, evdev.categorize(event), sep=": ") |
| 203 |
|
| 204 |
mouse = evdev.InputDevice("/dev/input/eventX") |
| 205 |
keybd = evdev.InputDevice("/dev/input/eventY") |
| 206 |
|
| 207 |
for device in mouse, keybd: |
| 208 |
asyncio.async(print_events(device)) |
| 209 |
|
| 210 |
loop = asyncio.get_event_loop() |
| 211 |
loop.run_forever() |
| 212 |
|
| 213 |
Since Python 3.5, the `async/await`_ syntax makes this even simpler: |
| 214 |
|
| 215 |
:: |
| 216 |
|
| 217 |
import asyncio, evdev |
| 218 |
|
| 219 |
mouse = evdev.InputDevice("/dev/input/event4") |
| 220 |
keybd = evdev.InputDevice("/dev/input/event5") |
| 221 |
|
| 222 |
async def print_events(device): |
| 223 |
async for event in device.async_read_loop(): |
| 224 |
print(device.path, evdev.categorize(event), sep=": ") |
| 225 |
|
| 226 |
for device in mouse, keybd: |
| 227 |
asyncio.ensure_future(print_events(device)) |
| 228 |
|
| 229 |
loop = asyncio.get_event_loop() |
| 230 |
loop.run_forever() |
| 231 |
|
| 232 |
|
| 233 |
Accessing evdev constants |
| 234 |
========================= |
| 235 |
|
| 236 |
:: |
| 237 |
|
| 238 |
>>> from evdev import ecodes |
| 239 |
|
| 240 |
>>> ecodes.KEY_A, ecodes.ecodes["KEY_A"] |
| 241 |
... (30, 30) |
| 242 |
>>> ecodes.KEY[30] |
| 243 |
... "KEY_A" |
| 244 |
>>> ecodes.bytype[ecodes.EV_KEY][30] |
| 245 |
... "KEY_A" |
| 246 |
>>> ecodes.KEY[152] # a single value may correspond to multiple codes |
| 247 |
... ["KEY_COFFEE", "KEY_SCREENLOCK"] |
| 248 |
|
| 249 |
|
| 250 |
Searching event codes by regex |
| 251 |
============================== |
| 252 |
|
| 253 |
:: |
| 254 |
|
| 255 |
>>> from evdev import util |
| 256 |
|
| 257 |
>>> res = util.find_ecodes_by_regex(r"(ABS|KEY)_BR(AKE|EAK)") |
| 258 |
>>> res |
| 259 |
... {1: [411], 3: [10]} |
| 260 |
>>> util.resolve_ecodes_dict(res) |
| 261 |
... {("EV_KEY", 1): [("KEY_BREAK", 411)], ("EV_ABS", 3): [("ABS_BRAKE", 10)]} |
| 262 |
|
| 263 |
|
| 264 |
Getting exclusive access to a device |
| 265 |
==================================== |
| 266 |
|
| 267 |
:: |
| 268 |
|
| 269 |
>>> dev.grab() # become the sole recipient of all incoming input events |
| 270 |
>>> dev.ungrab() |
| 271 |
|
| 272 |
This functionality is also available as a context manager. |
| 273 |
|
| 274 |
:: |
| 275 |
|
| 276 |
>>> with dev.grab_context(): |
| 277 |
... pass |
| 278 |
|
| 279 |
|
| 280 |
Associating classes with event types |
| 281 |
==================================== |
| 282 |
|
| 283 |
:: |
| 284 |
|
| 285 |
>>> from evdev import categorize, event_factory, ecodes |
| 286 |
|
| 287 |
>>> class SynEvent: |
| 288 |
... def __init__(self, event): |
| 289 |
... ... |
| 290 |
|
| 291 |
>>> event_factory[ecodes.EV_SYN] = SynEvent |
| 292 |
|
| 293 |
See :mod:`events <evdev.events.event_factory>` for more information. |
| 294 |
|
| 295 |
Injecting input |
| 296 |
=============== |
| 297 |
|
| 298 |
:: |
| 299 |
|
| 300 |
>>> from evdev import UInput, ecodes as e |
| 301 |
|
| 302 |
>>> ui = UInput() |
| 303 |
|
| 304 |
>>> # accepts only KEY_* events by default |
| 305 |
>>> ui.write(e.EV_KEY, e.KEY_A, 1) # KEY_A down |
| 306 |
>>> ui.write(e.EV_KEY, e.KEY_A, 0) # KEY_A up |
| 307 |
>>> ui.syn() |
| 308 |
|
| 309 |
>>> ui.close() |
| 310 |
|
| 311 |
|
| 312 |
Injecting events using a context manager |
| 313 |
======================================== |
| 314 |
|
| 315 |
:: |
| 316 |
|
| 317 |
>>> ev = InputEvent(1334414993, 274296, ecodes.EV_KEY, ecodes.KEY_A, 1) |
| 318 |
>>> with UInput() as ui: |
| 319 |
... ui.write_event(ev) |
| 320 |
... ui.syn() |
| 321 |
|
| 322 |
|
| 323 |
Specifying ``uinput`` device options |
| 324 |
==================================== |
| 325 |
|
| 326 |
.. note:: |
| 327 |
|
| 328 |
``ecodes.EV_SYN`` cannot be in the ``cap`` dictionary or the device will not be created. |
| 329 |
|
| 330 |
:: |
| 331 |
|
| 332 |
>>> from evdev import UInput, AbsInfo, ecodes as e |
| 333 |
|
| 334 |
>>> cap = { |
| 335 |
... e.EV_KEY : [e.KEY_A, e.KEY_B], |
| 336 |
... e.EV_ABS : [ |
| 337 |
... (e.ABS_X, AbsInfo(value=0, min=0, max=255, |
| 338 |
... fuzz=0, flat=0, resolution=0)), |
| 339 |
... (e.ABS_Y, AbsInfo(0, 0, 255, 0, 0, 0)), |
| 340 |
... (e.ABS_MT_POSITION_X, (0, 128, 255, 0)) ] |
| 341 |
... } |
| 342 |
|
| 343 |
>>> ui = UInput(cap, name="example-device", version=0x3) |
| 344 |
>>> print(ui) |
| 345 |
name "example-device", bus "BUS_USB", vendor "0001", product "0001", version "0003" |
| 346 |
event types: EV_KEY EV_ABS EV_SYN |
| 347 |
|
| 348 |
>>> print(ui.capabilities()) |
| 349 |
{0: [0, 1, 3], |
| 350 |
1: [30, 48], |
| 351 |
3: [(0, AbsInfo(value=0, min=0, max=0, fuzz=255, flat=0, resolution=0)), |
| 352 |
(1, AbsInfo(value=0, min=0, max=0, fuzz=255, flat=0, resolution=0)), |
| 353 |
(53, AbsInfo(value=0, min=0, max=255, fuzz=128, flat=0, resolution=0))]} |
| 354 |
|
| 355 |
>>> # move mouse cursor |
| 356 |
>>> ui.write(e.EV_ABS, e.ABS_X, 20) |
| 357 |
>>> ui.write(e.EV_ABS, e.ABS_Y, 20) |
| 358 |
>>> ui.syn() |
| 359 |
|
| 360 |
|
| 361 |
Creating an ``uinput`` device with capabilities of another device |
| 362 |
================================================================= |
| 363 |
|
| 364 |
:: |
| 365 |
|
| 366 |
>>> from evdev import UInput, InputDevice |
| 367 |
|
| 368 |
>>> mouse = InputDevice("/dev/input/event1") |
| 369 |
>>> keybd = "/dev/input/event2" |
| 370 |
|
| 371 |
>>> ui = UInput.from_device(mouse, keybd, name="keyboard-mouse-device") |
| 372 |
>>> ui.capabilities(verbose=True).keys() |
| 373 |
dict_keys([("EV_LED", 17), ("EV_KEY", 1), ("EV_SYN", 0), ("EV_REL", 2), ("EV_MSC", 4)]) |
| 374 |
|
| 375 |
|
| 376 |
.. _`async/await`: https://docs.python.org/3/library/asyncio-task.html |
| 377 |
|
| 378 |
|
| 379 |
Uploading FF-effects |
| 380 |
==================== |
| 381 |
|
| 382 |
:: |
| 383 |
|
| 384 |
from evdev import InputDevice, ff |
| 385 |
|
| 386 |
dev = InputDevice(...) |
| 387 |
|
| 388 |
rumble = ff.Rumble(strong_magnitude=0x0000, weak_magnitude=0xffff) |
| 389 |
duration_ms = 1000 |
| 390 |
|
| 391 |
effect = ff.Effect( |
| 392 |
type=ecodes.FF_RUMBLE, |
| 393 |
id=-1, |
| 394 |
direction=0, |
| 395 |
ff_trigger=ff.Trigger(button=0, interval=0), |
| 396 |
ff_replay=ff.Replay(length=duration_ms, delay=0), |
| 397 |
u=ff.EffectType(ff_rumble_effect=rumble), |
| 398 |
) |
| 399 |
|
| 400 |
# Since id=-1, `upload_effect()` will create a new effect and return its effect id. |
| 401 |
# By default, the effect id is also written back to the `effect.id` field. |
| 402 |
# This behaviour can be controlled by the `writeback_id` argument. |
| 403 |
eid = dev.upload_effect(effect) # mutates `effect` |
| 404 |
eid = dev.upload_effect(effect, writeback_id=False) # does not mutate `effect` |
| 405 |
|
| 406 |
|
| 407 |
Creating an ``uinput`` device capable of receiving FF-effects |
| 408 |
============================================================= |
| 409 |
|
| 410 |
:: |
| 411 |
|
| 412 |
import asyncio |
| 413 |
from evdev import UInput, categorize, ecodes |
| 414 |
|
| 415 |
cap = { |
| 416 |
ecodes.EV_FF: [ecodes.FF_RUMBLE ], |
| 417 |
ecodes.EV_KEY: [ecodes.KEY_A, ecodes.KEY_B] |
| 418 |
} |
| 419 |
|
| 420 |
ui = UInput(cap, name="test-controller", version=0x3) |
| 421 |
|
| 422 |
async def print_events(device): |
| 423 |
async for event in device.async_read_loop(): |
| 424 |
print(categorize(event)) |
| 425 |
|
| 426 |
# Wait for an EV_UINPUT event that will signal us that an |
| 427 |
# effect upload/erase operation is in progress. |
| 428 |
if event.type != ecodes.EV_UINPUT: |
| 429 |
continue |
| 430 |
|
| 431 |
if event.code == ecodes.UI_FF_UPLOAD: |
| 432 |
upload = device.begin_upload(event.value) |
| 433 |
upload.retval = 0 |
| 434 |
|
| 435 |
print(f"[upload] effect_id: {upload.effect.id}, type: {upload.effect.type}") |
| 436 |
device.end_upload(upload) |
| 437 |
|
| 438 |
elif event.code == ecodes.UI_FF_ERASE: |
| 439 |
erase = device.begin_erase(event.value) |
| 440 |
print(f"[erase] effect_id {erase.effect_id}") |
| 441 |
|
| 442 |
erase.retval = 0 |
| 443 |
device.end_erase(erase) |
| 444 |
|
| 445 |
asyncio.ensure_future(print_events(ui)) |
| 446 |
loop = asyncio.get_event_loop() |
| 447 |
loop.run_forever() |
| 448 |
|
| 449 |
|
| 450 |
Injecting an FF-event into the first FF-capable device found |
| 451 |
============================================================ |
| 452 |
|
| 453 |
:: |
| 454 |
|
| 455 |
from evdev import ecodes, InputDevice, ff, list_devices |
| 456 |
import time |
| 457 |
|
| 458 |
# Find the first EV_FF capable event device (that we have permissions to use). |
| 459 |
for name in list_devices(): |
| 460 |
dev = InputDevice(name) |
| 461 |
if ecodes.EV_FF in dev.capabilities(): |
| 462 |
break |
| 463 |
|
| 464 |
rumble = ff.Rumble(strong_magnitude=0x0000, weak_magnitude=0xffff) |
| 465 |
effect_type = ff.EffectType(ff_rumble_effect=rumble) |
| 466 |
duration_ms = 1000 |
| 467 |
|
| 468 |
effect = ff.Effect( |
| 469 |
ecodes.FF_RUMBLE, -1, 0, |
| 470 |
ff.Trigger(0, 0), |
| 471 |
ff.Replay(duration_ms, 0), |
| 472 |
effect_type |
| 473 |
) |
| 474 |
|
| 475 |
repeat_count = 1 |
| 476 |
effect_id = dev.upload_effect(effect) |
| 477 |
dev.write(ecodes.EV_FF, effect_id, repeat_count) |
| 478 |
time.sleep(duration_ms / 1000) |
| 479 |
dev.erase_effect(effect_id) |
| 480 |
|
| 481 |
|
| 482 |
Forwarding force-feedback from uinput to a real device |
| 483 |
====================================================== |
| 484 |
|
| 485 |
:: |
| 486 |
|
| 487 |
import evdev |
| 488 |
from evdev import ecodes as e |
| 489 |
|
| 490 |
# Find the first EV_FF capable event device (that we have permissions to use). |
| 491 |
for name in evdev.list_devices(): |
| 492 |
dev = evdev.InputDevice(name) |
| 493 |
if e.EV_FF in dev.capabilities(): |
| 494 |
break |
| 495 |
# To ensure forwarding works correctly it is important that `max_effects` |
| 496 |
# of the uinput device is <= `dev.ff_effects_count`. |
| 497 |
# `from_device()` will do this automatically, but in some situations you may |
| 498 |
# want to set the `max_effects` parameter manually, such as when using `Uinput()`. |
| 499 |
# `filtered_types` is specified as by default EV_FF events are filtered |
| 500 |
uinput = evdev.UInput.from_device(dev, filtered_types=[e.EV_SYN]) |
| 501 |
|
| 502 |
# Keeps track of which effects have been uploaded to the device |
| 503 |
effects = set() |
| 504 |
|
| 505 |
for event in uinput.read_loop(): |
| 506 |
# Handle the special uinput events |
| 507 |
if event.type == e.EV_UINPUT: |
| 508 |
|
| 509 |
if event.code == e.UI_FF_UPLOAD: |
| 510 |
upload = uinput.begin_upload(event.value) |
| 511 |
|
| 512 |
# Checks if this is a new effect |
| 513 |
if upload.effect.id not in effects: |
| 514 |
effects.add(upload.effect.id) |
| 515 |
# Setting id to -1 indicates that a new effect must be allocated |
| 516 |
upload.effect.id = -1 |
| 517 |
|
| 518 |
dev.upload_effect(upload.effect) |
| 519 |
upload.retval = 0 |
| 520 |
uinput.end_upload(upload) |
| 521 |
|
| 522 |
elif event.code == e.UI_FF_ERASE: |
| 523 |
erase = uinput.begin_erase(event.value) |
| 524 |
erase.retval = 0 |
| 525 |
dev.erase_effect(erase.effect_id) |
| 526 |
effects.remove(erase.effect_id) |
| 527 |
uinput.end_erase(erase) |
| 528 |
|
| 529 |
# Forward writes to actual rumble device. |
| 530 |
elif event.type == e.EV_FF: |
| 531 |
dev.write(event.type, event.code, event.value) |