33

What is the "format" of the character devices located in /dev/input/event*?

In other words, how can I decode the character stream? A Python example would be greatly appreciated.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
jldupont
  • 82,560
  • 49
  • 190
  • 305

5 Answers5

43

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()
Treviño
  • 1,907
  • 2
  • 20
  • 18
  • Any idea what the FORMAT means? When I convert an event to HEX it's a long string. Is there anyway to know what byte is the value? – etw3 Sep 15 '15 at 03:21
  • 2
    The comment above the FORMAT line describes what it means. I think the length will depend on the system architecture, but on my Raspberry Pi, long int is 4 bytes, unsigned short is 2 bytes, and unsigned int is 4 bytes. This would make a hex string of 16 characters, the last 4 being the value. Also of note, https://github.com/torvalds/linux/blob/master/include/uapi/linux/input.h details the different event types and values. – Oliver Sep 16 '15 at 00:31
  • @etw3 The reply given by Oliver is correct, however I've updated the code to make it clearer. – Treviño Jan 07 '20 at 12:11
25

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.

nelhage
  • 2,554
  • 17
  • 14
11

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Keith
  • 37,985
  • 10
  • 48
  • 67
  • 1
    These two modules can't be use standalone...they have other dependencies upon the pycopia stuff. – larsks Oct 28 '14 at 00:50
  • Nope, but wasn't clear from the answer, I thought -- because the links go directly to the module, rather than to the package itself. – larsks Oct 30 '14 at 02:48
  • 2
    @larsks I also just pointed to it because it embodies some of the hard parts of this interface that might make it easier to "roll your own" if you wanted to do that. – Keith Oct 31 '14 at 08:40
9

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
gvalkov
  • 3,499
  • 24
  • 25
7

The data is in the form of input_event structures; see http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/ for a C example. The struct definition is at (for example) http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/input.h?v=2.6.11.8. Note that you will need to use a bunch of ioctl calls to get information on the device before reading from it.

Jeremiah Willcock
  • 27,461
  • 5
  • 68
  • 75