I've remade this simpler version of Python 3.8 code that emulated some undesired behaviour in my program running on Windows 10. The main external dependency is this keyboard module:
import keyboard
from time import sleep
def get_key() -> str:
"""
Get the user-pressed key as a lowercase string. E.g. pressing the spacebar returns 'space'.
:return: str - Returns a string representation of the pressed key.
"""
print(f"Press any key...")
while True:
pressed_key: str = str(keyboard.read_key()).lower()
if not keyboard.is_pressed(pressed_key): # Wait for key release or keyboard detects many key presses.
break
else:
continue
return pressed_key
def main() -> None:
pressed_key: str = get_key()
print(f"You have pressed: '{pressed_key}'.")
sleep(1)
input("Now take other user input: ")
if __name__ == "__main__":
main()
when running python ./my_script.py
in the terminal and pressing a random key (say 'g'), I get the following output:
> Press any key...
> You have pressed: 'g'.
> Now take other user input: g
The issue is that when the program gets to the builtin input()
function, the character 'g
' is prepopulated in the terminal. This is problematic if, say, the initial user input is 'enter
' and the input function is then skipped without ever getting input from the user. Also, it can populate user input data unwanted characters.
Is there a simple way to:
- Temporarily move focus away from the terminal?
- Flush the stdin? (
sys.stdin.flush()
sadly does not work) - Use 'keyboard' in a different way such that this double character recording behaviour does not occur?
All questions and responses are welcome, thank you.