Android 使用python统计getevent按键

1、连接ADB
2、开启脚本

作用:
统计时间内相应的event数量

python代码:

import subprocess
import re

def 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 = 110

    try:
        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()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值