使用msvcrt(在Windows中),如何捕获Enter键? [英] Using msvcrt (in Windows), how to catch Enter key?
问题描述
Windows XP专业版,Python 2.5.1
导入msvcrt
而True:
if msvcrt.kbhit() :
key = msvcrt.getch()
if key ==''输入''
做点什么
>
有没有办法按下''Enter''键?
谢谢,
Dick Moores
Windows XP Pro, Python 2.5.1
import msvcrt
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == ''Enter''
do something
Is there a way to catch the pressing of the ''Enter'' key?
Thanks,
Dick Moores
推荐答案
10月29日上午11点26分,Dick Moores< r ... @ rcblue.comwrote:
On Oct 29, 11:26 am, Dick Moores <r...@rcblue.comwrote:
Windows XP专业版,Python 2.5.1
导入msvcrt
而True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key ==''Enter''
做点什么
有没有办法赶上Enter键?
Windows XP Pro, Python 2.5.1
import msvcrt
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == ''Enter''
do something
Is there a way to catch the pressing of the ''Enter'' key?
是的。只需打开Python shell,看看当你按Enter键时`getch`或`getche`函数返回的是什么
:
Yes there is. Just open the Python shell and see what is being
returned by `getch` or `getche` functions when you press Enter:
>> import msvcrt
msvcrt.getch()
>>import msvcrt
msvcrt.getch()
''\ r''
还要尽量避免忙等待并在循环中调用msvcrt.kbhit
不睡觉声明。我不知道你的情况,但可能这个
应该足够了:
而True:
if msvcrt.getch ()==''\ r'':
...
fw
''\r''
Also try to avoid `busy waiting` and calling msvcrt.kbhit in a loop
without a sleep statement. I don''t know your case but probably this
should be enough:
while True:
if msvcrt.getch() == ''\r'':
...
fw
10月29日凌晨4:26,Dick Moores< r ... @ rcblue.comwrote:
On Oct 29, 4:26 am, Dick Moores <r...@rcblue.comwrote:
Windows XP Pro,Python 2.5。 1
导入msvcrt
而True:
if msvcrt.kbhit():
key = msvcrt.getch()
如果键==''输入''
做点什么
有没有办法按下''回车'键?
谢谢,
Dick Moores
Windows XP Pro, Python 2.5.1
import msvcrt
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == ''Enter''
do something
Is there a way to catch the pressing of the ''Enter'' key?
Thanks,
Dick Moores
让我们找出:
Let''s find out:
>> from msvcrt import getch
而True:
>>from msvcrt import getch
while True:
.... key = getch()
.... if key:print repr(key)+'','',
....
''p'',''r'',''e'','s'','s'','''','n'',''g'', ''''','e'','''','''','''','''','
'':'',' ''',''\''',
哎呀,我按下了回车键,它返回''\ r''。我想知道......
.... key = getch()
.... if key: print repr(key)+'','',
....
''p'', ''r'', ''e'', ''s'', ''s'', ''i'', ''n'', ''g'', '' '', ''e'', ''n'', ''t'', ''e'', ''r'',
'':'', '' '', ''\r'',
Gee, I pressed enter, and it returned ''\r''. I wonder...
>>导入msvcrt
而True:
>>import msvcrt
while True:
.... if msvcrt.kbhit():
.... key = msvcrt .getch()
.... if key ==''\ r'':
.... print" success!"
....
成功!
.... if msvcrt.kbhit():
.... key = msvcrt.getch()
.... if key == ''\r'':
.... print "success!"
....
success!
2007年10月29日上午04:29,菲利普Wasilewski写道:
At 04:29 AM 10/29/2007, Filip Wasilewski wrote:
> 10月29日上午11点26分,Dick Moores< r ... @ rcblue.comwrote:
>On Oct 29, 11:26 am, Dick Moores <r...@rcblue.comwrote:
Windows XP专业版,Python 2.5.1
导入msvcrt
而True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key ==''输入''
做点什么
有没有办法抓住压力输入键?
Windows XP Pro, Python 2.5.1
import msvcrt
while True:
if msvcrt.kbhit():
key = msvcrt.getch()
if key == ''Enter''
do something
Is there a way to catch the pressing of the ''Enter'' key?
是的。只需打开Python shell,然后按Enter键查看`getch`或`getche`函数返回的内容:
Yes there is. Just open the Python shell and see what is being
returned by `getch` or `getche` functions when you press Enter:
>导入msvcrt
msvcrt.getch()
>import msvcrt
msvcrt.getch()
''\ r''
''\r''
太棒了!谢谢。
Terrific! Thanks.
>还试着避免忙等待并在一个循环中调用msvcrt.kbhit
没有睡眠声明。
我不知道你的情况,但可能这个
应该足够了:
而True:
if msvcrt.getch()==''\ rr '':
>Also try to avoid `busy waiting` and calling msvcrt.kbhit in a loop
without a sleep statement.
I don''t know your case but probably this
should be enough:
while True:
if msvcrt.getch() == ''\r'':
我试过它并发现没有msvcrt.kbhit我点击的第一把钥匙
什么也没做。我必须再次点击那把钥匙或其他钥匙。
迪克
I tried it and find that without the msvcrt.kbhit the first key I hit
doesn''t do anything. I have to hit that key again, or another key.
Dick
这篇关于使用msvcrt(在Windows中),如何捕获Enter键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!