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
Problem solved, it was because I was using a VNC instead of using the keyboard directly connected to the Raspberry Pi.