I'm running into an issue with some pyglet code on Python 2.7.9 on Ubuntu 15.
While I can view the output of gamepad events (button A presses) when the pyglet window is absent, these events are not handled when the window is present.
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()
The above lines:
- Search through the list of attached devices,
- Finds the Logitech "f310", and the corresponding BTN_A control, then
- Attempts to "attach-send-dispatch" BTN_A events to the pyglet window such that the pyglet.app.run() event loop will detect BTN_A presses.
This works as expected when I comment out the line that draws the window, or when I close the window with the ESC key.
I know there's a way to attach BTN_A events such that they're detected by the pyglet.app.run()
event loop when the window is open, but my self.push_handlers(BTN_A)
attempt seems to have failed.
I did search through the related questions; the pyglet documentation on this issue was cryptic and assumed deep familiarity with event dispatchers. Although I tried the calls listed (set_handlers, push_handlers, etc) my syntax must have been wrong.