Format of /dev/input/event*

A simple and raw reader can be just done using:

#!/usr/bin/python
import struct
import time
import sys

infile_path = "/dev/input/event" + (sys.argv[1] if len(sys.argv) > 1 else "0")

"""
FORMAT represents the format used by linux kernel input event struct
See https://github.com/torvalds/linux/blob/v5.5-rc5/include/uapi/linux/input.h#L28
Stands for: long int, long int, unsigned short, unsigned short, unsigned int
"""
FORMAT = 'llHHI'
EVENT_SIZE = struct.calcsize(FORMAT)

#open file in binary mode
in_file = open(infile_path, "rb")

event = in_file.read(EVENT_SIZE)

while event:
    (tv_sec, tv_usec, type, code, value) = struct.unpack(FORMAT, event)

    if type != 0 or code != 0 or value != 0:
        print("Event type %u, code %u, value %u at %d.%d" % \
            (type, code, value, tv_sec, tv_usec))
    else:
        # Events with code, type and value == 0 are "separator" events
        print("===========================================")

    event = in_file.read(EVENT_SIZE)

in_file.close()

The python-evdev package provides bindings to the event device interface. A short usage example would be:

from evdev import InputDevice
from select import select

dev = InputDevice('/dev/input/event1')

while True:
   r,w,x = select([dev], [], [])
   for event in dev.read():
       print(event)

# event at 1337427573.061822, code 01, type 02, val 01
# event at 1337427573.061846, code 00, type 00, val 00

Keep in mind that, unlike the very convenient, purely Pythonic modules mentioned so far, evdev contains C extensions. Building them requires having your Python development and kernel headers installed.


Right here in the Input.py module. You'll also need the event.py module.


The format is described in the Documentation/input/input.txt file in the Linux source. Basically, you read structs of the following form from the file:

struct input_event {
    struct timeval time;
    unsigned short type;
    unsigned short code;
    unsigned int value;
};

type and code are values defined in linux/input.h. For example, type might be EV_REL for relative moment of a mouse, or EV_KEY for a keypress, and code is the keycode, or REL_X or ABS_X for a mouse.