import msvcrt
while True:
try:
a=msvcrt.getch()
a=a.decode('utf-8')
print(a)
except:
print(a)
The above piece of code yields unexpected results when I input arrow keys or page up/page down/delete etc.
The output is as follows:
[I/P=a]
a #expected result
[I/P=UP ARROW]
b'\xe0'
H #unexpected result
I can understand b'\xe0' being printed, but why is H
being printed along with it?
H is not printed when I do this:
import msvcrt
a=msvcrt.getch()
print(a)#b'\xe0'
a=a.decode('utf-8')
print(a)
When I input UP ARROW here, it raises a UNICODEDECODERROR.
I've looked at the other question that explains how msvcrt.getch() works, but that still fails to explain why I get two characters in the first piece of code and only one character in the second piece of code. Instead of waiting for the next character to be entered, why does a assume the value b'H'?