Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to create a program/game in python where the user quickly enters 2 keys such as the z or x keys and this makes their character move forward everytime they press it in the right order. I've done a lot of research and decided that I should use msvcrt.getch() to recieve 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 the function.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.9k views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...