Tab/Enter (and other keystrokes) handling in Kivy's TextInput widgets

前端 未结 4 1913
夕颜
夕颜 2021-02-05 13:53

I\'m writing an app using Kivy framework and I stumbled upon a minor but annoying problem: I don\'t know how to handle Tab/Enter/Arrow keys in text fields so that press

4条回答
  •  感情败类
    2021-02-05 14:04

    [Insufficient points to just comment, so adding this here...]

    It's crucial to note that the keyboard NEXT behavior only works easily if the next field is managed by the same keyboard layout. However, an advanced app will have:

    • username (qwerty)
    • password (password)
    • ID (numeric) etc

    So the approaches above really doesn't work out.

    In the kv file:

        MyTextInput:
            next: idTheNextFieldBelowThis
    

    In your MyTextInput class:

        def insert_text(self, value, from_undo=False):
            #
            # Unfortunately the TextInput write_tab behavior only works if the next field is the same exact keyboard
            # type.
            #
            if not value[-1:] == '  ':
                return super(MyTextInput, self).insert_text(value, from_undo=from_undo)
            r = super(MyTextInput, self).insert_text(value[:-1], from_undo=from_undo)
            if self.next is not None:
                self.next.focus = True
            return r
    

提交回复
热议问题