[Tutor] Single char input

David bouncingcats at gmail.com
Thu Oct 24 18:48:05 EDT 2019


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


More information about the Tutor mailing list