Is there a pygame constant that checks when no buttons are being pressed?

Recommended Answers

I think this is, what you need:

Look at the comments, too.

Jump to Post

The pygame.KEYDOWN event figures this one out, just put it into an if statement.

Jump to Post

All 7 Replies

The pygame.KEYDOWN event figures this one out, just put it into an if statement.

When using the KEYDOWN thing, if i have 2 buttons pressed, then i release one of those with the other still being held down, the charactor just stops instead of doing the right stuff based on the button that is still held down :S

This behavior is consistent with most other applications I believe. It may have something to do with the way input is handled by the Operating System.

well is there any way to get the number of keys being held down?

I tried to make a function to figure this out but it never returns 0 :(

Heres the code:

def getNumKeysPressed():
    temp = 0
    for item in pygame.key.get_pressed():
        if item:
            temp += 1
    return temp

It should return 0 at some stage, because it refreshes everytime the function gets called ( temp = 0 ) which is everytime it goes around the loop.
Heres where it's called:

for event in pygame.event.get():
            val = getNumKeysPressed() ##Here
            if event.type == pygame.QUIT:
            if  event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                elif event.key == pygame.K_RIGHT:
                elif event.key == pygame.K_UP:
                elif event.key == pygame.K_DOWN:
            if val == 0:

All you help will and has been appreciated :)

Don't worry I fixed it, it turns out that when there is no buttons pressed, pygame.key.get_pressed() returns a list with one value in it, so my function returned a value of 1 :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.