I need to create a program (game) in python where the user quickly enters 2 keys such as the z or x and this makes their character move forward every time they press it in the right order. I've done a lot of research and decided that I should use msvcrt.getch()
to receive the input, but whenever I try it, it stores the input as a byte which I cannot use. I am relatively new to programming and could use some help explaining this function.
You need to decode the returned value to a str
object:
msvcrt.getch().decode('ASCII')
would interpret the byte as a ASCII codepoint, for example. You may need to use a different encoding depending on the keyboard layout and locale, but the msvcrt.getch()
API specifically only deals with ASCII characters according to the documentation:
The module implements both the normal and wide char variants of the console I/O api. The normal API deals only with ASCII characters and is of limited use for internationalized applications. The wide char API should be used where ever possible.
You probably want to use msvcrt.getwch()
instead to get Unicode values directly; the method supports more than just ASCII codepoints as well.