Why do I get Pygame KEYUP event without releasing the key?

二次信任 提交于 2019-12-11 04:04:04

问题


Firstly, I'm a complete beginner, so I do not have any experience, I have however searched all the possible places these last 2 days for a resolve, and could not find it.

I'm using this on a Raspberry PI 3 with Raspbian.

I'm trying to build a simple code in Python 3.6 that will do the following: When pressing a keyboard key:

1.it should print 'press' if the key was pressed, without repeating.

(if the key is being held down, it should print 'press' only once and stop).

2.it should print 'release' if the key was released without repeating.

Basically I want to print once the last state of the key,

The problem I am having is:

while holding down the key, I'm getting consecutive press/release press/release press/release events, even if no key was physically released, instead of getting only 1 'press'.

Below is the code that I'm trying to use.

#!/usr/bin/env python
import pygame
from pygame.locals import *
from time import sleep
import time

pygame.init()
screen = pygame.display.set_mode((800,800))

keys= [False]
last = None
pygame.key.set_repeat()

while True:
        if keys[0]==True and last != 'press': 
            print ('press')
            last = 'press'

        if keys[0]==False and last != 'release':
            print('release')
            last = 'release'

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                exit(0)

            if event.type == pygame.KEYDOWN:
                if event.key==K_d:
                    keys[0]=True                        

            if event.type == pygame.KEYUP:
                if event.key==K_d:
                    keys[0]=False

回答1:


Problem solved, it was because I was using a VNC instead of using the keyboard directly connected to the Raspberry Pi.



来源:https://stackoverflow.com/questions/50434460/why-do-i-get-pygame-keyup-event-without-releasing-the-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!