0
파이썬에서 단일 키 누르기를 읽는 방법에 대해 많은 질문을 보았습니다. Windows의 경우 대답은 작동하는 msvcrt 모듈을 사용한다고 말합니다.Python 2.7 : Linux에서 단일 키 누르기를 읽는 방법 (특수 문자 포함)?
import termios, fcntl, sys, os
def kbhit():
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
while True:
try:
c = sys.stdin.read(1)
return True
except IOError:
return False
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
나이 :하지만 리눅스, 그들은이 같은 것을 사용
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
getch = _Getch()
그 두 가지의 문제들이 CNTRL-C와 같은 특수 문자를 읽을 수 없다는 것입니다
(^ C) 또는 화살표 키. 화살표 키와 같은 특수 문자를 포함하여 단일 키 누르기 을 읽는 Linux 방법이 있습니까?