Reading Keyboard Scan Codes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Bendzick

    Reading Keyboard Scan Codes

    Is there a simple way in python to read a keyboard scan code? I'm
    working on a shell script that interfaces with a proprietary keyboard
    device (extra buttons) and need to be able to distinguish between
    those keys.

    Thank you
  • François Pinard

    #2
    Re: Reading Keyboard Scan Codes

    [Michael Bendzick]
    [color=blue]
    > Is there a simple way in python to read a keyboard scan code? I'm working
    > on a shell script that interfaces with a proprietary keyboard device
    > (extra buttons) and need to be able to distinguish between those keys.[/color]

    Within Linux in console mode, you merely do:

    os.system('kbd_ mode -k')

    to read key codes, which might be what you want, or:

    os.system('kbd_ mode -s')

    to really read raw scan codes. To get back on your feet, do:

    os.system('kbd_ mode -a')

    Beware that you can easily burn yourself if your program fails to restore
    normal mode, as per last example. Use try/finally! You should probably use
    the `curses' module for preparing to read codes one byte at a time.[1]

    However, I do not know if reading scan codes or key codes is easily done
    within X, nor on MS-Windows. I would like finding or discovering recipes in
    this area. So, please share your tricks in this area, if any! :-)

    --------------------
    [1] I was given two different C programs written originally for QNX and its
    own special `term' library, to be quickly ported to Linux, without rewriting
    them if possible. To achieve this, I wrote a compatibility module in C, and
    a more bulky keyboard driver module in Python, meant for key code assembly
    into curses key events, and for any special keyboard processing. The link
    between all parts was made using Pyrex, and a bit of Makefile trickery...

    --
    François Pinard http://www.iro.umontreal.ca/~pinard

    Comment

    • Alan Gauld

      #3
      Re: Reading Keyboard Scan Codes

      On 23 Jul 2003 12:29:06 -0700, michaelb_spam_t [email protected]
      (Michael Bendzick) wrote:
      [color=blue]
      > Is there a simple way in python to read a keyboard scan code? I'm
      > working on a shell script that interfaces with a proprietary keyboard
      > device (extra buttons) and need to be able to distinguish between
      > those keys.[/color]

      The code to do this is on my programming tutor under Event Driven
      Programming in the Advanced topics section. It uses Tkinter to
      read and print the codes.

      The critical part is this bit:

      self.txtBox.bin d("<Key>", self.doKeyEvent )


      def doKeyEvent(self ,event):
      str = "%d\n" % event.keycode
      self.txtBox.ins ert(END, str)
      return "break"

      self.txtBox is a standard Text widget in Tkinter.

      If you can find my book you'll see the code to do it using
      msvcrt. :-)

      Again the critical bit is:

      key = mscvrt.getch()
      if key == '\000' or key == '\xe0' #detect special keys
      key = msvcrt.getch() # read the second byte.

      HTH,

      Alan G.
      Author of the Learn to Program website

      Comment

      Working...