I'm trying to capture keyup and keydown events on Raspian, using VNC from Windows. I would expect the code below to acknowledge only when I first press the key and again when I release it; however, when I press a key, it repeatedly reports "1 down" "1 up" etc. The code below works with a local keyboard.

import pygame

width, height = 200, 200
pygame.display.set_mode((width, height))
start_key = pygame.K_1-1
key_state_dict = {}
key_state_dict = {i:-1 for i in range(1,9)}


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); #sys.exit() if sys is imported
        for i in range (1, 9):
            if event.type == pygame.KEYDOWN:
                if event.key == i+start_key:
                    print str(i)+" down"
                    key_state_dict[i] = 1
            if event.type == pygame.KEYUP:
                if event.key == i+start_key:
                   print str(i)+" up"
                   key_state_dict[i] = 0

The documentation I saw seems to suggest that the keyboard repeat rate is initially set to disabled (presumably the setting equals 0 {milliseconds})

From pygame 1.8 it says you can use pygame.key.get_repeat() - what does that indicate?

pygame.key.set_repeat(n) - where n is repeat interval in milliseconds - configures the setting; it might be worth using anyhow, even with the default supposedly being what you would want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.