NCurses and ESC,ALT keys

Here is an example for python:

key = self.screen.getch()
if key == ord('q'): # quit
    go = False
elif key == 27: # Esc or Alt
    # Don't wait for another key
    # If it was Alt then curses has already sent the other key
    # otherwise -1 is sent (Escape)
    self.screen.nodelay(True)
    n = self.screen.getch()
    if n == -1:
        # Escape was pressed
        go = False
    # Return to delay
    self.screen.nodelay(False)

Resolved by:

  1. Use noecho or timeout mode
  2. Check for 27(ALT or ESC) code... if pass:
  3. try to read another code
  4. if another code is ERR then.. you have ESC key in other way you have ALT+another code

Tags:

Key

Ncurses