Reading Keyboard Scan Codes

Alan Gauld alan.gauld at btinternet.com
Thu Jul 24 18:11:34 EDT 2003


On 23 Jul 2003 12:29:06 -0700, michaelb_spam_this at yahoo.com
(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




More information about the Python-list mailing list