/ dev/input/event*的格式?

jld*_*ont 27 python linux device

字符设备的"格式"是/dev/input/event*什么?换句话说,我该如何解码字符流?一个python的例子将非常感激.

我一直在谷歌搜索疯狂无济于事...请帮忙.

Tre*_*iño 37

一个简单的原始读者可以使用以下方法完成:

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

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

#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()
Run Code Online (Sandbox Code Playgroud)

  • FORMAT行上方的注释描述了它的含义.我认为长度将取决于系统架构,但在我的Raspberry Pi上,long int是4个字节,unsigned short是2个字节,unsigned int是4个字节.这将生成一个16个字符的十六进制字符串,最后4个是值.另外值得注意的是,https://github.com/torvalds/linux/blob/master/include/uapi/linux/input.h详细介绍了不同的事件类型和值. (2认同)

nel*_*age 22

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

struct input_event {
    struct timeval time;
    unsigned short type;
    unsigned short code;
    unsigned int value;
};
Run Code Online (Sandbox Code Playgroud)

type并且code是在中定义的值linux/input.h.例如,类型可以是EV_REL鼠标的相对时刻,也EV_KEY可以是按键,code可以是键码,REL_XABS_X可以是鼠标.


Kei*_*ith 11

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

  • @larsks我也只是指出它,因为它体现了这个界面的一些难点,如果你想这样做,可能会更容易"滚动你自己". (2认同)

gva*_*kov 9

蟒-了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
Run Code Online (Sandbox Code Playgroud)

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


Jer*_*ock 6

数据采用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在读取设备之前,您需要使用一堆调用来获取设备上的信息.