Using msvcrt (in Windows), how to catch Enter key?
Dick Moores
rdm at rcblue.com
Mon Oct 29 08:23:54 EDT 2007
At 04:29 AM 10/29/2007, Filip Wasilewski wrote:
>On Oct 29, 11:26 am, Dick Moores <r... at rcblue.com> wrote:
> > Windows XP Pro, Python 2.5.1
> >
> > import msvcrt
> > while True:
> > if msvcrt.kbhit():
> > key = msvcrt.getch()
> > if key == 'Enter'
> > do something
> >
> > Is there a way to catch the pressing of the 'Enter' key?
>
>Yes there is. Just open the Python shell and see what is being
>returned by `getch` or `getche` functions when you press Enter:
>
> >>> import msvcrt
> >>> msvcrt.getch()
>'\r'
Terrific! Thanks.
>Also try to avoid `busy waiting` and calling msvcrt.kbhit in a loop
>without a sleep statement.
>I don't know your case but probably this
>should be enough:
>
>while True:
> if msvcrt.getch() == '\r':
I tried it and find that without the msvcrt.kbhit the first key I hit
doesn't do anything. I have to hit that key again, or another key.
Dick
More information about the Python-list
mailing list