How To Handle Several Keys Pressed At The Same Time In Kivy?
Solution 1:
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
(Edit: 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)
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'.
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 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.
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.
Edit: 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
classMyApp(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 = Nonedef_on_keyboard_down(self, *args):
print'down', args
def_on_keyboard_up(self, *args):
print'up', args
defbuild(self):
self.moveable = Scatter()
self.moveable.add_widget( Label(text = 'Hello moving world!') )
return self.moveable
if __name__ == '__main__':
MyApp().run()
Post a Comment for "How To Handle Several Keys Pressed At The Same Time In Kivy?"