如何在 Kivy 中处理同时按下的多个键? [英] How to handle several keys pressed at the same time in Kivy?

查看:22
本文介绍了如何在 Kivy 中处理同时按下的多个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正计划用 kivy 做一个小型跨平台游戏,当我在测试从 pc 上的键盘获取输入的方式时,我有点惊讶.

I was planning on doing a little crossplatform game with kivy and, when I was testing the way to get input from the keyboard on the pc, I had a little surprise.

Kivy 似乎无法处理与其 on_keyboard_down 事件同时按下的多个键,当您在 kivy 中同时按下多个键时,官方文档中使用的键盘类会传递最后按下的键以更改所有正在按下的键.

Kivy doesn't seem to handle several keys pressed at same with it's on_keyboard_down events, when you press more than one key at same in kivy, the keyboard class used in the official documentation passes the last pressed key in change of all the keys being pressed at the moment.

看起来键盘类旨在让用户在应用程序中键入,因为当您按下一个键几秒钟时,第一个键事件和其余事件之间会有一点延迟(最后是每一步一个),就像您在输入时可以注意到的那样.

It looks like the keyboard class is designed to let the user type in the app because when you press a key for seconds, there's a little delay between the first key event and the rest of them (which are finally one per step), just like the one you can notice when typing.

这是我为键盘交互的 hello world 编写的代码.

This is the code I wrote for a keyboard-interactive hello world.

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.core.window import Window

class MyApp(App):
    def __init__(self, **kwargs):
        super(MyApp, self).__init__(**kwargs)
        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
        self._keyboard.bind(on_key_down = self._on_keyboard_down)

    def _keyboard_closed(self):
        self._keyboard.unbind(on_key_down = self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
        print str(keyboard)+' '+str(keycode[1])+' '+str(text)+' '+str(modifiers)
        if keycode[1] == 'w':
            self.moveable.y += 1

        if keycode[1] == 's':
            self.moveable.y -= 1

        if keycode[1] == 'd':
            self.moveable.x += 1

        if keycode[1] == 'a':
            self.moveable.x -= 1 


    def build(self):
        self.moveable = Scatter()
        self.moveable.add_widget( Label(text = 'Hello moving world!') )
        return self.moveable

if __name__ == '__main__':
    MyApp().run()

由于我之前写过的内容,这似乎不像我在视频游戏中使用的那种键盘输入,而且我还没有找到任何地方可以为此目的更好地使用它.

Because of what I wrote before, this doesn't seem like the kind of keyboard input I'd use for a video game, and I haven't found anywhere how to get it better for that purpose.

对不起,如果由于某种原因问题不合适,我试图找到答案,但我找不到,所以在这里.

Sorry if the question is inappropiate for some reason, I tried to find the answer to this, but I couldn't, so here it is.

如果您能提供帮助,请提前非常感谢您.

Thanks you a lot in advance if you can help with it.

推荐答案

看起来键盘类旨在让用户在应用程序中输入,因为当您按下一个键几秒钟时,第一个键事件和其余键事件之间会有一点延迟

It looks like the keyboard class is designed to let the user type in the app because when you press a key for seconds, there's a little delay between the first key event and the rest of them

(重读您的帖子后,我认为您根本不想更改此内容并且误解了如何使用键盘输入,请参阅本文的第二部分)

( Having reread your post, I think you don't want to change this at all and have misunderstood how to work with keyboard input, see the second part of this post)

我很确定这取决于操作系统设置,而不是 Kivy.如果您愿意,有很多关于在线更改此值的讨论,例如在 linux 上,您可能可以执行 xset r rate [delay] [rate],例如xset r rate 200 25,或者在各种桌面环境中都有一个 gui 设置.类似的事情在 Windows 上也是如此.关键字似乎是重复延迟".

I'm pretty sure this is down to operating system settings, not to do with Kivy. There's plenty of discussion about changing this value online if you want to, for instance on linux you can probably do xset r rate [delay] [rate], e.g. xset r rate 200 25, or in various desktop environments there's a gui setting for it. Similar things are true on windows. The keyword seems to be 'repeat delay'.

也就是说,我不确定为什么这真的很重要.Kivy 会告诉您何时按下该键以及何时释放该键,并且您可以在此期间执行任意次数的事件,如果您愿意的话.为什么操作系统是否不断向您发送额外的按键很重要?

That said, I'm not sure why this actually matters. Kivy tells you when the key is pressed and when it is released, and you can perform an event however often you like during that time if you want to. Why does it matter whether the os keeps sending you extra keypresses?

Kivy 似乎无法处理与其 on_keyboard_down 事件同时按下的多个键,当您在 kivy 中同时按下多个键时,官方文档中使用的键盘类会传递最后按下的键以更改所有正在按下的键.

Kivy doesn't seem to handle several keys pressed at same with it's on_keyboard_down events, when you press more than one key at same in kivy, the keyboard class used in the official documentation passes the last pressed key in change of all the keys being pressed at the moment.

我认为您可能误解了 Kivy 是如何向您呈现这些信息的.当一个键被按下时,kivy 会为你传递一个该键的 on_key_down 事件.当它被释放时,kivy 会为那个键传递一个 on_key_up 事件(你的程序不会对此做任何事情).在这些事件之间,您知道按键仍然被按下,系统是否继续为您提供假按键并不重要.

I think you might be misunderstanding how Kivy is presenting you this information. When a key is pressed, kivy passes you an on_key_down event for that key. When it is released, kivy passes an on_key_up event for that key (your program doesn't do anything with this). In between those events you know the key is still pressed, it doesn't matter whether the system keeps feeding you fake keypresses.

这是您的程序的修改版本,它也打印 on_key_up 事件信息:

Here is a modified version of your program that prints on_key_up event information as well:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scatter import Scatter
from kivy.core.window import Window

class MyApp(App):
    def __init__(self, **kwargs):
        super(MyApp, self).__init__(**kwargs)
        self._keyboard = Window.request_keyboard(self._keyboard_closed, self)
        self._keyboard.bind(on_key_down = self._on_keyboard_down)
        self._keyboard.bind(on_key_up = self._on_keyboard_up)

    def _keyboard_closed(self):
        self._keyboard.unbind(on_key_down = self._on_keyboard_down)
        self._keyboard = None

    def _on_keyboard_down(self, *args):
        print 'down', args

    def _on_keyboard_up(self, *args):
        print 'up', args


    def build(self):
        self.moveable = Scatter()
        self.moveable.add_widget( Label(text = 'Hello moving world!') )
        return self.moveable

if __name__ == '__main__':
    MyApp().run()

这篇关于如何在 Kivy 中处理同时按下的多个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆