Reading Keyboard Scan Codes

Discussion in 'Python' started by Michael Bendzick, Jul 23, 2003.

  1. 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
     
    Michael Bendzick, Jul 23, 2003
    #1
    1. Advertising

  2. [Michael Bendzick]

    > 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.


    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
     
    =?iso-8859-1?q?Fran=E7ois_Pinard?=, Jul 23, 2003
    #2
    1. Advertising

  3. Michael Bendzick

    Alan Gauld Guest

    On 23 Jul 2003 12:29:06 -0700,
    (Michael Bendzick) wrote:

    > 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.


    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.bind("<Key>", self.doKeyEvent)


    def doKeyEvent(self,event):
    str = "%d\n" % event.keycode
    self.txtBox.insert(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
    http://www.freenetpages.co.uk/hp/alan.gauld
     
    Alan Gauld, Jul 24, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Rapha=EBl?=

    How to scan Keyboard in Perl or else on Linux ?

    =?ISO-8859-1?Q?Rapha=EBl?=, Nov 4, 2004, in forum: Perl
    Replies:
    2
    Views:
    908
    =?ISO-8859-1?Q?Rapha=EBl?=
    Nov 9, 2004
  2. aman

    scan codes

    aman, Mar 1, 2004, in forum: C Programming
    Replies:
    3
    Views:
    2,396
    E. Robert Tisdale
    Mar 1, 2004
  3. Bjorn Pettersen

    RE: Reading Keyboard Scan Codes

    Bjorn Pettersen, Jul 23, 2003, in forum: Python
    Replies:
    0
    Views:
    553
    Bjorn Pettersen
    Jul 23, 2003
  4. Francois Pinard

    Re: Reading Keyboard Scan Codes

    Francois Pinard, Jul 24, 2003, in forum: Python
    Replies:
    0
    Views:
    530
    Francois Pinard
    Jul 24, 2003
  5. Replies:
    2
    Views:
    2,995
    Malcolm
    Aug 20, 2005
Loading...

Share This Page