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)
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_X也ABS_X可以是鼠标.
的蟒-了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开发和内核头文件.
数据采用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在读取设备之前,您需要使用一堆调用来获取设备上的信息.