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

Viewed 16439

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 pressing either of them would dispatch an event, eg. switch the focus (jump) to another TextInput or launch something like send_form()

Could anyone please shed some light on this issue?

4 Answers

[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
Related