5

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.

Kritzefitz
  • 2,644
  • 1
  • 20
  • 35
  • 1
    What's the question, exactly? Does your code work, and if not, what problems have you encountered? – Aya May 14 '13 at 15:50
  • There isn't even a file /dev/keyboard so my code can't work. – Kritzefitz May 14 '13 at 15:53
  • Does pressing a button on your Logitech keyboard currently do anything in a Linux terminal window? – Aya May 14 '13 at 15:57
  • @IchUndNichtDu There, i've listed all your options that you can possibly do without going in to C code, which.. would be more neat on it's own but yea your question was Python so i listed them all.. – Torxed May 14 '13 at 16:10

2 Answers2

3


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

Torxed
  • 22,866
  • 14
  • 82
  • 131
  • If i press these keys (called G-keys) they don't end up in sys.stdin. – Kritzefitz May 14 '13 at 15:57
  • If none of my solutions work, you woun't get the input from doing a Python program because it heavely relies on the operating system handing you the data that you're looking for. You'll need to implement a driver that can correctly handle the USB events sent from the keyboard to the OS.. this is not something Python can do. – Torxed May 14 '13 at 15:59
  • If the hardware is supported but the keys just doesn't work try fiddling around with `xorg.conf` or whatever hardware profiler you're using. – Torxed May 14 '13 at 16:01
  • I tried your first and your third answer. Neither worked. The first showed that the G-keys still doesn't send something. Thanks anyway. – Kritzefitz May 14 '13 at 16:24
  • And the third option `pyusb`? Nothing? if not try the last option. Either that or again, you're screwed sorry to say :) – Torxed May 14 '13 at 16:25
  • Yeah sorry should try it. But i don't have time right now. I will test it later and inform wether it worked. – Kritzefitz May 14 '13 at 16:28
0

Logitech doesn't provide drivers for Linux. So i thought i could (try) to write my own driver for this.

Linux drivers are written in C; it's very low-level code and runs in kernel space.

xuanji
  • 5,007
  • 2
  • 26
  • 35