tags:

views:

199

answers:

2

I recently asked this question in the pyglet-users group, but got response, so I'm trying here instead.

I would like to extend Pyglet to be able to use an infra red input device supported by lirc. I've used pyLirc before ( http://pylirc.mccabe.nu/ ) with PyGame and I want to rewrite my application to use Pyglet instead.

To see if a button was pressed you would typically poll pyLirc to see if there is any button presses in its queue.

My question is, what is the correct way in Pyglet to integrate pyLirc?

I would prefer if it works in the same was as the current window keyboard/mouse events, but I'm not sure where to start.

I know I can create a new EventDispatcher, in which I can register the new types of events and dispatch them after polling, like so:

class pyLircDispatcher(pyglet.event.EventDispatcher):
    def poll(self):
        codes = pylirc.nextcode()
        if codes is not None:
            for code in codes:
                self.dispatch_event('on_irbutton', code)

    def on_irbutton(self, code):
        pass

But how do I integrate that into the application's main loop to keep on calling poll() if I use pyglet.app.run() and how do I attach this eventdispatcher to my window so it works the same as the mouse and keyboard dispatchers?

I see that I can set up a scheduler to call poll() at regular intervals with pyglet.clock.schedule_interval, but is this the correct way to do it?

+1  A: 

The correct way is whatever works. You can always change it later if you find a better way.

Vezquex
+1  A: 

It's probably too late for the OP, but I'll reply anyway in case it's helpful to anyone else.

Creating the event dispatcher and using pyglet.clock.schedule_interval to call poll() at regular intervals is a good way to do it.

To attach the event dispatcher to your window, you need to create an instance of the dispatcher and then call its push_handlers method:

dispatcher.push_handlers(window)

Then you can treat the events just like any other events coming into the window.

Kiv