【问题标题】:binding key event in python using ctypes function使用 ctypes 函数在 python 中绑定键事件
【发布时间】:2018-05-04 21:05:54
【问题描述】:

我一直在尝试使用 python 将我的自定义事件绑定到具有特定事件代码编号的键盘事件,如下所示

ctypes.windll.user32.keybd_event('0x24',0,2,0)

但你已经知道了

windll

该库仅适用于 Windows 操作系统。 我怎样才能在 Linux 机器上做这样的事情? 我读到了

CDLL('libc.so.6')

但我不知道这个库是否有用?

是否有另一种方法可以使用虚拟键代码在 OS 级别使用 python 设置按键侦听器?

【问题讨论】:

  • 你想达到什么目的?
  • 我正在尝试设置我自己的事件侦听器,只使用关键代码号。如下所示:keyboard_listener('0x24',custom_event_listener())
  • 有一个现有的解决方案 pykeylogger。

标签: python linux keyboard


【解决方案1】:

Linux输入子系统由三部分组成:驱动层、输入子系统核心层和事件处理层。 并且键盘或其他输入事件都由input_event描述。

使用下面的代码并在您的终端输入python filename.py | grep "keyboard"

#!/usr/bin/env python
#coding: utf-8
import os

deviceFilePath = '/sys/class/input/'

def showDevice():
    os.chdir(deviceFilePath)
    for i in os.listdir(os.getcwd()):
        namePath = deviceFilePath + i + '/device/name'
        if os.path.isfile(namePath):
            print "Name: %s Device: %s" % (i, file(namePath).read())

if __name__ == '__main__':
    showDevice()

你应该得到Name: event1 Device: AT Translated Set 2 keyboard。 然后使用

#!/usr/bin/env python
#coding: utf-8
from evdev import InputDevice
from select import select

def detectInputKey():
    dev = InputDevice('/dev/input/event1')

    while True:
        select([dev], [], [])
        for event in dev.read():
            print "code:%s value:%s" % (event.code, event.value)


if __name__ == '__main__':
    detectInputKey()

evdev 是一个包,提供与 Linux 中的通用输入事件接口的绑定。 evdev 接口的目的是将内核中生成的事件通过字符设备直接传递到用户空间,这些字符设备通常位于 /dev/input/.andselectselect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-28
    相关资源
    最近更新 更多