Creating a new input event dispatcher in Pyglet (infra red input)

隐身守侯 提交于 2021-02-08 07:33:28

问题


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:


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.




回答2:


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



来源:https://stackoverflow.com/questions/1206628/creating-a-new-input-event-dispatcher-in-pyglet-infra-red-input

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!