2016-09-28 2 views
0

J'ai ce scriptSense flèche vers le haut en Python?

import sys, os, termios, tty 
home = os.path.expanduser("~") 
history = [] 
if os.path.exists(home+"/.incro_repl_history"): 
    readhist = open(home+"/.incro_repl_history", "r+").readlines() 
    findex = 0 
    for j in readhist: 
     if j[-1] == "\n": 
      readhist[findex] = j[:-1] 
     else: 
      readhist[findex] = j 
     findex += 1 
    history = readhist 
    del readhist, findex 

class _Getch: 
    def __call__(self): 
      fd = sys.stdin.fileno() 
      old_settings = termios.tcgetattr(fd) 
      try: 
       tty.setraw(sys.stdin.fileno()) 
       ch = sys.stdin.read(3) 
      finally: 
       termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) 
      return ch 

while True: 
    try: 
     cur = raw_input("> ") 
     key = _Getch() 
     print key 
     if key == "\x1b[A": 
      print "\b" * 1000 
      print history[0] 
     history.append(cur) 
    except EOFError: 
     sys.stdout.write("^D\n") 
     history.append("^D") 
    except KeyboardInterrupt: 
     if not os.path.exists(home+"/.incro_repl_history"): 
      histfile = open(home+"/.incro_repl_history", "w+") 
      for i in history: 
       histfile.write(i+"\n") 
     else: 
      os.remove(home+"/.incro_repl_history") 
      histfile = open(home+"/.incro_repl_history", "w+") 
      for i in history: 
       histfile.write(i+"\n") 
    sys.exit("") 

Effectué, il s'est le contenu de /home/bjskistad/.incro_repl_history, lit les lignes, et supprime le caractère Newspace et définit alors la classe/fonction _Getch. Ensuite, il exécute la boucle principale du script. Il try s pour définir cur à raw_input(). J'essaie ensuite de détecter la flèche vers le haut en utilisant la classe _Getch définie. C'est là que j'ai des problèmes. Je ne peux pas sentir la flèche vers le haut en utilisant ma classe _Getch. Comment puis-je détecter la flèche vers le haut avec mon code actuel?

Répondre

0

La fonction raw_input lecture toujours une chaîne jusqu'à ENTRER, pas un seul caractère (flèche, etc.)

Vous devez définir votre propre fonction getch, voir: Python read a single character from the user.

Ensuite, vous pouvez ré-implémenter votre fonction "entrée" avec une boucle en utilisant la fonction getch.

Voici une utilisation simple:

while True: 
    char = getch() 
    if char == '\x03': 
     raise SystemExit("Bye.") 
    elif char in '\x00\xe0': 
     next_char = getch() 
     print("special: {!r}+{!r}".format(char, next_char)) 
    else: 
     print("normal: {!r}".format(char)) 

Sous Windows, avec les touches suivantes: Hello<up><down><left><right><ctrl+c>, vous obtiendrez:

normal: 'H' 
normal: 'e' 
normal: 'l' 
normal: 'l' 
normal: 'o' 
special: '\xe0'+'H' 
special: '\xe0'+'P' 
special: '\xe0'+'K' 
special: '\xe0'+'M' 

Alors flèche correspond aux caractères combinant: « \ xe0H ".

+0

Ce code a l'air un peu ancien. – baranskistad

+0

Quel est le code pour entrer? – baranskistad

+0

Ancien mais disponible avec Python 3. Essayez chr (13) pour Entrée, chr (27) pour Escape ... –