如何在 Python 中读取键盘输入
How can I read keyboard input in Python
我在 Python 中遇到键盘输入问题。我试过 raw_input 并且只调用了一次。但我想在每次用户按下任意键时读取键盘输入。我该怎么做?感谢您的回答。
在python2.x中,简单地使用无限while
循环和条件break
:
In [11]: while True:
...: k = raw_input('> ')
...: if k == 'q':
...: break;
...: #do-something
> test
> q
In [12]:
例如,您有这样的 Python 代码:
file1.py
#!/bin/python
... do some stuff...
并且在文档的某个位置您希望始终检查输入:
while True:
input = raw_input(">>>")
... do something with the input...
那将一直等待输入。您可以将该无限循环作为一个单独的进程进行线程处理,同时执行其他操作,以便用户输入可以对您正在执行的任务产生影响。
如果您只想在按下某个键时请求输入,并以循环方式执行此操作,则使用此代码(取自 this ActiveState recipe by Steven D'Aprano)您可以等待按键按下,并且然后请求输入,执行任务并 return 回到之前的状态。
import sys
try:
import tty, termios
except ImportError:
# Probably Windows.
try:
import msvcrt
except ImportError:
# FIXME what to do on other platforms?
# Just give up here.
raise ImportError('getch not available')
else:
getch = msvcrt.getch
else:
def getch():
"""getch() -> key character
Read a single keypress from stdin and return the resulting character.
Nothing is echoed to the console. This call will block if a keypress
is not already available, but will not wait for Enter to be pressed.
If the pressed key was a modifier key, nothing will be detected; if
it were a special function key, it may return the first character of
of an escape sequence, leaving additional characters in the buffer.
"""
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
那么如何处理呢?好吧,现在每次您想等待按键时只需调用 getch()
即可。就像这样:
while True:
getch() # this also returns the key pressed, if you want to store it
input = raw_input("Enter input")
do_whatever_with_it
您也可以同时执行其他任务。
请记住,Python 3.x 不再使用 raw_input,而是简单地使用 input()。
我在 Python 中遇到键盘输入问题。我试过 raw_input 并且只调用了一次。但我想在每次用户按下任意键时读取键盘输入。我该怎么做?感谢您的回答。
在python2.x中,简单地使用无限while
循环和条件break
:
In [11]: while True:
...: k = raw_input('> ')
...: if k == 'q':
...: break;
...: #do-something
> test
> q
In [12]:
例如,您有这样的 Python 代码:
file1.py
#!/bin/python
... do some stuff...
并且在文档的某个位置您希望始终检查输入:
while True:
input = raw_input(">>>")
... do something with the input...
那将一直等待输入。您可以将该无限循环作为一个单独的进程进行线程处理,同时执行其他操作,以便用户输入可以对您正在执行的任务产生影响。
如果您只想在按下某个键时请求输入,并以循环方式执行此操作,则使用此代码(取自 this ActiveState recipe by Steven D'Aprano)您可以等待按键按下,并且然后请求输入,执行任务并 return 回到之前的状态。
import sys try: import tty, termios except ImportError: # Probably Windows. try: import msvcrt except ImportError: # FIXME what to do on other platforms? # Just give up here. raise ImportError('getch not available') else: getch = msvcrt.getch else: def getch(): """getch() -> key character Read a single keypress from stdin and return the resulting character. Nothing is echoed to the console. This call will block if a keypress is not already available, but will not wait for Enter to be pressed. If the pressed key was a modifier key, nothing will be detected; if it were a special function key, it may return the first character of of an escape sequence, leaving additional characters in the buffer. """ 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
那么如何处理呢?好吧,现在每次您想等待按键时只需调用 getch()
即可。就像这样:
while True:
getch() # this also returns the key pressed, if you want to store it
input = raw_input("Enter input")
do_whatever_with_it
您也可以同时执行其他任务。
请记住,Python 3.x 不再使用 raw_input,而是简单地使用 input()。