感觉CTRL + X而raw_input>

时间:2016-05-14 00:04:32

标签: python bash

我有一个看起来像的脚本(我已经导入了所有必需的模块。):

def _find_getch():
try:
    import termios
except ImportError:
    # Non-POSIX. Return msvcrt's (Windows') getch.
    import msvcrt
    return msvcrt.getch

# POSIX system. Create and return a getch that manipulates the tty.
import sys, tty
def _getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    try:
        tty.setraw(fd)
        ch = sys.stdin.read(1)
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

return _getch

getch = _find_getch()
#keypress for CTRL+X is 88

if len(sys.argv) == 1:
    controlPressed = False
    file = ''
    while not controlPressed:
        latest = raw_input("> ")
        key = getch
        if key == '<function _getch at 0x7f9602558e60>':
            controlPressed = True
            sys.exit()

我在终端中运行此脚本,当我按 CTRL + X 时,它永远不会感觉到。当raw_input正在运行时,我如何感知 CTRL + X

1 个答案:

答案 0 :(得分:-1)

_getch功能无法正常工作。