Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NCurses and ESC,ALT keys

Tags:

key

ncurses

I have a problem with NCurses... i need to handle all keys like Esc, Alt+F etc. Problem is that the codes are similar... i.e:


Esc - 27


Alt+A - 27 65


As an example there is double code for Alt+[key] combination what similar to Esc key... Any ideas how handle that?

like image 322
Marcin Petrów Avatar asked May 12 '11 11:05

Marcin Petrów


2 Answers

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
like image 104
Marcin Petrów Avatar answered Sep 20 '22 21:09

Marcin Petrów


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)
like image 29
sjohnson.pi Avatar answered Sep 17 '22 21:09

sjohnson.pi