Read original keyboard input in python

I am trying to get the original input of my keyboard in python. I have a Logitech gaming keyboard with soft keys, but Logitech does not provide drivers for Linux. So I thought I could (try) write my own driver for this. I think the solution could be something like this:

with open('/dev/keyboard', 'rb') as keyboard: while True: inp = keyboard.read() -do something- 

English is not my native language. If you find errors, correct them.

+4
source share
2 answers


Two input methods that rely on keyboard processing OS

 import sys for line in sys.stdin.readlines(): print line 

This is one โ€œsimpleโ€ solution to your problem, given that it reads the sys.stdin file, you will probably need a driver, and if the OS blocks the material along the way, it will probably break anyway.

This is another solution (linux afaik only):

 import sys, select, tty, termios class NonBlockingConsole(object): def __enter__(self): self.old_settings = termios.tcgetattr(sys.stdin) tty.setcbreak(sys.stdin.fileno()) return self def __exit__(self, type, value, traceback): termios.tcsetattr(sys.stdin, termios.TCSADRAIN, self.old_settings) def get_data(self): try: if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []): return sys.stdin.read(1) except: return '[CTRL-C]' return False data = '' printed = '' last = '' with NonBlockingConsole() as nbc: while 1: c = nbc.get_data() if c: if c == '\x1b': # x1b is ESC break elif c == '\x7f': # backspace data = data[:-1] printed = data[:-1] last = '' sys.stdout.write('\b') elif c == '[CTRL-C]': data = '' last = '' sys.stdout.write('\n') elif c == '\n': # it RETURN sys.stdout.write('\n') # parse data here data = '' else: data += (c) last = c sys.stdout.write(c) 

Is there a problem with the driver?

If none of the above work, you will not be able to get the keys in Python.
Most likely, you will need an actual driver that can analyze data sent from the keyboard, which is not a normal keyboard event in the USB stack, that is ... This is a low-level control method for Python, and you're out of luck ... if you don't know how to create linux drivers.

Anyway, take a look: http://ubuntuforums.org/showthread.php?t=1490385

It seems like more people were trying to do something.

PyUSB Attempt

http://pyusb.sourceforge.net/docs/1.0/tutorial.html

You can try the PyUSB solution and extract the raw data from the USB jack, but again. If the G-keys are not registered as โ€œtraditionalโ€ USB data, they can be reset and you will not receive it.

Connecting to Input Channels on Linux

Another untested method, but may work // Hackaday: enter image description here

+2
source

Logitech does not provide drivers for Linux. So I thought that I could (try) to write my own driver for this.

Linux drivers are written in C; it is very low level code and works in kernel space.

0
source

Source: https://habr.com/ru/post/1480750/


All Articles