Skip to content

Commit a7c0645

Browse files
committed
simplify windows scancode dict
the windows scancodes consist of two bytes, the first denoting a special scancode and the second being the actual key information. Previously both bytes where used to calculate the number to look up in a dictionary, but as only the second byte is actually different, this can be simplified. closes #75, closes #66, closes #9, closes #8
1 parent 2140334 commit a7c0645

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

readchar/readchar.py

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,44 +19,40 @@
1919

2020
if sys.platform in ("win32", "cygwin"):
2121
#
22-
# Windows uses scan codes for extended characters. The ordinal returned is
23-
# 256 * the scan code. This dictionary translates scan codes to the
24-
# unicode sequences expected by readkey.
22+
# Windows uses scan codes for extended characters. This dictionary translates
23+
# scan codes to the unicode sequences expected by readkey.
2524
#
2625
# for windows scan codes see:
2726
# https://msdn.microsoft.com/en-us/library/aa299374
2827
# or
2928
# http://www.quadibloc.com/comp/scan.htm
3029
xlate_dict = {
31-
13: key.ENTER,
32-
27: key.ESC,
33-
15104: key.F1,
34-
15360: key.F2,
35-
15616: key.F3,
36-
15872: key.F4,
37-
16128: key.F5,
38-
16384: key.F6,
39-
16640: key.F7,
40-
16896: key.F8,
41-
17152: key.F9,
42-
17408: key.F10,
43-
22272: key.F11,
44-
34528: key.F12,
45-
7680: key.ALT_A,
30+
59: key.F1,
31+
60: key.F2,
32+
61: key.F3,
33+
62: key.F4,
34+
63: key.F5,
35+
64: key.F6,
36+
65: key.F7,
37+
66: key.F8,
38+
67: key.F9,
39+
68: key.F10,
40+
133: key.F11,
41+
134: key.F12,
4642
# don't have table entries for...
4743
# CTRL_ALT_A, # Ctrl-Alt-A, etc.
4844
# CTRL_ALT_SUPR,
49-
# CTRL-F1
50-
21216: key.INSERT,
51-
21472: key.SUPR, # key.py uses SUPR, not DELETE
52-
18912: key.PAGE_UP,
53-
20960: key.PAGE_DOWN,
54-
18400: key.HOME,
55-
20448: key.END,
56-
18656: key.UP,
57-
20704: key.DOWN,
58-
19424: key.LEFT,
59-
19936: key.RIGHT,
45+
# CTRL_ALT_A, .., etc.
46+
82: key.INSERT,
47+
83: key.SUPR, # key.py uses SUPR, not DELETE
48+
73: key.PAGE_UP,
49+
81: key.PAGE_DOWN,
50+
71: key.HOME,
51+
79: key.END,
52+
72: key.UP,
53+
80: key.DOWN,
54+
75: key.LEFT,
55+
77: key.RIGHT,
6056
}
6157

6258
def readkey(getchar_fn=None):
@@ -69,13 +65,10 @@ def readkey(getchar_fn=None):
6965
a = ord(ch)
7066
if a == 0 or a == 224:
7167
b = ord(msvcrt.getch())
72-
x = a + (b * 256)
73-
7468
try:
75-
return xlate_dict[x]
69+
return xlate_dict[b]
7670
except KeyError:
7771
return None
78-
return x
7972
else:
8073
return ch.decode()
8174

0 commit comments

Comments
 (0)