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

Recommended Answers

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:
                pygame.quit()
                return
            if  event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    p.walk_left()
                elif event.key == pygame.K_RIGHT:
                    p.walk_right()
                elif event.key == pygame.K_UP:
                    p.walk_up()
                elif event.key == pygame.K_DOWN:
                    p.walk_down()
            if val == 0:
                p.reset()

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 developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.