在 Pyglet 中创建一个新的输入事件调度程序(红外输入)

发布于 2024-07-29 20:58:46 字数 943 浏览 3 评论 0原文

我最近在 pyglet-users 组中问了这个问题,但得到了回复,所以我在这里尝试。

我想扩展 Pyglet 以便能够使用 lirc 支持的红外输入设备。 我之前用过 pyLirc ( http://pylirc.mccabe.nu/ ) 和 PyGame ,我想要重写我的应用程序以使用 Pyglet 代替。

要查看是否按下了按钮,您通常会轮询 pyLirc 以查看其队列中是否有任何按钮按下。

我的问题是,Pyglet 中集成 pyLirc 的正确方法是什么?

我希望它的工作方式与当前窗口键盘/鼠标事件相同,但我不知道从哪里开始。

我知道我可以创建一个新的 EventDispatcher,在其中我可以注册 新类型的事件并在轮询后调度它们,如下所示:

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

但是,如果我使用 pyglet.app.run() ,如何将其集成到应用程序的主循环中以继续调用 poll() 以及如何将此事件调度程序附加到我的窗口所以它的工作方式与鼠标和键盘调度程序相同?

我看到我可以设置一个调度程序来使用 pyglet.clock.schedule_interval 定期调用 poll() ,但这是正确的方法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意犹 2024-08-05 20:58:46

对于OP来说可能为时已晚,但无论如何我都会回复,以防对其他人有帮助。

创建事件调度程序并使用 pyglet.clock.schedule_interval 定期调用 poll() 是一个好方法。

要将事件调度程序附加到窗口,您需要创建调度程序的实例,然后调用其 push_handlers 方法:

dispatcher.push_handlers(window)

然后您可以像对待进入窗口的任何其他事件一样对待这些事件。

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.

破晓 2024-08-05 20:58:46

正确的方法是任何有效的方法。 如果您找到更好的方法,以后随时可以更改它。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文