/ dev / input / event *的格式?

什么是位于/dev/input/event*中的字符设备的“格式”? 换句话说,我怎样才能解码字符stream? 一个python的例子将不胜感激。

我一直在疯狂Googlesearch无济于事…请帮助。

在这里Input.py模块。 你还需要event.py模块。

一个简单和原始的阅读器可以完成使用:

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

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

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

typecode是在linux/input.h定义的值。 例如,对于鼠标的相对时刻,types可能是EV_REL ,对于按键,types可能是EV_RELcode是鼠标的键码或REL_XABS_X

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扩展。 构build它们需要安装你的python开发和内核头文件。

数据是以input_event结构的forms存在的; 有关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调用来获取设备上的信息。