[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] BUG keyboard events



Hi, I got bug in pygame
If I change window resolution in keyboard handler then I got strange KEYUP event:
<Event(2-KeyDown {'scancode': 114, 'key': 275, 'unicode': u'', 'mod': 0})> (K_RIGHT)
<Event(3-KeyUp {'scancode': 0, 'key': 275, 'mod': 0})> (K_RIGHT) # HERE
<Event(3-KeyUp {'scancode': 114, 'key': 275, 'mod': 0})> (K_RIGHT)

but if I hit key very quickly, then I got that:
<Event(2-KeyDown {'scancode': 114, 'key': 275, 'unicode': u'', 'mod': 0})> (K_RIGHT)
<Event(3-KeyUp {'scancode': 0, 'key': 275, 'mod': 0})> (K_RIGHT)

And sometimes pygame stops to receive any keyboard events, but mouse and all other is ok (ALT + F4 still works)

To check bug try to press "a", "s", "d", "right" at the same time few times, after few tries pygame stops to receive any keyboard events.

My system:
$ uname -a
Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.57-3 x86_64 GNU/Linux

$ cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 7 (wheezy)"
NAME="Debian GNU/Linux"
VERSION_ID="7"
VERSION="7 (wheezy)"
ID=debian
ANSI_COLOR="1;31"
HOME_URL="http://www.debian.org/";
SUPPORT_URL="http://www.debian.org/support/";
BUG_REPORT_URL="http://bugs.debian.org/";

$ python
Python 2.7.3 (default, Mar 13 2014, 11:03:55) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>   import sys, pygame
>>>   sys.version
'2.7.3 (default, Mar 13 2014, 11:03:55) \n[GCC 4.7.2]'
>>>   pygame.ver
'1.9.1release'
#! /usr/bin/env python2
# -*- coding: utf-8 -*-

import pygame, random, time

def random_set_mode():
    w = random.randint(128, 640)
    h = random.randint(128, 640)
    return pygame.display.set_mode((w, h), 0, 32)

def draw_test():
    w, h = screen.get_size()
    d = lambda x1, y1, x2, y2: pygame.draw.line(screen, (255, 255, 255), (x1 * w, y1 * h), (x2 * w, y2 * h))

    # T
    d(0.006, 0.033, 0.25,  0.033)
    d(0.125, 0.033, 0.125, 0.40)

    # E
    d(0.3, 0.033, 0.3,  0.383)
    d(0.3, 0.033, 0.46, 0.033)
    d(0.3, 0.183, 0.46, 0.183)
    d(0.3, 0.383, 0.46, 0.383)

    # S
    pl = []
    for x, y in (0.71, 0.05), (0.625, 0.016), (0.55, 0.0833), (0.6, 0.18), (0.7125, 0.266), (0.6875, 0.366), (0.6375, 0.383), (0.5375, 0.35):
        pl.append((x * w, y * h))

    pygame.draw.lines(screen, (255, 255, 255), False, pl)

    # T
    d(0.7625, 0.030, 0.9875, 0.030)
    d(0.875,  0.030, 0.875,  0.383)

def eat_cpu():
    for i in xrange(1048576):
        pass

def handle_keydown(k):
    global screen
    global update_request

    if k == pygame.K_RIGHT:
        eat_cpu()
        screen = random_set_mode()
        update_request = True

def get_key_info(k):
    for i in dir(pygame):
        if i.startswith('K_'):
            c = pygame.__getattribute__(i)
            if k == c:
                return i

    return 'Unknown key'

pygame.display.init()
pygame.display.set_caption('Keyboard bug')
screen = random_set_mode()
update_request = True
working = True

while 1:
    while 1:
        e = pygame.event.poll()
        if e.type == pygame.NOEVENT: break

        if e.type == pygame.KEYDOWN or e.type == pygame.KEYUP:
            print('%s (%s)' % (str(e), get_key_info(e.key)))
        else:
            print(e)

        if e.type == pygame.QUIT:
            working = False
            break

        if e.type == pygame.KEYDOWN:
            if e.key == pygame.K_ESCAPE:
                working = False
                break

            handle_keydown(e.key)

    if not working: break

    if update_request:
        update_request = False
        screen.fill((0, 0, 0))
        draw_test()
        pygame.display.update()

    else:
        time.sleep(0.01)