This package provides bindings to the generic input event interface in
Linux. The evdev interface serves the purpose of passing events
generated in the kernel directly to userspace through character
devices that are typically located in
This package also comes with bindings to uinput, the userspace input subsystem. Uinput allows userspace programs to create and handle input devices that can inject events directly into the input subsystem.
In other words, python-evdev allows you to read and write input events on Linux. An event can be a key or button press, a mouse movement or a tap on a touchscreen.
The following GNU/Linux distributions have python-evdev in their package repositories:
The latest stable version of python-evdev can be installed from pypi, provided that you have gcc/clang, pip and the Python and Linux development headers installed on your system. Installing them is distribution specific and typically falls in one of the following categories:
On a Debian compatible OS:
$ apt-get install python-dev python-pip gcc $ apt-get install linux-headers-$(uname -r)
On a Redhat compatible OS:
$ yum install python-devel python-pip gcc $ yum install kernel-headers-$(uname -r)
On Arch Linux and derivatives:
$ pacman -S core/linux-api-headers python-pip gcc
Installing python-evdev with pip:
$ sudo pip install evdev
Listing accessible event devices:¶
>>> import evdev >>> devices = [evdev.InputDevice(fn) for fn in evdev.list_devices()] >>> for device in devices: ... print(device.fn, device.name, device.phys) /dev/input/event1 Dell Dell USB Keyboard usb-0000:00:12.1-2/input0 /dev/input/event0 Dell USB Optical Mouse usb-0000:00:12.0-2/input0
Reading events from a device:¶
>>> import evdev >>> device = evdev.InputDevice('/dev/input/event1') >>> print(device) device /dev/input/event1, name "Dell Dell USB Keyboard", phys "usb-0000:00:12.1-2/input0" >>> for event in device.read_loop(): ... if event.type == evdev.ecodes.EV_KEY: ... print(categorize(event)) ... # pressing 'a' and holding 'space' key event at 1337016188.396030, 30 (KEY_A), down key event at 1337016188.492033, 30 (KEY_A), up key event at 1337016189.772129, 57 (KEY_SPACE), down key event at 1337016190.275396, 57 (KEY_SPACE), hold key event at 1337016190.284160, 57 (KEY_SPACE), up
Reading events using async/await:¶
Python-evdev proudly supports the new async/await syntax in Python 3.5:
import asyncio, evdev async def print_events(device): async for event in device.async_read_loop(): print(device.fn, evdev.categorize(event), sep=': ') device = evdev.InputDevice('/dev/input/event4') asyncio.ensure_future(print_events(device)) loop = asyncio.get_event_loop() loop.run_forever()
Accessing evdev constants:¶
>>> from evdev import ecodes >>> ecodes.KEY_A, ecodes.ecodes['KEY_A'] ... (30, 30) >>> ecodes.KEY ... 'KEY_A' >>> ecodes.bytype[ecodes.EV_KEY] ... 'KEY_A' >>> ecodes.KEY # a single value may correspond to multiple codes ... ['KEY_COFFEE', 'KEY_SCREENLOCK']
Listing and monitoring input devices:¶
The python-evdev package also comes with a small command-line program for listing and monitoring input devices:
$ python -m evdev.evtest
Jun 16, 2015:Version 0.5.0 released.
Oct 07, 2014:Version 0.4.6 released.
Jul 06, 2014:Version 0.4.5 released.
Jun 04, 2014:Version 0.4.4 released.
Dec 19, 2013:Version 0.4.3 released.
Dec 13, 2013:Version 0.4.2 released.
Oct 17, 2013:Python-evdev is now available in Ubuntu Saucy.
Oct 11, 2013:Python-evdev is now available in Arch Linux.
Jul 24, 2013:Version 0.4.1 released.
Jul 01, 2013:Version 0.4.0 released.
May 29, 2013:Version 0.3.3 released.
Apr 05, 2013:Version 0.3.2 released.
Nov 23, 2012:Version 0.3.1 released.
Nov 06, 2012:Version 0.3.0 released.
Aug 22, 2012:Version 0.2.0 released.
May 18, 2012:Version 0.1.1 released.
May 17, 2012:Version 0.1.0 released.
Please refer to the changelog for a full list of changes.