keypressed() function

P

pinkfloydhomer

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
 
R

robert

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:
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
 
G

Gabriel Genellina

At said:
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
 
R

Ravi Teja

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top