473,378 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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
Jul 18 '05 #1
2 16366
[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

Jul 18 '05 #2
On 23 Jul 2003 12:29:06 -0700, mi****************@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.


Alan G.
Author of the Learn to Program website
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Jay | last post by:
I have a web page that waits for a users to enter a number and press ENTER simulating the click on the submit button. The problem is when the page reloads the cursor is not in the textfield on the...
by: qmudassir | last post by:
I want to capture keystrokes when the focus isn't on my program....any idea... thanks in advance -- Posted via http://dbforums.com
by: chewie | last post by:
Does anyone know how to capture keyboard scan codes using C#? Thanks
by: aman | last post by:
i know how to get scan codes and ascii codes for special keys. i can get ascii codes of alphabetic keys. how does one get scan codes of alphabetic keys??
by: gj_williams2000 | last post by:
Not sure if this is the right board but it is in c... I've got a console program written in c which receives key presses and gets a Windows Virtual key code. I am trying to convert it to the...
by: Yandos | last post by:
Hello php gurus, maybe it sounds crazy, but I'd would like to use php for some system administration, backups, etc, but sometimes user interaction is needed. Till now i have been using windows...
by: yasin | last post by:
is it possible inputting characters from keyboard by codes.or can we use assemly interrupts in asp.net to press keyboard buttons by code.
by: JDeats | last post by:
I have a WinForm with a single command button in the middle, I have added a KeyDown event handler to the form and the method for this is: private void Form2_KeyDown(object sender, KeyEventArgs e)...
by: perpetualsoft | last post by:
hey guys i have a big problem,actually I m developing a software for my a client. this is verfication of RFID card via PS/2 port its reader. I can read data from card easily when application is...
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.