How to hold a ‘key down’ in Pygame?

I use Pygame 1.9.6 and Python 3.7.4. I want to hold down the space bar and it continuously does the same action over and over. I know how to have have the button get pressed with KEYDOWN. I looked at the question: How to efficiently hold a key in Pygame? for answers but can’t understand the one answer:

while not done: 
    keys = key.get_pressed() 
    if keys[K_DOWN]: 
        print "DOWN" 
    for e in event.get(): 
        pass # proceed other events. 
            # always call event.get() or event.poll() in the main loop

I don’t get the key.get_pressed(). It’s not from Pygame. Also, I’m assuming it’s a function they wrote, but this doesn’t show me on when I hold down a ‘Key’ it continues to run that action and when the ‘Key’ is released it stops the action being called. Any pointers on how to actually hold down a button or how to make one?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

pygame.key.get_pressed() is a function form pygame.key module. It returns a list of boolean values representing the state of every key on the keyboard.

If you want to test if the SPACE key is pressed the you have to get the state of K_SPACE by subscription:

keys = pygame.key.get_pressed() 
if keys[pygame.K_SPACE]:
    # [...]


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x