Skip to content

Moving with the directional arrows doesn't work. #4

Description

@4surix

OS : Linux MINT
Version OS : 21.2
Terminal : xfce4-terminal
Version terminal : 1.0.4


Problem

Moving with the arrows doesn't work.
In my terminal, arrow directions send three characters, first Escape, then [, then one character :

  • A for Up
  • B for Down
  • C for Right
  • D for Left

The problem is here :

pitwi/pitwi/keypress.py

Lines 126 to 127 in 0f7d346

elif key == '\x1b':
key = 'Escape'

First solution

    elif key == '\x1b':
        key = getKey()

        if key == "[":
            key = getKey()

            if key == "A":
                key = "Up"
            elif key == "B":
                key = "Down"
            elif key == "C":
                key = "Right"
            elif key == "D":
                key = "Left"
        else:
            key = 'Escape'

BUT this doesn't work any more with terminals that just return Escape and use another form of character for movement with the directional arrows. This will stop the program looking for a key that doesn't (yet) exist.

Second solution

It would be necessary to create a listener that retrieves the keys.
Then the analyses, and finally a method for retrieving the analyzed keys.
This separates retrieval from listening, and will allow you to retrieve just the keys that have been preset and not wait.

Example

class Listener:

    def __init__(self):

        self.__running = False
        self.__keys = []

    @property
    def next(self):
        return self.__keys[0]

    def run(self):

        if self.__running:
            return

        self.__running = True

        while True:
            self.__keys.append(getKey())

    def get(self) -> str:

        if self.next == '\x1b':
            self.__keys.pop(0)

            if self.next == '[':
                self.__keys.pop(0)

                if self.next == "A":
                    self.__keys.pop(0)
                    key = "Up"

                ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions