2011-05-12 9 views
12

Ho un problema con NCurses ... ho bisogno di gestire tutte le chiavi come Esc, Alt +F ecc Il problema è che i codici sono simili .. . cioè:NCurses e ESC, tasti ALT


Esc - 27


Alt +A - 27 65


Come esempio c'è il doppio codice per Alt +[tasto] combinazione quello simile-Esc chiave ... Qualsiasi idee come gestirlo?

risposta

7

Risolto da:

  1. Usa noecho o timeout modalità
  2. Verificare la presenza di 27 (ALT o ESC) codice ... se passa:
  3. provare a leggere un altro codice
  4. se un altro codice è ERR quindi .. hai la chiave ESC in altro modo hai ALT + un altro codice
+0

Cosa succede se l'utente preme 'ESC' e quindi un altro tasto come' ['ad esempio, molto veloce uno dopo l'altro, non è possibile che il nostro codice veda entrambe le chiavi e quindi _would non sembra esattamente un' ESC'_? Perché non vedo davvero come il terminale saprebbe che alla mia seconda chiamata raccoglierò il prossimo personaggio che sto cercando di determinare se è stato colpito solo "ESC" ?! –

12

Ecco un esempio per 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) 
Problemi correlati