/dev/input/event* 的格式

发布于 2024-10-18 10:52:40 字数 99 浏览 2 评论 0原文

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

蓬勃野心 2024-10-25 10:52:40

一个简单而原始的阅读器可以使用以下命令完成:

#!/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()

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()
养猫人 2024-10-25 10:52:40

格式在 Documentation/input/input.txt 文件。基本上,您从文件中读取以下形式的结构:

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

typecodelinux/input.h 中定义的值。例如,
类型可能是 EV_REL 表示鼠标的相对力矩,或 EV_KEY 表示
按键,code 是按键代码,或者 REL_XABS_X 表示按键
老鼠。

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.

树深时见影 2024-10-25 10:52:40

就在 Input.py 模块。您还需要 event.py< /a> 模块。

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

梦回梦里 2024-10-25 10:52:40

python-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

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

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.

时间你老了 2024-10-25 10:52:40

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文