Ryan
Ryan

Reputation: 23

Im Trying to Detect a Certain Key being Pressed (Python)

This is My Code, I'm a beginner and I've been looking around I've looked at least 5-10 posts about this, they aren't really helping me because I can't understand the code.

In My code im trying a really basic starting screen for a game and what I want to do is how detect E or S being pressed at the time of the start screen,

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

Upvotes: 1

Views: 576

Answers (1)

Marcos Castro
Marcos Castro

Reputation: 41

I use getch, if you want to use, download py-getch:

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

the command to install: python setup.py install

Example code:

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

Upvotes: 1

Related Questions