使用带有pyglet的游戏手柄:窗口存在时未记录的事件

时间:2015-08-19 22:55:47

标签: python pyglet gamepad

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

以上几行:

  • 搜索附加设备列表
  • 找到Logitech" f310"以及相应的BTN_A控件,然后
  • 尝试" attach-send-dispatch" BTN_A事件到pyglet窗口,这样pyglet.app.run()事件循环将检测BTN_A按下。

当我注释掉绘制窗口的行,或者当我用ESC键关闭窗口时,这可以正常工作。

我知道有一种方法可以附加BTN_A事件,以便在窗口打开时pyglet.app.run()事件循环检测到它们,但我的self.push_handlers(BTN_A)尝试似乎失败了

我确实在搜索相关问题;关于这个问题的pyglet文档很神秘,并且对事件调度程序非常熟悉。虽然我尝试了列出的调用(set_handlers,push_handlers等),但我的语法肯定是错误的。

0 个答案:

没有答案