473,326 Members | 2,148 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,326 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 16359
[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.

HTH,

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
Jul 18 '05 #3

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

Similar topics

1
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...
2
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
0
by: chewie | last post by:
Does anyone know how to capture keyboard scan codes using C#? Thanks
3
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??
2
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...
2
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...
1
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.
1
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)...
9
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
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...
0
isladogs
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 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.