From a1ba318bf886f79a02fa8b82a9eeb2fca6e6369e Mon Sep 17 00:00:00 2001 From: Jan Wille Date: Mon, 2 May 2022 11:49:23 +0200 Subject: [PATCH 1/6] Revert bad PR #65 from jhonatan-lopes/master This PR broke the BACKSPACE on Linux systems and introduced further inconsistencies into the codebase --- readchar/key.py | 2 +- readchar/readchar.py | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/readchar/key.py b/readchar/key.py index 865427f..33cc87d 100644 --- a/readchar/key.py +++ b/readchar/key.py @@ -2,7 +2,7 @@ LF = "\x0d" CR = "\x0a" ENTER = "\x0d" -BACKSPACE = "\x08" +BACKSPACE = "\x7f" SUPR = "" SPACE = "\x20" ESC = "\x1b" diff --git a/readchar/readchar.py b/readchar/readchar.py index 8aa352a..6e58f40 100644 --- a/readchar/readchar.py +++ b/readchar/readchar.py @@ -53,10 +53,10 @@ 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 + 18656: key.UP, + 20704: key.DOWN, + 19424: key.LEFT, + 19936: key.RIGHT, } def readkey(getchar_fn=None): From 8544d1b779823fbef388b53bfba54f7bc704cb37 Mon Sep 17 00:00:00 2001 From: Jan Wille Date: Mon, 2 May 2022 12:37:14 +0200 Subject: [PATCH 2/6] bump black in pre-commit, as 22.1.0 is unstable --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 45f221f..f2a938b 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 From 35dc17a8cce0be5a357ec9d4ff21fc3c7755ba96 Mon Sep 17 00:00:00 2001 From: Jan Wille Date: Mon, 11 Jul 2022 15:16:38 +0200 Subject: [PATCH 3/6] 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 --- readchar/readchar.py | 59 +++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/readchar/readchar.py b/readchar/readchar.py index 6e58f40..1a3fd58 100644 --- a/readchar/readchar.py +++ b/readchar/readchar.py @@ -19,44 +19,40 @@ if sys.platform in ("win32", "cygwin"): # - # 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, - 18656: key.UP, - 20704: key.DOWN, - 19424: key.LEFT, - 19936: key.RIGHT, + # 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): @@ -69,13 +65,10 @@ 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 else: return ch.decode() From 87a91badf06864c329a3529ef45ff37254c4a00d Mon Sep 17 00:00:00 2001 From: Jan Wille Date: Mon, 2 May 2022 11:51:32 +0200 Subject: [PATCH 4/6] windows actually uses \x08 for BACKSPACE closes #57 --- readchar/readchar.py | 2 ++ test.py | 1 + 2 files changed, 3 insertions(+) diff --git a/readchar/readchar.py b/readchar/readchar.py index 1a3fd58..beb74d0 100644 --- a/readchar/readchar.py +++ b/readchar/readchar.py @@ -69,6 +69,8 @@ def readkey(getchar_fn=None): return xlate_dict[b] except KeyError: return None + elif a == 8: + return key.BACKSPACE else: return ch.decode() diff --git a/test.py b/test.py index d78b18f..e3fa4b1 100644 --- a/test.py +++ b/test.py @@ -25,6 +25,7 @@ readchar.key.F10: "F10", readchar.key.F12: "F12", readchar.key.ALT_A: "ALT_A", + readchar.key.BACKSPACE: "BACKSPACE", } while True: From 950cab3c4fbc85443e1319766d84320654ad2737 Mon Sep 17 00:00:00 2001 From: Jan Wille Date: Mon, 2 May 2022 12:00:21 +0200 Subject: [PATCH 5/6] windows actually uses \r for ENTER --- readchar/readchar.py | 2 ++ test.py | 1 + 2 files changed, 3 insertions(+) diff --git a/readchar/readchar.py b/readchar/readchar.py index beb74d0..e0a3b47 100644 --- a/readchar/readchar.py +++ b/readchar/readchar.py @@ -71,6 +71,8 @@ def readkey(getchar_fn=None): return None elif a == 8: return key.BACKSPACE + elif a == 13: + return key.ENTER else: return ch.decode() diff --git a/test.py b/test.py index e3fa4b1..7af90f7 100644 --- a/test.py +++ b/test.py @@ -26,6 +26,7 @@ readchar.key.F12: "F12", readchar.key.ALT_A: "ALT_A", readchar.key.BACKSPACE: "BACKSPACE", + readchar.key.ENTER: "ENTER", } while True: From 8693531502c73add9b246d32081f75e1ce9af75c Mon Sep 17 00:00:00 2001 From: Jan Wille Date: Sun, 24 Jul 2022 16:53:02 +0200 Subject: [PATCH 6/6] ignore flask8:C901 the very long "windows only" if statement causes a flake8 warning. As this will be reworked anyway the warning is ignored --- readchar/readchar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readchar/readchar.py b/readchar/readchar.py index e0a3b47..c8869e0 100644 --- a/readchar/readchar.py +++ b/readchar/readchar.py @@ -17,7 +17,7 @@ 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. This dictionary translates # scan codes to the unicode sequences expected by readkey.