Skip to content

Latest commit

 

History

History
215 lines (148 loc) · 6.43 KB

index.rst

File metadata and controls

215 lines (148 loc) · 6.43 KB

Synopsis

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 /dev/input/.

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.

Quick Start

Installing:

The following GNU/Linux distributions have python-evdev in their 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 usually falls in one of these 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-headers python-pip gcc

Installing python-evdev with pip:

$ sudo pip install evdev

Listing accessible event devices:

>>> from evdev import InputDevice, list_devices

>>> devices = [InputDevice(fn) for fn in list_devices()]
>>> for dev in devices:
...    print(dev.fn, dev.name, dev.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:

>>> from evdev import InputDevice, categorize, ecodes

>>> dev = InputDevice('/dev/input/event1')
>>> print(dev)
device /dev/input/event1, name "Dell Dell USB Keyboard", phys "usb-0000:00:12.1-2/input0"

>>> for event in dev.read_loop():
...     if event.type == 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

Accessing evdev constants:

>>> from evdev import ecodes

>>> ecodes.KEY_A, ecodes.ecodes['KEY_A']
... (30, 30)
>>> ecodes.KEY[30]
... 'KEY_A'
>>> ecodes.bytype[ecodes.EV_KEY][30]
... 'KEY_A'
>>> ecodes.KEY[152]  # a single value may correspond to multiple codes
... ['KEY_COFFEE', 'KEY_SCREENLOCK']

Further information:

  • Read the full tutorial <tutorial>.
  • See the example programs.
  • Refer to the API documentation <apidoc>:
    • device <evdev.device>
      • AbsInfo <evdev.device.AbsInfo>
      • KbdInfo <evdev.device.KbdInfo>
      • InputDevice <evdev.device.InputDevice>
    • events <evdev.events>
      • InputEvent <evdev.events.InputEvent>
      • KeyEvent <evdev.events.KeyEvent>
      • RelEvent <evdev.events.RelEvent>
      • AbsEvent <evdev.events.AbsEvent>
      • SynEvent <evdev.events.SynEvent>
    • util <evdev.util>
      • list_devices() <evdev.util.list_devices>
      • is_device() <evdev.util.is_device>
      • categorize() <evdev.util.categorize>
      • categorize() <evdev.util.categorize>
    • uinput <evdev.uinput>
      • UInput <evdev.uinput.UInput>
    • ecodes <evdev.ecodes>

News

See changelog <changelog> for a full list of changes.

License

The evdev package is released under the terms of the Revised BSD License.