0

I have this script

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("")

When run, it get's the contents of /home/bjskistad/.incro_repl_history, reads the lines, and removes the newspace character, and then defines the _Getch class/function. Then, it runs the main loop of the script. It trys to set cur to raw_input(). I then try to sense the up arrow using the _Getch class defined. This is where I am having trouble. I can't sense the up arrow using my _Getch class. How can I sense the up arrow with my current code?

baranskistad
  • 2,176
  • 1
  • 21
  • 42

1 Answers1

0

The raw_input function always read a string until ENTER, not a single character (arrow, etc.)

You need to define your own getch function, see: Python read a single character from the user.

Then you can re-implement your "input" function with a loop using getch` function.

Here is a simple usage:

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))

Under Windows, with the following keys: Hello<up><down><left><right><ctrl+c>, you'll get:

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

So arrow corresponds to the combining characters: "\xe0H".

Community
  • 1
  • 1
Laurent LAPORTE
  • 21,958
  • 6
  • 58
  • 103
  • Ancient but available with Python 3. Try chr(13) for Enter, chr(27) for Escape… – Laurent LAPORTE Sep 30 '16 at 03:11
  • Official documentation on MSDN is here: https://msdn.microsoft.com/fr-fr/library/078sfkak.aspx, but nothing about character codes. – Laurent LAPORTE Sep 30 '16 at 03:37
  • I have found some references on this forum: https://social.msdn.microsoft.com/Forums/en-US/bd9079a0-58fc-40ac-8f7d-4669fcade7de/where-do-i-find-the-definition-of-the-getch-function-return-codes?forum=vclanguage – Laurent LAPORTE Sep 30 '16 at 03:43