Пример #1
0
def getch():
    """
    Wait for keypress, return character or a list of characters.

    Arrows and special keys generate a sequence of characters, so if there are
    extra symbols in input buffer, this function returns list.
    """
    # Credits: Danny Yoo, Python Cookbook
    ch = None
    morech = []
    try:
        if PY3K:
            from msvcrt import kbhit, getwch as _getch
        else:
            from msvcrt import kbhit, getch as _getch
        ch = _getch()
        # [ ] deal with buffered output - return when a key is
        #     recognized or scan code exceeds max len
        while kbhit():
            morech.append(_getch())
    except ImportError:
        ''' we're not on Windows, so we try the Unix-like approach '''
        import sys, tty, termios
        fd = sys.stdin.fileno()
        # save old terminal settings, because we are changing them
        old_settings = termios.tcgetattr(fd)
        try:
            # set terminal to "cbreak" mode, in which driver returns
            # one char at a time instead of one line at a time
            #
            # tty.setcbreak() is just a helper for tcsetattr() call, see
            # http://hg.python.org/cpython/file/c6880edaf6f3/Lib/tty.py
            tty.setcbreak(fd)
            ch = sys.stdin.read(1)


            # clear input buffer placing all available chars into morech
            newattr = termios.tcgetattr(fd)   # change terminal settings
                                              # to allow non-blocking read
            newattr[6][termios.VMIN] = 0      # CC structure
            newattr[6][termios.VTIME] = 0
            termios.tcsetattr(fd, termios.TCSANOW, newattr)

            morech = []
            while True:
                ch2 = sys.stdin.read(1)
                if ch2 != '':
                    morech.append(ch2)
                else:
                    break
        finally:
            # restore terminal settings. Do this when all output is
            # finished - TCSADRAIN flag
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

    if len(morech):
        return [ch] + morech

    return ch
Пример #2
0
 def readInput():
     start_time = time.time()
     while True:
         if kbhit():
             return _getch()
         if (time.time() - start_time) > timeout:
             return None
Пример #3
0
 def readInput():
     start_time = time.time()
     while True:
         if kbhit():
             return _getch()
         if (time.time() - start_time) > timeout:
             return None
Пример #4
0
 def getch():  # define non-Windows version
     ch = _getch()
     try:
         ch = ch.decode('utf-8')
     except UnicodeDecodeError:
         print("character can not be decoded, sorry!")
         ch = None
     return ch
Пример #5
0
def getch():
    if NT:
        # Any visible key is fellowed by an b'\x00'
        c = _getch()
        if c == b'\x00':
            c = _getch()

        # Some of the special keys like direction key
        # and Home, PgDn start with an b'\xe0'.
        if c == b'\xe0':
            c = _getch()
            # print(c, SPECIAL_KEY_MAP.get(c, b''))
            c = SPECIAL_KEY_MAP.get(c, b'')

        return c
    else:
        c = getch()
    return c
Пример #6
0
def wait_key(keys=None):
    ''' Waits for a keypress at the console and returns it.

        Arguments:
            keys - if passed, wait for this specific key, e.g. 'Q', 'ESC'.
                   may be a tuple.
        Returns:
            char or ESC - depending on key hit.
            None - immediately under i/o redirection, not an interactive tty.
    '''
    if is_a_tty():
        if keys:
            if not isinstance(keys, tuple):
                keys = (keys,)
            while True:
                key = _getch()
                if key in keys:
                    return key
        else:
            return _getch()
Пример #7
0
def getch():
    '''
    跨平台getch
    '''
    try:
        from msvcrt import getch as _getch
    except:
        from getch import getch as _getch
    temp = _getch()
    if type(temp) == bytes:
        temp = temp.decode('utf-8')
    print(temp)
    return temp
Пример #8
0
    def inkey(self, timeout=None):
        # Since get_key is a single method, we will just do the cbreak
        # and input stuff in a single method.
        # We should ever need inkey without cbreak

        if sys.version_info[0] >= 3:
            from msvcrt import kbhit, getwch as _getch
        else:
            from msvcrt import kbhit, getch as _getch

        def readInput():
            start_time = time.time()
            while True:
                if kbhit():
                    return _getch()
                if (time.time() - start_time) > timeout:
                    return None

        key = readInput()
        if not key:
            return None

        if key == '\x03':
            # Ctrl C
            raise CaughtSignal(2, None)

        elif key == '\x1c':
            # Ctrl \
            sys.exit(1)

        elif key == '\xe0':
            # Its an arrow key, get next symbol
            next_key = _getch()
            if next_key == 'H':
                return Val(name='KEY_UP', code=259)
            elif next_key == 'K':
                return Val(name='KEY_LEFT', code=260)
            elif next_key == 'P':
                return Val(name='KEY_DOWN', code=258)
            elif next_key == 'M':
                return Val(name='KEY_RIGHT', code=261)

        elif key == '\x1b':
            return Val(name='KEY_ESCAPE', code=361)
        elif key == '\x0d':
            return Val(name='KEY_ENTER', code=362)

        else:
            return Val(key=key)
Пример #9
0
    def inkey(self, timeout=None):
        # Since get_key is a single method, we will just do the cbreak
        # and input stuff in a single method.
        # We should ever need inkey without cbreak

        if sys.version_info[0] >= 3:
            from msvcrt import kbhit, getwch as _getch
        else:
            from msvcrt import kbhit, getch as _getch

        def readInput():
            start_time = time.time()
            while True:
                if kbhit():
                    return _getch()
                if (time.time() - start_time) > timeout:
                    return None

        key = readInput()
        if not key:
            return None

        if key == '\x03': # Ctrl C
            raise CaughtSignal(2, None)

        elif key == '\x1c': # Ctrl \
            sys.exit(1)

        elif key == '\xe0':
            # Its an arrow key, get next symbol
            next_key = _getch()
            if next_key == 'H':
                return Val(name='KEY_UP', code=259)
            elif next_key == 'K':
                return Val(name='KEY_LEFT', code=260)
            elif next_key == 'P':
                return Val(name='KEY_DOWN', code=258)
            elif next_key == 'M':
                return Val(name='KEY_RIGHT', code=261)

        elif key == '\x1b': # Esc
            return Val(name='KEY_ESCAPE', code=361)
        elif key == '\x0d': # Enter
            return Val(name='KEY_ENTER', code=362)

        else:
            return Val(key=key)
Пример #10
0
def not_escape_pressed():
    return kbhit() or _getch() != '\x1b'