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

查看:157
本文介绍了如何处理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世界编写的代码.

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天全站免登陆