pmbarrett314 / curses-menu

A simple console menu system in python using the curses library
MIT License
475 stars 53 forks source link

Keyboard input feature? #35

Closed ttlekich closed 2 years ago

ttlekich commented 6 years ago

I am slightly new to Python/Github (other than just working in my own repo), so I apologize if this is the wrong place or a naive question.

Is there a feature or a possibility of a feature to add keyboard input selection? For example, say I wanted to quit the menu by just typing "q".

I extended the CursesMenu class and added a check for this in process_user_input() (see below).

    def process_user_input(self):
        """
        Gets the next single character and decides what to do with it
        """
        user_input = self.get_input()

        go_to_max = ord("9") if len(self.items) >= 9 else ord(str(len(self.items)))

        if ord("1") <= user_input <= go_to_max:
            self.go_to(user_input - ord("0") - 1)
        elif user_input == curses.KEY_DOWN:
            self.go_down()
        elif user_input == curses.KEY_UP:
            self.go_up()
        elif user_input == ord("\n"):
            self.select()
  ->    elif user_input == ord("q"):
            exit()
        return user_input

I am sure there are better strategies that are not hard-coded...

Thank you!