Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read raw input from keyboard in python

I'm trying to get the raw input of my keyboard in python. I have a Logitech gaming keyboard with programmable keys, but Logitech doesn't provide drivers for Linux. So i thought i could (try) to write my own driver for this. In think the solution could be something like:

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

English isn't my native language. If you find errors, please correct it.

like image 409
Kritzefitz Avatar asked Nov 03 '22 22:11

Kritzefitz


1 Answers


Two input methods that rely on the OS handling the keyboard

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

This is one "simple" solution to your problem considering it reads sys.stdin you'll probably need a driver and if the OS strips stuff along the way it will probably break anyways.

This is another solution (linux only afaik):

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's RETURN
                sys.stdout.write('\n')
                # parse data here
                data = ''
            else:
                data += (c)
                last = c
                sys.stdout.write(c)

Driver issue?

If none of the above work, you woun't be able to get the keys within Python.
Most likely you'll need an actual driver that can parse the data sent from the keyboard that is not a normal keyboard event on the USB stack, meaning.. This is way to low-level for Python and you're out of luck... unless you know how to build linux drivers.

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

Looks like more people have tried to do something about it.

Trying PyUSB

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

You could try a PyUSB solution and fetch raw data from the USB socket, but again.. if the G-keys are not registered as "traditional" USB data it might get dropped and you won't recieve it.

Hooking on to the input pipes in Linux

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

like image 133
Torxed Avatar answered Nov 08 '22 04:11

Torxed