getch python returning without using

Asked

Viewed 574 times

1

I’m trying to use the getch from python to grab the key the user pressed, but without me pressing anything it is returning "b'\xff'", and if I use ord(getch()), it returns 255. If you can help me thank.

from msvcrt 
import getch key = msvcrt.getch() 
print(getch()) 
if key == 0: 
    quit() 
elif key == 1: 
    print("Game inicado") 
else: 
    print('Opção invalida!') 
  • Add your code so we can reproduce the error and help you ;)

1 answer

2

Your syntax is wrong at first:

Instead of:

from msvcrt 
import getch key = msvcrt.getch() 

Should be:

from msvcrt import getch
key = msvcrt.getch() 

Besides, this piece is suspicious:

print(getch())

I think what you wanted was this:

print(key)

And also, instead of key == 0 and key == 1, you should use key == '0' and key == '1'. I mean, you forgot to put the quotes.

  • The biggest problem is not especially this, but yes, why I neither grip anything and the getch already returns something.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.