Linux监听事件

本文介绍了一种使用Python和evdev库从USB键盘实时读取按键事件的方法。通过示例代码展示了如何检测并打印出键盘事件的详细信息,包括按键代码和值,适用于Linux环境下对键盘输入进行监控的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

键盘

https://blog.youkuaiyun.com/kcp606/article/details/79859024
https://python-evdev.readthedocs.io/en/latest/tutorial.html
https://www.jianshu.com/p/5f12b0e9a9e5

我的笔记本键盘: /dev/input/event11
我的USB键盘: /dev/input/event10

#!/home/tqc/anaconda3/envs/tf/bin/python
#coding: utf-8
from evdev import InputDevice
from select import select

def detectInputKey():
    dev = InputDevice('/dev/input/event10')
    print(dev)
    print(dev.capabilities(verbose=True))
    while True:
        print('\n')
        select([dev], [], [])
        for event in dev.read():
            print("code:%s value:%s" % (event.code, event.value))


if __name__ == '__main__':
    detectInputKey()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值