为什么在不释放密钥的情况下获得Pygame KEYUP事件? [英] Why do I get Pygame KEYUP event without releasing the key?
问题描述
首先,我是一个完整的初学者,所以我没有任何经验,但是我搜索了过去两天所有可能的地方以找到解决方法,但找不到它.
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.
我在带有Raspbian的Raspberry PI 3上使用它.
I'm using this on a Raspberry PI 3 with Raspbian.
我正在尝试在Python 3.6中构建一个简单的代码,该代码将执行以下操作: 按下键盘键时:
I'm trying to build a simple code in Python 3.6 that will do the following: When pressing a keyboard key:
1.如果按下该键,则应打印"press",而无需重复.
1.it should print 'press' if the key was pressed, without repeating.
(如果按住该键,则只应打印按"一次并停止).
2.如果已释放密钥而不重复,则应打印"release".
2.it should print 'release' if the key was released without repeating.
基本上我想一次打印密钥的最后状态,
Basically I want to print once the last state of the key,
我遇到的问题是:
在按住键的同时,即使没有实际释放键,我也要连续获得按下/释放按下/释放按下/释放事件,而不是仅获得1次按下".
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
推荐答案
问题解决了,这是因为我使用的是VNC而不是直接连接到Raspberry Pi的键盘.
Problem solved, it was because I was using a VNC instead of using the keyboard directly connected to the Raspberry Pi.
这篇关于为什么在不释放密钥的情况下获得Pygame KEYUP事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!