[Tutor] how does one poll the keyboard?

Gregor Lingl glingl@aon.at
Fri, 23 Nov 2001 18:26:40 +0100


This is a multi-part message in MIME format.

------=_NextPart_000_0021_01C1744C.64F07020
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Dear Tony!

Your problem seems to have a platform-specific
character.
Maybe the following points to a solution of your
problem (i. e. if you are using Windows). It uses
the msvcrt-module contained in the windows-distribution.

import msvcrt
while 1:
    a =3D  msvcrt.getch()
    if a =3D=3D chr(13): break
    print a

This is a slightliy modified snippet from Python 2.1 - Bible
(p. 669) which in my opinion is worth reading (or at least=20
using).

However, if you already are stuffed with Python literature:
there is also an example in Lundh's Python Standard Library
on msvcrt. Could be found for example in the index under the
keyword:

    kbhit function, using the msvcrt module=20
         and, 233

Hope that helps

Gregor
  ----- Original Message -----=20
  From: tonycervone=20
  To: Tutor@python.org=20
  Sent: Friday, November 23, 2001 5:37 PM
  Subject: [Tutor] how does one poll the keyboard?


   How does one poll the keyboard with Python to see if a specific key( =
that I choose to define as the key to press) has been pressed while a =
program is running?  I guess the script would poll the keyboard and run =
in the background of a main program.Of course, the script could not have =
an indefinite loop so as to prevent the program from running. The =
program will then quickly run the script and if the key has been pressed =
a message will appear that such a key has been pressed.=20
  thanks.
  PS. I have now most books on Python; yet I still feel that the indices =
are not comprehensive enough for novices.  I know that this is a general =
statement, but for this particular question , for example, I could not =
find anything in the back of the books. the books are: The quick Python =
Book, Python Standard Library, learn to Program using Python, and the =
big Core Python Programming. Considering the number of pages( a lot), =
they all seem to lack easy and comprehensive indices. The examples in =
the books, too, have very small type and assume some intuition about how =
Python works . This is not meant as a criticism, but constructive =
observations that should help authors who want to reach a wider general =
want to be audience. The folks are among the best, most knowlegeable and =
kind helpers I have found. So keep up the good work.


    tony

------=_NextPart_000_0021_01C1744C.64F07020
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3D"Courier New" size=3D2>Dear Tony!</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Your problem seems to have a=20
platform-specific</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>character.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Maybe the following&nbsp;points =
to a=20
solution of your</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>problem (i. e. if you are using =
Windows).=20
It uses</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>the msvcrt-module contained in =
the=20
windows-distribution.</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>import msvcrt</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>while 1:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; a =3D =
</FONT>&nbsp;<FONT=20
face=3D"Courier New" size=3D2>msvcrt.getch()</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; if a =3D=3D =
chr(13):=20
break</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; print =
a</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>This is a slightliy modified =
snippet from=20
Python 2.1 - Bible</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>(p. 669) which in my opinion is =
worth=20
reading (or at least </FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>using).</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>However, if you already are =
stuffed with=20
Python literature:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>there is also an example in =
Lundh's Python=20
Standard Library</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>on msvcrt. Could be found for =
example in=20
the index under the</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>keyword:</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>&nbsp;&nbsp;&nbsp; kbhit =
function, using=20
the msvcrt module </FONT></DIV>
<DIV><FONT face=3D"Courier New"=20
size=3D2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; and, =
233</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Hope that helps</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Gregor</FONT></DIV>
<BLOCKQUOTE dir=3Dltr=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A title=3Dtonycervone@netzero.net=20
  href=3D"mailto:tonycervone@netzero.net">tonycervone</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A title=3DTutor@python.org =

  href=3D"mailto:Tutor@python.org">Tutor@python.org</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Friday, November 23, 2001 =
5:37=20
  PM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> [Tutor] how does one =
poll the=20
  keyboard?</DIV>
  <DIV><BR></DIV>
  <DIV><FONT face=3DArial size=3D2>&nbsp;How does one poll the keyboard =
with Python=20
  to see if a specific key( that I choose to define as the key to press) =
has=20
  been pressed while a program is running?&nbsp; I guess the script =
would poll=20
  the keyboard and run in the background of a main program.Of course, =
the script=20
  could not have an indefinite loop so as to prevent the program from =
running.=20
  The program will then quickly run the script and if the key has been =
pressed a=20
  message will appear that such a key has been pressed. </FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>thanks.</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2>PS. I have now most books on Python; =
yet I still=20
  feel that the indices are not comprehensive enough for novices.&nbsp; =
I know=20
  that this is a general statement, but for this particular question , =
for=20
  example, I could not find anything in the back of the books. the books =
are:=20
  The quick Python Book, Python Standard Library, learn to Program using =
Python,=20
  and the big Core Python Programming. Considering the number of pages( =
a lot),=20
  they all seem to lack easy and comprehensive indices. The examples in =
the=20
  books, too, have very small type and assume some intuition about how =
Python=20
  works&nbsp;. This is not meant as a criticism, but constructive =
observations=20
  that should help authors who want to reach a wider general want to be=20
  audience. The folks are among the best, most knowlegeable and kind =
helpers I=20
  have found. So keep up the good work.</FONT></DIV>
  <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
  <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
  <DIV><FONT face=3DArial=20
size=3D2>&nbsp;&nbsp;tony</FONT></DIV></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_0021_01C1744C.64F07020--