[Tutor] key detection

Alan Gauld alan.gauld at btinternet.com
Wed May 6 00:36:18 CEST 2015


On 05/05/15 22:30, Jim Mooney Py3.4.3winXP wrote:
> Can python detect a keypress?

That sounds simple but is actually quite tricky
since it's terminal dependent.

> like Javascript does. All I find are references using msvcrt, which is
> Msoft specific, or using tkinter - but I don't want a whacking big GUI,

You can also use curses on Unix like systems
which is CLI based. But it acts a bit like a GUI, you do need to 
initialize the framework ec.

It's described in the Event Driven Programming topic of my tutorial.

There are other  ways like using the low level C functions directly from 
the C libraries using ctypes, but that brings its own difficulties in 
converting Python data into C compatible types etc.

curses is the preferred option on Unix like systems.

There are some third party modules that try to address this in
a system independent manner, they can be found on PyPI but
I can't recommend any of them as I've not used them. I think
Fred Lundh created one such called, maybe, terminal?

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