如何正确使用键盘绑定?

How to use keyboard bind correctly?

我想编写一个允许同时使用文本输入和键盘绑定的程序。但是当我单击文本输入小部件时,会发生错误。我如何重写代码以使其不存在? (问题与python kivy有关)

from kivy.app import App
from kivy.core.window import Window
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button

class MyApp(App):
    def build(self):   
        self.my_keyboard = Window.request_keyboard(self.my_keyboard_down, self.root)
        self.my_keyboard.bind(on_key_down=self.my_keyboard_down)
        
        widget = FloatLayout()
        
        text_input = TextInput(multiline=False)
        widget.add_widget(text_input)

        print_text = lambda arg: print(text_input.text)
        widget.add_widget(Button(on_press = print_text, size_hint = [0.2, 0.2], text = 'press pls'))

        return widget

    def my_keyboard_down(self, keyboard, keycode, text, modifiers):
        if keycode[1] == 'escape': quit()
        if keycode[1] == 'right': print('right')
        if keycode[1] == 'left': print('left')
if __name__ == '__main__':
    MyApp().run()

出于某种原因,当您单击 TextInput 时,TextInput 会调用您的 my_keyboard_down(),但没有任何参数。您可以通过使用变量 args 定义 my_keyboard_down() 来解决此问题:

# def my_keyboard_down(self, keyboard, keycode, text, modifiers):
def my_keyboard_down(self, *args):
    if len(args) == 0:
        return
    else:
        keyboard = args[0]
        keycode = args[1]
        text = args[2]
        modifiers = args[3]
    if keycode[1] == 'escape': quit()
    if keycode[1] == 'right': print('right')
    if keycode[1] == 'left': print('left')

我怀疑这是 TextInput 中的错误。