1

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";
}
3

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

This library requires root access in ubuntu at least
0

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...")

4 Comments

this would work for windows but does it work with other OS ?
for which OS specifically?
I Code In Vc (Windows) But the function should be cross platform supportable (Linux,MacOS) @FishingCode
some of these have code that works across different OS systems, check this link ...
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.