1

I want my Python program to stop and wait until any key is pressed, then to print the key that was just pressed.

It's like the Pascal's command key := readkey; writeln(key);.

I' ve tried yet to use the msvcrt module and the getch command but it didn' t work. I tried this:

import msvcrt 
s = msvcrt.getch()
print (s)

And it didn' t wait I pressed a key, it immediately printed b'\xff' and it ended the program

I' m working with python 3.4

Do you know any way to help me ?

2
  • 7
    possible duplicate of Python read a single character from the user Commented Jul 1, 2015 at 9:34
  • That work fine for me - although you might prefer to use: msvcrt.getche() to also echo the response back to the terminal. How are you running the script? Commented Jul 1, 2015 at 12:50

2 Answers 2

3

I found out that if I started my .py file from the command line (pythonw) the command msvcrt.getch() didn' t work but if I save my .py file and I launched it, the command msvcrt.getch() worked correctly !

Sign up to request clarification or add additional context in comments.

Comments

0

Im not sure this will be usefull for anyone but the answer to this is

s = input()

This will store the input in the "s" variable.

1 Comment

No it isn’t. input waits for the user to input any amount of text and then also press Enter before unblocking and returning all that text. Nor it is single-key-press nor it is an arbitrary key-press because one needs to press Enter afterwards. (And I’m even disregarding the OP’s request for printing the character afterwards because it’s both quite trivial and very separate from getting a keypress.)

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.