Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ repos:
- name: check YAML format
id: yamllint
- repo: https://github.com/psf/black
rev: 22.1.0
rev: 22.3.0
hooks:
- name: re-format with black
id: black
Expand Down
2 changes: 1 addition & 1 deletion readchar/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
LF = "\x0d"
CR = "\x0a"
ENTER = "\x0d"
BACKSPACE = "\x08"
BACKSPACE = "\x7f"
SUPR = ""
SPACE = "\x20"
ESC = "\x1b"
Expand Down
65 changes: 31 additions & 34 deletions readchar/readchar.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,42 @@
raise NotImplementedError("The platform %s is not supported yet" % sys.platform)


if sys.platform in ("win32", "cygwin"):
if sys.platform in ("win32", "cygwin"): # noqa: C901
#
# Windows uses scan codes for extended characters. The ordinal returned is
# 256 * the scan code. This dictionary translates scan codes to the
# unicode sequences expected by readkey.
# Windows uses scan codes for extended characters. This dictionary translates
# scan codes to the unicode sequences expected by readkey.
#
# for windows scan codes see:
# https://msdn.microsoft.com/en-us/library/aa299374
# or
# http://www.quadibloc.com/comp/scan.htm
xlate_dict = {
13: key.ENTER,
27: key.ESC,
15104: key.F1,
15360: key.F2,
15616: key.F3,
15872: key.F4,
16128: key.F5,
16384: key.F6,
16640: key.F7,
16896: key.F8,
17152: key.F9,
17408: key.F10,
22272: key.F11,
34528: key.F12,
7680: key.ALT_A,
59: key.F1,
60: key.F2,
61: key.F3,
62: key.F4,
63: key.F5,
64: key.F6,
65: key.F7,
66: key.F8,
67: key.F9,
68: key.F10,
133: key.F11,
134: key.F12,
# don't have table entries for...
# CTRL_ALT_A, # Ctrl-Alt-A, etc.
# CTRL_ALT_SUPR,
# CTRL-F1
21216: key.INSERT,
21472: key.SUPR, # key.py uses SUPR, not DELETE
18912: key.PAGE_UP,
20960: key.PAGE_DOWN,
18400: key.HOME,
20448: key.END,
18432: key.UP, # 72 * 256
20480: key.DOWN, # 80 * 256
19200: key.LEFT, # 75 * 256
19712: key.RIGHT, # 77 * 256
# CTRL_ALT_A, .., etc.
82: key.INSERT,
83: key.SUPR, # key.py uses SUPR, not DELETE
73: key.PAGE_UP,
81: key.PAGE_DOWN,
71: key.HOME,
79: key.END,
72: key.UP,
80: key.DOWN,
75: key.LEFT,
77: key.RIGHT,
}

def readkey(getchar_fn=None):
Expand All @@ -69,13 +65,14 @@ def readkey(getchar_fn=None):
a = ord(ch)
if a == 0 or a == 224:
b = ord(msvcrt.getch())
x = a + (b * 256)

try:
return xlate_dict[x]
return xlate_dict[b]
except KeyError:
return None
return x
elif a == 8:
return key.BACKSPACE
elif a == 13:
return key.ENTER
else:
return ch.decode()

Expand Down
2 changes: 2 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
readchar.key.F10: "F10",
readchar.key.F12: "F12",
readchar.key.ALT_A: "ALT_A",
readchar.key.BACKSPACE: "BACKSPACE",
readchar.key.ENTER: "ENTER",
}

while True:
Expand Down