Open
Description
https://tldp.org/HOWTO/Keyboard-and-Console-HOWTO-21.html
When the terminal is in application cursor key mode, the cursor keys produce Esc O x
and otherwise Esc [ x
where x is one of A
, B
, C
, 'D`. Certain programs put the terminal in application cursor key mode; if you kill them with kill -9, or if they crash, then the mode will not be reset.
The cursor keys are different between the two modes:
https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences#cursor-keys
The key code constants only match the normal mode.
To fix this, I think the easiest way is to reset the cursor key mode to normal:
def readkey() -> str:
"""Get a keypress. If an escaped key is pressed, the full sequence is
read and returned as noted in `_posix_key.py`."""
# Reset the terminal to normal cursor key mode
# https://tldp.org/HOWTO/Keyboard-and-Console-HOWTO-21.html
print("\x1b[?1l", end='')
# It can be quite long when input is from IME.
return _readkeybuffer(1000)
If you agree with me, I can create a PR or you can also use the above code directly.