473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2787
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.rea d(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/GetAsyncKeyStat e. For
the whole keyboard use GetKeyboardStat e.
--
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
14916
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
7
4570
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 not be displayed. Tnx a lot Karl
5
2816
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 invisible() { alert("invisible() called"); } }
2
7666
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 like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this...
2
12671
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
2618
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 KeyPressed event in the UserControl. Right? Kind of kludge - is there a more direct way? The usercontrol needs no KeyPress event at all and the Picturebox...
6
3351
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 anyone has experience with this? Cause i would really like some help..
2
2852
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 problem is occuring when the result i given. When one do a search the given result is appended to the existing rowx in the table instead of redrawing the...
2
4907
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 e.KeyChar Case Microsoft.VisualBasic.ChrW(13) MsgBox(" ENTER PRESSED") ' move to next field Case Microsoft.VisualBasic.ChrW(8)
0
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.