libevdev Python wrapper

libevdev-python is a wrapper around the libevdev C library, with a pythonic API. libevdev makes it easy to

  • read and parse events from an input device
  • create a virtual input device and make it send events
  • duplicate an existing device and modify the event stream

See the Basic examples section below for simple code or the Examples page for more detailed examples.

Source code

The source code for this project is available at

Why libevdev?

libevdev-python uses libevdev for most operations. This provides a number of advantages over direct evdev event handling, libevdev hides some of the quirks of the evdev protocol. For example libevdev provides

  • access to the state of the device (rather than just the events)
  • correct handling of fake multitouch devices
  • synching of slots and per-slot state
  • transparent generation of missing tracking ids after SYN_DROPPED
  • disabling/enabling events on a per-context basis, so one can disable/enable ABS_FOO and then not care about quirks in the client-side code.
  • transparent handling of the UI_GET_SYSNAME and UI_DEV_SETUP ioctls

The above are all features that were added to libevdev (the C library) over time because of a need for it in projects like the Xorg drivers, libinput, evemu and others.

Unfortunately, the evdev kernel API is very simple, but getting the behavior of the API correct is hard. Even kernel drivers frequently do it wrong. libevdev (the C library) does hide a lot of that and thus makes consuming evdev safer.

For the precise behavior of the libevdev C library refer to the offical documentation at

Basic examples

Below are examples that cover the most common cases that need to be done with libevdev. More specific examples can be found on the Examples page.

To read events from an existing device:

import libevdev

fd = open('/dev/input/event0', 'rb')
d = libevdev.Device(fd)
if not d.has(libevdev.EV_KEY.BTN_LEFT):
     print('This does not look like a mouse device')

# Loop indefinitely while pulling the currently available events off
# the file descriptor
while True:
    for e in
        if not e.matches(libevdev.EV_KEY):

        if e.matches(libevdev.EV_KEY.BTN_LEFT):
            print('Left button event')
        elif e.matches(libevdev.EV_KEY.BTN_RIGHT):
            print('Right button event')


Reading from and writing to input devices requires root access to the device node. Any programs using libevdev need to run as root.

To create a new uinput device with a specific set of events:

import libevdev
d = libevdev.Device() = 'some test device'

uinput = d.create_uinput_device()
print('new uinput test device at {}'.format(uinput.devnode))
events = [InputEvent(libevdev.EV_REL.REL_X, 1),
          InputEvent(libevdev.EV_REL.REL_Y, 1),
          InputEvent(libevdev.EV_SYN.SYN_REPORT, 0)]


Creating uinput devices requires root access. Any programs using libevdev need to run as root.

It’s common to read events or device descriptions from some file (e.g. evemu recordings). libevdev makes it easy to convert numbers or strings into a correct event code:

>>> import libevdev
>>> print(libevdev.evbit(0))
>>> print(libevdev.evbit(2))
>>> print(libevdev.evbit(3, 4))
>>> print(libevdev.evbit('EV_ABS'))
>>> print(libevdev.evbit('EV_ABS', 'ABS_X'))
>>> print(libevdev.evbit('ABS_X'))

Indices and tables