linux - read raw input from keyboard in python -
i'm trying raw input of keyboard in python. have logitech gaming keyboard programmable keys, logitech doesn't provide drivers linux. thought (try) write own driver this. in think solution like:
with open('/dev/keyboard', 'rb') keyboard: while true: inp = keyboard.read() -do something-
english isn't native language. if find errors, please correct it.
two input methods rely on os handling keyboard
import sys line in sys.stdin.readlines(): print line
this 1 "simple" solution problem considering reads sys.stdin you'll need driver , if os strips stuff along way break anyways.
this solution (linux 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 = '' nonblockingconsole() nbc: while 1: c = nbc.get_data() if c: if c == '\x1b': # x1b 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 above work, woun't able keys within python.
you'll need actual driver can parse data sent keyboard not normal keyboard event on usb stack, meaning.. way low-level python , you're out of luck... unless know how build linux drivers.
anyway, have at: http://ubuntuforums.org/showthread.php?t=1490385
looks more people have tried it.
trying pyusb
http://pyusb.sourceforge.net/docs/1.0/tutorial.html
you try pyusb solution , fetch raw data usb socket, again.. if g-keys not registered "traditional" usb data might dropped , won't recieve it.
hooking on input pipes in linux
another untested method, might work //hackaday:
Comments
Post a Comment