gpt4 book ai didi

python -/dev/input/event 的格式*

转载 作者:IT老高 更新时间:2023-10-28 22:13:03 29 4
gpt4 key购买 nike

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

换句话说,如何解码字符流?非常感谢您提供 Python 示例。

最佳答案

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

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

关于python -/dev/input/event 的格式*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5060710/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com