Android 使用python统计getevent按键
1、连接ADB
2、开启脚本
作用:
统计时间内相应的event数量
python代码:
import subprocess
import redef parse_getevent_output():# 启动getevent进程getevent_process = subprocess.Popen(['adb', 'shell', 'getevent', '-t', '-l'], stdout=subprocess.PIPE,stderr=subprocess.PIPE, text=True)event_count = 0 # 事件计数器start_time = 0 # 记录开始时间last_timestamp = 0 # 用于存储上一个事件的时间戳targetnum = 110try:while True:line = getevent_process.stdout.readline()if not line:break # 如果没有输出了,跳出循环# 使用正则表达式匹配事件的时间戳、类型、代码和值match = re.match( r'\[(\d+\.\d+)\] (\S+): (\w+) +(\w+) +([0-9A-Fa-f]+)', re.sub(r'\[\s+', '[', line).strip())if match:timestamp = match.group(1)device_path = match.group(2)event_type = match.group(3)event_code = match.group(4)event_value = match.group(5)# print(f"Timestamp: {timestamp}")# print(f"Device Path: {device_path}")# print(f"Event Type: {event_type}")# print(f"Event Code: {event_code}")# print(f"Event Value: {event_value}")if start_time == 0 :start_time = float(timestamp)# 检查是否已经过了一秒钟if float(timestamp) - start_time >= 1.0:# 打印一秒钟内的事件数量print(f"your printl data")event_count = 0 # 重置事件计数器start_time = float(timestamp) # 重置开始时间if event_code == "SYN_REPORT" :event_count += 1 # 增加事件计数# else:# print(f"{line.strip()}")except KeyboardInterrupt:print("\nScript terminated by user.") # 用户中断脚本finally:getevent_process.terminate() # 确保进程被终止print("Getevent process terminated.")if __name__ == "__main__":parse_getevent_output()