0
我試圖檢測擊鍵在Python 3.X在Mac碼頭,這裏是我的代碼有Python 3.x密鑰檢測。 print語句來鍵後,檢測功能
import tty
import termios
import sys
def get_key():
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
def key_detect():
print("Key detect: ", end="")
print(get_key())
while True:
key_detect()
我想它的工作原理是:
Key detect:
並等到我按下某個東西,打印結果,並且應該等待下一次。就像這樣:
Key detect: a
Key detect:
但它是這樣的:
// A cursor flashes, but nothing has been printed
而當我追問了一句:
Key detect: a
*cursor*
或者關於Python 3.3及以上,則可以提供'沖水= TRUE'到'print'功能。 – user2357112
非常感謝它:) – terryy