Linux /dev/input/event* 的格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5060710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:56:28  来源:igfitidea点击:

Format of /dev/input/event*

pythonlinuxdevice

提问by jldupont

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

位于的字符设备的“格式”是/dev/input/event*什么?

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

换句话说,我如何解码字符流?一个 Python 示例将不胜感激。

采纳答案by Keith

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

就在Input.py模块中。您还需要event.py模块。

回答by Jeremiah Willcock

The data is in the form of input_eventstructures; 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 ioctlcalls to get information on the device before reading from it.

数据采用input_event结构形式;有关 C 示例,请参见http://www.thelinuxdaily.com/2010/05/grab-raw-keyboard-input-from-event-device-node-devinputevent/。结构体定义位于(例如)http://www.cs.fsu.edu/~baker/devices/lxr/http/source/linux/include/linux/input.h?v=2.6.11.8。请注意,ioctl在读取设备信息之前,您需要使用一堆调用来获取设备上的信息。

回答by nelhage

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

该格式Documentation/input/input.txt在 Linux 源文件中的文件中有所描述。基本上,您从文件中读取以下形式的结构:

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

typeand codeare values defined in linux/input.h. For example, type might be EV_RELfor relative moment of a mouse, or EV_KEYfor a keypress, and codeis the keycode, or REL_Xor ABS_Xfor a mouse.

typecode中定义的值linux/input.h。例如,类型可能是EV_REL相对时刻鼠标,或EV_KEY用于一个按键,并且code是的键码,或者REL_XABS_X为鼠标。

回答by gvalkov

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

蟒-了evdev包提供绑定到事件设备接口。一个简短的使用示例是:

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, evdevcontains C extensions. Building them requires having your Python development and kernel headers installed.

请记住,与目前提到的非常方便的纯 Pythonic 模块不同,evdev包含 C 扩展。构建它们需要安装 Python 开发和内核头文件。

回答by Trevi?o

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()