我试图检测某个键被按下(Python)

时间:2015-10-03 00:05:14

标签: python key detect

这是我的代码,我是初学者,我一直在环顾四周,我至少看过5-10个帖子,他们并没有真正帮助我,因为我可以&我#39;理解代码。

在我的代码中我尝试了一个非常基本的游戏开始画面,我想要做的是如何在开始画面时检测到E或S被按下,

print (" _____________________________________________ ")
print ("|                                             |")
print ("|                                             |")
print ("|          The Chronicles                    |")
print ("|                Of Game                      |")
print ("|                                             |")
print ("|                                             |")
print ("|                                             |")
print ("|                                             |")
print ("|_____________________________________________|")
print ("|                                             |")
print ("|       START             EXIT                |")
print ("|        (S)               (E)                |")
print (" \___________________________________________/ ")

1 个答案:

答案 0 :(得分:1)

我使用getch,如果你想使用,请下载py-getch:

https://github.com/joeyespo/py-getch

安装命令:python setup.py install

示例代码:

from getch import getch, pause

# game loop
while(True):
    key = getch()
    if(key == 'S'):
        print('key S pressed...')
    elif(key == 'E'):
        print('key E pressed...')

key pressed