5

I have been trying to use python to bind my customize event to keyboard event with specific event code number like below

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

but as you already know

windll

the library only worked on Windows OS. how can I do something like this in Linux machines? I read about

CDLL('libc.so.6')

but I can't figure it out if this library is helpful or not?

is there another way to set keypress listener in OS level with python using the virtual key code?

3
  • what are you trying to achieve as such? Commented Nov 25, 2017 at 8:52
  • i'm try to set my own event listener using only key code number .some thing like below : keyboard_listener('0x24',custom_event_listener()) Commented Nov 25, 2017 at 10:39
  • there is an existing solution pykeylogger. Commented Nov 26, 2017 at 19:08

1 Answer 1

5
+50

Linux input subsystem is composed of three parts: the driver layer, the input subsystem core layer and the event processing layer. and the keyboard or other input event is all describe by input_event.

use below code and type in your Terminal 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()

you should get Name: event1 Device: AT Translated Set 2 keyboard. then use

#!/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 is a package provides bindings to the generic input event interface in Linux. The evdev interface serves the purpose of passing events generated in the kernel directly to userspace through character devices that are typically located in /dev/input/.andselect is select.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.