我在 Ubuntu 15 上的 Python 2.7.9 上遇到了一些 pyglet 代码的问题。
虽然我可以在 pyglet 窗口不存在时查看游戏手柄事件(按下按钮 A)的输出,但在窗口存在时不会处理这些事件。
import pyglet
from pprint import pprint
class example_class(pyglet.window.Window):
def __init__(self):
self.bEnd = False
pyglet.window.Window.__init__(self, width=100, height=100)
devices = pyglet.input.get_devices()
gamepad = []
for i in range(0,len(devices)):
if "F310" in devices[i].name:
gamepad = devices[i]
controls = gamepad.controls
gamepad.open()
for i in range(0,len(controls)):
if(("BTN_A" in controls[i].raw_name) |
("Button 0" in controls[i].raw_name)):
BTN_A = controls[i]
@BTN_A.event
def on_press(): # define the event handler
print('BTN_A event handler')
self.bEnd = True
# Push the handlers from BTN A to the window
self.push_handlers(BTN_A)
print(self.__dict__['_event_handlers'])
# 1 ms timing
pyglet.clock.schedule_interval(self.update,.001)
#self.on_press = on_press
return
def on_press():
self.bEnd=True
print('occurred')
# This on_mouse_press event handler is inherited from the window class
def on_mouse_press(self, x, y, dx, dy):
self.bEnd = True
print('mouse press')
def update(self,dt):
if self.bEnd==True:
pyglet.app.exit()
return
def run(self):
print('run')
for i in range(10):
print('%d' % i)
self.bEnd = False
pyglet.app.run()
ex = example_class()
ex.run()
以上几行:
- 搜索连接的设备列表,
- 找到罗技“f310”,以及对应的 BTN_A 控件,然后
- 尝试将 BTN_A 事件“附加-发送-调度”到 pyglet 窗口,以便 pyglet.app.run() 事件循环将检测 BTN_A 按下。
当我注释掉绘制窗口的行或使用 ESC 键关闭窗口时,这将按预期工作。
我知道有一种方法可以附加 BTN_A 事件,以便pyglet.app.run()
在窗口打开时被事件循环检测到,但我的self.push_handlers(BTN_A)
尝试似乎失败了。
我确实搜索了相关问题;关于这个问题的 pyglet 文档是神秘的,并且假定对事件调度程序非常熟悉。尽管我尝试了列出的调用(set_handlers、push_handlers 等),但我的语法一定是错误的。