3

pyglet 的文档中指出

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.

然而,当 Unity 的键盘设置称为“按住键时重复按键”时,pyglet (1.2alpha1) 将在按键保持按下时重复 on_key_press 和 on_key_release。

可以通过以下脚本切换设置来测试这种意外行为:

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() 

有没有办法覆盖单个窗口的键重复?也欢迎任何其他解决方法。

此设置默认开启,游戏要求将其关闭是不愉快的。

4

1 回答 1

3

一个简单的解决方法是:

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)
于 2014-08-09T17:47:26.730 回答