[Tutor] Use python to listen for key strokes

Alan Gauld alan.gauld at yahoo.co.uk
Wed Sep 16 20:28:26 EDT 2020


On 17/09/2020 00:11, Chris C wrote:

> This is what I have:
> 
>    import msvcrt
>    while True:
>        if msvcrt.kbhit():

kbhit() tells you if a key is hit at that very instant.

>            key = msvcrt.getch()

But getch will wait till a key is hit before returning.
There is therefore no need for the kbhit() test.

>            print(key)   # just to show the result

Note that getch() returns a byte string not a char string.
You probably want to decode it.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list