Pyglets Window.on_key_press and Window.on_key_release affected by key repeat on ubuntu 12.10 with unity

StackOverflow https://stackoverflow.com/questions/14112309

  •  13-12-2021
  •  | 
  •  

Question

On pyglet's documentation it is stated that

The Window.on_key_press and Window.on_key_release events are fired when
any key on the keyboard is pressed or released, respectively. These events 
are not affected by "key repeat" -- once a key is pressed there are no more
events for that key until it is released.

However when the Unity's keyboard setting called "Key presses repeat when key is held down" is selected, pyglet (1.2alpha1) will repeat on_key_press and on_key_release when a key remains pressed.

This unintended behavior can be tested by the following script a toggling the setting:

import pyglet

window = pyglet.window.Window()

@window.event
def on_key_press(symbol, modifiers):
    print "key press"

@window.event
def on_key_release(symbol, modifiers):
    print "key release"

pyglet.app.run() 

Is there a way to override key repeat for a single window? Any other workaround is also welcome.

This setting is on by default and it is not pleasant for a game to request setting it off.

Was it helpful?

Solution

A simple workaround would be something like:

pressed_keys = []

@window.event
def on_key_press(symbol, modifiers):
    if symbol in pressed_keys:
        return
    # handle pressed key
    pressed_keys.append(symbol)

@window.event
def on_key_release(symbol, modifiers):
    if symbol in pressed_keys:
        pressed_keys.remove(symbol)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top