473,396 Members | 2,023 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,396 software developers and data experts.

keypressed() function

I need a function (blocking or non-blocking) that tells me if a key has
been pressed (even before it has been released etc.). Also, I would of
course like to know _which_ key has been pressed.

I know that this probably does not exist in the Python library already
as a platform-independant abstraction (even though it probably could),
but then I would at least like solutions that works on Windows and on
Linux.

/David

Dec 26 '06 #1
4 2776
pi************@gmail.com wrote:
I need a function (blocking or non-blocking) that tells me if a key has
been pressed (even before it has been released etc.). Also, I would of
course like to know _which_ key has been pressed.

I know that this probably does not exist in the Python library already
as a platform-independant abstraction (even though it probably could),
but then I would at least like solutions that works on Windows and on
Linux.

/David
Its a terminal I/O function - not a platform function. E.g. On Win only in a rough console msvcrt.kbhit() does it. In PythonWin, IPython, Crust ... things are of course different.
On regular Unix terminals you have the sys.stdin file:

sys.stdin.read(1) #maybe in a thread and interthread-pass it to your main loop

or possibly trick with fcntl.fcntl(sys.stdin, fcntl.F_SETFL, os.O_NDELAY|os.O_NONBLOCK)

when nothing is on sys.stdin - you get immediately an IOError:
>>sys.stdin.read(1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 11] Resource temporarily unavailable
And see also other ioctl, termios, tty, cbreak, curses .. functions to get things early before \n buffering depending on the terminal mode )
Robert
Dec 26 '06 #2
At Tuesday 26/12/2006 10:25, pi************@gmail.com wrote:
>I need a function (blocking or non-blocking) that tells me if a key has
been pressed (even before it has been released etc.). Also, I would of
course like to know _which_ key has been pressed.
On Windows you can listen to the messages WM_KEYDOWN/WM_KEYUP.
Or, for a specific key, you can use GetKeyState/GetAsyncKeyState. For
the whole keyboard use GetKeyboardState.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Dec 26 '06 #3
I know that this probably does not exist in the Python library already
as a platform-independant abstraction (even though it probably could),
but then I would at least like solutions that works on Windows and on
Linux.
http://aspn.activestate.com/ASPN/Coo.../Recipe/134892

But it is blocking.

--
mvh Björn
Dec 28 '06 #4

pi************@gmail.com wrote:
I need a function (blocking or non-blocking) that tells me if a key has
been pressed (even before it has been released etc.). Also, I would of
course like to know _which_ key has been pressed.

I know that this probably does not exist in the Python library already
as a platform-independant abstraction (even though it probably could),
but then I would at least like solutions that works on Windows and on
Linux.
Hmm.. 2 questions on this today. On Windows PyHook will work. It
signals for both Key Up and Key Down events. However it is a Windows
only module making use of very platform specific API. I am not sure if
there is an equivalent for Linux.

Dec 29 '06 #5

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
7
by: Karl Ebener | last post by:
Hi! A newbie question: How can I check, whether a key has been pressed and take that input to perform some action? I don't want to wait for that input (=> no Enter needed) and the input should...
5
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
2
by: active | last post by:
I have a picturebox usercontrol sitting on a usercontrol. I need a KeyPress event in the PictureBox. I suppose I could have a property in the pictureBox that is accessed by a call in a...
6
by: Shirenseru | last post by:
Hi, I recently saw on a website, that there was a possibility that when you're on a certain webpage, and you type like " code " on your keyboard, that some function is being called. Does...
2
by: Memborg | last post by:
Hej Everybody I have tried do make an "instant search" in javascript, ie everytime one hits a key the javascript sends the request to a database and returns the result into a table. The...
2
by: Peter Newman | last post by:
vb.net 2003 i have the following procedure Private Sub StrLicence_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles StrLicence.KeyPress Select Case...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.