Python Forum
How to recognize space or enter as one-character input? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to recognize space or enter as one-character input? (/thread-21849.html)



How to recognize space or enter as one-character input? - Mark17 - Oct-17-2019

The program I'm currently working on asks the user to press SPACE if correct or ENTER if incorrect. Can you give me a code snippet that will enable Python to recognize this? I started with this:
checkif = True #this indicates whether I still need to check input
for r in range(origlistsize): #this will be done origlistsize times (total # of problems in list)
    question = random.randint(0,curlistsize)
    answer = input(multfacts[question])
    while checkif == True:
        if answer=="" or answer==" ": checkif = False
        else:
            answer = input("Enter SPACE if that was correct or ENTER if wrong: ")
The problem is that in order to recognize a space or enter, the enter key has to be subsequently pressed. This makes for two keystrokes when I really want one due to time constraints.


RE: How to recognize space or enter as one-character input? - ichabod801 - Oct-17-2019

The input function returns what the user typed up to but not including the final enter. So if they just hit enter, and empty string ('') will be returned. If they hit space + enter, a space character will be returned (' ').

If they just hit space without hitting enter, input will sit there waiting for them to hit enter.


RE: How to recognize space or enter as one-character input? - Mark17 - Oct-17-2019

(Oct-17-2019, 03:44 PM)ichabod801 Wrote: The input function returns what the user typed up to but not including the final enter. So if they just hit enter, and empty string ('') will be returned. If they hit space + enter, a space character will be returned (' ').

If they just hit space without hitting enter, input will sit there waiting for them to hit enter.

Is there an altogether different command that solicits input and records it without the "final enter?"

I've seen programs in the past (language unknown) that will say things like "enter Y or N" and proceed once that first keystroke (Y or N) is pressed. That's what I'm trying to do.


RE: How to recognize space or enter as one-character input? - ichabod801 - Oct-17-2019

(Oct-17-2019, 05:06 PM)Mark17 Wrote: Is there an altogether different command that solicits input and records it without the "final enter?"

You can build one, but that low level access to the system makes it platform dependent. There's a thread at Stack Overflow with some examples.


RE: How to recognize space or enter as one-character input? - Mark17 - Oct-17-2019

Thanks!


RE: How to recognize space or enter as one-character input? - jefsummers - Oct-17-2019

There is a library called keyboard that may help. Looks like works on Windows and Linux, not sure about Mac.
Keyboard module on PyPI