我想将热键添加到我的项目中。
例如:ctrl + S用于保存
我找到了一个小例子。但它只测试是否按下了一个键。 我怎么能检查按下多个按钮?
编辑:修饰符是一个空列表。我该如何使用修饰符?
这是我尝试过的代码:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.properties import StringProperty,ObjectProperty
from kivy.core.window import Window, Keyboard
from kivy.logger import Logger
class textInsert(FloatLayout):
def __init__(self, **kwargs):
super(textInsert, self).__init__(**kwargs)
text = StringProperty()
########################################
keyboard = Window.request_keyboard(self._keyboard_released, self)
keyboard.bind(on_key_down=self._keyboard_on_key_down)
########################################
#end def __init__
def _keyboard_released(self):
self.focus = False
def _keyboard_on_key_down(self, window, keycode, text, modifiers):
if keycode[1] in ["s","y","q","w"]:
print("its in the list")
return True
else:
print('returned false')
return False
class ROOT(App):
def build(self):
return textInsert()
if __name__ == '__main__':
ROOT().run()
答案 0 :(得分:1)
这是一个工作示例,如何处理修改后的字符。我知道这不是正确的解决方案,但它是有效的。如果您知道正确的方法,请在下面发布...
代码:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import StringProperty
from kivy.core.window import Window, Keyboard
from kivy.logger import Logger
class textInsert(FloatLayout):
def __init__(self, **kwargs):
super(textInsert, self).__init__(**kwargs)
self.super = []
text = StringProperty()
########################################
keyboard = Window.request_keyboard(self._keyboard_released, self)
keyboard.bind(on_key_down=self._keyboard_on_key_down, on_key_up=self._keyboard_released)
########################################
#end def __init__
def _keyboard_released(self, window, keycode):
self.super = []
def _keyboard_on_key_down(self, window, keycode, text, super):
if 'lctrl' in self.super and keycode[1] == 's':
Logger.info("Item saved, {}".format(self.super))
self.super = []
return False
elif 'lctrl' not in self.super and keycode[1] in ["lctrl"]:
self.super.append(keycode[1])
return False
else:
Logger.info("key {} pressed.".format(keycode))
return False
class ROOT(App):
def build(self):
return textInsert()
if __name__ == '__main__':
ROOT().run()
答案 1 :(得分:0)
在我的机器上,Control键未存储在generic
type element_type is private;
with procedure Print(Item : element_type);
package gen_pack is
...
end gen_pack;
列表中,但它具有特定的键码。在我的例子中,按Ctrl键时打印modifiers
的参数会产生类似的结果:
on_key_down
在我的kivy版本中,(<kivy.core.window.window_sdl2.WindowSDL object at 0x7fc094802360>, 305, 224, u'\u0131', [])
还有一个额外的参数,因此您可以根据您的版本进行调整。仅供参考,&#34; S&#34;关键是:
on_key_down
要绑定多个键,只需使用带有多个语句的条件。在我的情况下,我会这样做:
(<kivy.core.window.window_sdl2.WindowSDL object at 0x7fc094802360>, 115, 22, u's', [])