开始想用python做一个键盘记录的工具,搜索linux keyboard hook,发现找不到东西。后来发现原来linux直接读设备文件就行了,才恍然大悟。 /dev/input/下有十多个event。要找到键盘对应的event,查看grep keyboard /proc/bus/input/devices -A 12。 然后找到一点资料,自己写了一段程序:
# coding: utf-8
import os
import struct

fmt = "llHHI"
size = struct.calcsize(fmt)
filename = "/dev/input/event3"

fd = os.open(filename, os.O_RDWR)
while 1:
    op = os.read(fd, size)
    timeval, suseconds, typ, code, value = struct.unpack(fmt, op)
    print typ, code, value
最后又找到两个库,用来读或模拟键盘按键的(或别的设备)。python-evdev和python-uinput。

上一篇:
下一篇:

相关文章:

Categories: 博客记录

0 Responses so far.

Leave a Reply