Reputation: 19
I am a c++ User and im am newly learning python,
i used to use getch()
statements in c++ to get user input without having to press enter although it had some limitation ... I am looking for a similar function in python (anything other than raw_input()
& input()
which require enter key stroke). would prefer the code to have cross platform support
for eg in c++ :
void getkey()
{
_getch();
cout<<"You Hit A Key";
}
Upvotes: 1
Views: 8085
Reputation: 1419
Was writing a simple drum machine on a Raspberry Pi using Python and Sonic Pi: press a key, play a sound. Here's the core of the input routine.
pip install getch
import getch
while True:
key = getch.getch()
if key == 'x':
break
Upvotes: 0
Reputation: 7390
Here is a simple example using keyboard
:
import keyboard
while True:
event = keyboard.read_event()
if event.event_type == keyboard.KEY_DOWN:
key = event.name
print(f'Pressed: {key}')
if key == 'q':
break
Upvotes: 2
Reputation: 2019
You could do this if you are on a windows OS.
import msvcrt
print("Press y or n to continue: \n")
getLetter = msvcrt.getch()
if getLetter.upper() == 'S':
print("cool it works...")
else:
print("no it doesn't work...")
Upvotes: 0