Skip to content

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

Open

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

  1. Cube707 commented on Apr 5, 2025

    @Cube707
    Collaborator

    I don't think pushing that onto every call to readkey() for every user is a good idea. When needed it can be added by applications on startup once, which is the much better solution.

    There are development efforts to create a context-manager for this lib which would be a good place to add this code, but that is far of from beeing ready to be shipped and I don't have much time to work on it currently.

  2. added
    out of scopethis is currently not managable (but may be in the future)
    on Apr 5, 2025
  3. Cube707 commented on Apr 6, 2025

    @Cube707
    Collaborator

    this is somewhat related to #24

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