单击“TextInput”对象后,在Kivy中重新获得键盘焦点

时间:2018-03-21 06:50:03

标签: python textbox focus kivy keyboard-events

以下代码使用单个屏幕MainScreen创建Kivy应用,并显示LabelTextInput对象。

如果您在应用首次加载时提供一些键盘输入,它会在控制台中打印有关按下了哪个键的信息。但是,如果您再单击TextInput对象,键盘将重新聚焦,以便您可以键入TextInput对象。但是,一旦您单击TextInput对象,例如,在标签或背景上,按下更多键,相同的控制台打印操作不会执行;看起来键盘并没有散焦'来自TextInput对象。

单击TextInput对象后,如何重新获得键盘焦点?

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.textinput import TextInput

kv = '''ScreenManagement:
    MainScreen:

<MainScreen>:
    name: "main"
    BoxLayout:
        Label:
            text: "Label"
            font_size: 20
            size_hint: 0.2,0.1
        TextInput
            input_filter: 'float'
            font_size: 20
            hint_text: "Input Text"
            size_hint: 0.2,0.1'''

class MainScreen(Screen):

    def __init__(self, **kwargs):
        super().__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('INFO: The key', keycode, 'has been pressed')

        return True # return True to accept the key. Otherwise, it will be used by the system.

class ScreenManagement(ScreenManager):
    pass

class MainApp(App):
    def build(self):
        return Builder.load_string(kv)

if __name__ == "__main__":
    MainApp().run()

1 个答案:

答案 0 :(得分:1)

一种可能的解决方案是使用on_touch_down事件,并在按下不包含TextInput的部分时重新配置键盘:

class MainScreen(Screen):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.config_keyboard()

    def on_touch_down(self, touch):
        self.isTextInput = False

        def filter(widget):
            for child in widget.children:
                filter(child)
            if isinstance(widget, TextInput) and widget.collide_point(*touch.pos):
                self.isTextInput = True
                widget.on_touch_down(touch)

        filter(self)

        if not self.isTextInput and self._keyboard is None:
            self.config_keyboard()

    def config_keyboard(self):
        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('INFO: The key', keycode, 'has been pressed')

        return True # return True to accept the key. Otherwise, it will be used by the system.