Skip to content

The cursor keys in application mode are not match the constants #124

Description

@chen3feng

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    out of scopethis is currently not managable (but may be in the future)

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions