libtcod / python-tcod

A high-performance Python port of libtcod. Includes the libtcodpy module for backwards compatibility with older projects.
BSD 2-Clause "Simplified" License
413 stars 36 forks source link

Some keyboard events are lost #98

Closed Darky-Lucera closed 4 years ago

Darky-Lucera commented 4 years ago

When I changed from:

    tcod.console_is_key_pressed(tcod.KEY_UP):

to the new API:

for event in tcod.event.get():
    if isinstance(event, tcod.event.KeyboardEvent):
        keyEvents.append(event)

some of the keyboard events are lost.

To reproduce it just get the events and then put this code:

for keyEvent in keyEvents:
    if keyEvent.sym == tcod.event.K_UP:
        if isinstance(keyEvent, tcod.event.KeyDown):
            print(' 1')
        if isinstance(keyEvent, tcod.event.KeyUp):
            print('-1')

and play with the up key. Some KeyUp (-1) will be lost.

I'm working with Windows 10, python 3.7.8, tcod 11.15.3

Darky-Lucera commented 4 years ago

Note that with the old code I could move the character around with the direction keys without problem. Now, sometimes, the character keeps moving sometimes after the player releases the keys

HexDecimal commented 4 years ago

You'll need to clarify by showing the output of print(event). Repeat keys don't have a KeyUp event.

HexDecimal commented 4 years ago

To prevent lag issues you'll want to minimize or avoid calling the console_flush/context.present functions inside of the event loop. This is known to build up a long event buffer if the drawing routine is slow and if the window is redrawn for ignored events such as mouse motion.

Darky-Lucera commented 4 years ago

Oops! It was my fault, in a different point I consumed the event.

Sorry!