[Tutor] Single char input

Alan Gauld alan.gauld at yahoo.co.uk
Thu Oct 24 19:01:11 EDT 2019


I should add that this is all much easier and portable in a GUI framework such as Tkinter. It handles all the weirdness for you.

Alan g

On 24 October 2019, at 23:48, David <bouncingcats at gmail.com> wrote:

On Thu, 24 Oct 2019 at 08:25, Ed Connell <edwinconnell at gmail.com> wrote:
>
> Hi,
> How can I accept, evaluate, and act on a single keypress in python?

Hi,
There is code to do this under the link "pykbhit" at:
https://simondlevy.academic.wlu.edu/software/

I tested it on python 2 and 3 on Debian stretch.

It appears to work except that, like most similar code, it does not
handle the keys that emit more than one character.

There are many such keys (for example the function keys and the cursor
movement keys) which all emit a short sequence of characters beginning
with the same character that the "escape" key emits.

How to see evidence of these codes depends on what operating system
you are using, which you have not yet told us. If you are on Debian Linux,
start a 'dash' shell (not bash). Then press some keys and the terminal will
show you the character sequences that the keys emit.

So this code just detects all those keys as an "escape" key, because
that is the first one in the sequence. With some work it could be
extended to detect those keys too, however it's not a simple job.

I searched for a simple explanation of this for you, but was unable to find
something appropriate. Maybe someone else knows of one.
If you need more information then ask again here, or try reading these
links which are the best I could find but unfortunately more complex
or domain-specific that I hoped to offer you:
https://unix.stackexchange.com/questions/103608/is-there-any-reason-why-i-get-a-when-i-press-up-arrow-at-the-console-login-sc
https://mywiki.wooledge.org/ReadingFunctionKeysInBash
_______________________________________________
Tutor maillist  -  Tutor at python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list