Python key binding event using ctypes function

I am trying to use python to bind a customize event to a keyboard with a specific event code code, as shown below

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

but as you already know

windll

the library only worked on Windows. how can i do something like this on linux machines? I read about

CDLL ('libc.so.6')

but I can’t understand if this library is useful or not?

is there any other way to set OS-level keystroke listener using python using virtual key code?

+6
source share
1 answer

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/.and select is select.

+4

All Articles