Jun-22-2018, 12:04 PM
Can do a test,use
Then can write it like this.
Not doing any encoding here
Here is the table.
I pushed Page Up 3 times last
Let see if it match with table that say Page Up is
Did not test that,maybe it easier to keep all as bytes and use
repr()
to see what really going on.import msvcrt while True: character = msvcrt.getch() print(repr(character)) if character == b'q': breakSo i get
b'\x00'
normal key and b'\xe0'
for function keys.>>> import msvcrt >>> help(msvcrt.getch) Help on built-in function getch in module msvcrt: getch() Read a keypress and return the resulting character as a byte string. Nothing is echoed to the console. This call will block if a keypress is not already available, but will not wait for Enter to be pressed. If the pressed key was a special function key, this will return '\000' or '\xe0'; the next call will return the keycode. The Control-C keypress cannot be read with this function.So it say that next call will return keycode.
Then can write it like this.
import msvcrt import sys while True: ch = msvcrt.getch() if ch in b'\x00': ch = msvcrt.getch() # Second call returns the scan code if ch in b'\xe0': ch = msvcrt.getch() # Second call Function keys if ch == b'q': sys.exit() else: print(f'Key Pressed: {ch}')
Output:E:\1
λ python b1.py
Key Pressed: b'h'
Key Pressed: b'e'
Key Pressed: b'l'
Key Pressed: b'l'
Key Pressed: b'o'
Key Pressed: b'I'
Key Pressed: b'I'
Key Pressed: b'I'
Writing hello and 3 times Page Up,as you see no extra space.Not doing any encoding here
utf-8
may be wrong as key are ASCII.Here is the table.
I pushed Page Up 3 times last
b'I'
Let see if it match with table that say Page Up is
73
.>>> k = b'I' >>> ord(k) 73Also look at doc there are eg
msvcrt.getwche()
which return Unicode.Did not test that,maybe it easier to keep all as bytes and use
ord()
if need also need function key decoding.