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.
evdev (short for 'event device') is a generic input event interface in the Linux kernel and FreeBSD.
You can use blocking and nonblocking reads, also select() on the /dev/input/eventX devices, and you'll always get a whole number of input events on a read. Their layout is: struct input_event { struct timeval time; unsigned short type; unsigned short code; unsigned int value; };
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.
The input event fires when the value of an <input>, <select>, or <textarea> element has been changed. The event also applies to elements with contenteditable enabled, and to any element when designMode is turned on. In the case of contenteditable and designMode, the event target is the editing host.
The input event fires when the value of an <input>, <select>, or <textarea> element has been changed. The event also applies to elements with contenteditable enabled, and to any element when designMode is turned on.
Note: The input event is fired every time the value of the element changes. This is unlike the change event, which only fires when the value is committed, such as by pressing the enter key, selecting a value from a list of options, and the like.
The input protocol uses a map of types and codes to express input device values to userspace. This document describes the types and codes and how and when they may be used. A single hardware event generates multiple input events. Each input event contains the new value of a single data item.
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 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.
Right here in the Input.py module. You'll also need the event.py module.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With