Type just one character not followed by ENTER - 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: Type just one character not followed by ENTER (/thread-11862.html) |
Type just one character not followed by ENTER - sylas - Jul-29-2018 Hi all! If I remember well,in C++, I could wait an answer to (y/n), the user typing y, not followed by hitting ENTER. Is it possible in python3 ?? Thanks RE: Type just one character not followed by ENTER - buran - Jul-29-2018 https://pypi.org/project/readchar/ https://pypi.org/project/getkey/ https://pypi.org/project/getch/ Click also provides this as functionality http://click.pocoo.org/5/utils/#getting-characters-from-terminal I think recently a noticed yet another one RE: Type just one character not followed by ENTER - snippsat - Jul-29-2018 I just had a post about getting keypress here. So if i rewrite it a little. import msvcrt print('Would you like to go on y/n') while True: ch = msvcrt.getch() if ch in b'\x00': ch = msvcrt.getch() if ch == b'y': print('<y> typed going out') break if ch == b'q': break else: print(f'Key Pressed: {ch}')Test: This is Windows only,as @buran mention Click which is my favorite CLI tool for Python.Click work cross platform and solve Unicode for all shell(even cmd Windows). My tutorial about it. See that i use b'y' to get it to work Python 3.import click click.echo('Continue? [yn] ', nl=False) c = click.getchar() click.echo() if c == b'y': click.echo('We will go on') elif c == b'n': click.echo('Abort!') else: click.echo('Invalid input :(')Test:
|