コード例 #1
0
 def edit():
     'event loop for testing inputline'
     while True:
         # reads single chars, arrow keys won't work
         key = terminal.getchar()
         if key in printing_chars or key in command.keymap:
             command.handler(key)
         else:
             break
コード例 #2
0
ファイル: key.py プロジェクト: jon-jacky/Piety
    def __call__(self):
        """
        Get char from terminal, add to key sequence, check for complete sequence
        Return after each character.  If key sequence not complete, return ''
        If key sequence is complete, return the entire sequence

        CORRECTION: This version does *not* return after each char,
        it collects the entire key sequence and then returns.
        This version never returns ''.

        This version *blocks* while collecting multi-char sequences.
        Should be okay if multi-char sequences come from keyboard,
        but *not* okay for collecting emacs-style commands: M-x ... etc.

        This version *only* handles single character keys, and
        ANSI terminal codes of the form: csi + one character,
        like arrow keys: up esc[A, down esc[B, right esc[C, left esc[D

        This version stops collecting characters and returns sequence
        as soon as key sequence matches esc[c (for any c) 
        or prefix *fails* to match.  

        The handler must deal with unrecognized keys,
        including incomplete prefix sequences.
        """
        # To avoid blocking here, call when select (or...) says char is ready
        # We find that unfortunately select does *not* indicate when
        #  subsequent chars from keyboard key sequences are ready,
        #   so we read them here without returning.
        self.key += terminal.getchar()
        if self.key[-1] == keyboard.esc: # begin escape sequence
            self.key += terminal.getchar() # block waiting for next key
            if self.key[-1] == '[': # esc-[ is ansi ctrl seq introducer, csi
                self.key += terminal.getchar() # block waiting for next key
        # Here we have ansi controll sequence with one char: up esc[A etc.
        k = self.key 
        self.key = ''
        return k