Skip to content
Advertisement

Kivy: Control + Backspace in TextInput to delete token

I’m trying to get my TextInput boxes to recognize a ctrl+backspace keystroke to backspace an entire token (like most modern programs).

So far, I have this:

class MyTextInput(TextInput):
    def keyboard_on_key_down(self, window, keycode, text, modifiers):
        """Run when a key is pressed down"""
        # Do backspace for single and ctrl+backspace for token
        if keycode[1] == 'backspace':
            if 'control' in modifiers:
                pass # Delete previous token

        return super(TabTextInput, self).keyboard_on_key_down(window, keycode, text, modifiers)

I’m not sure how to detect the previous token from the cursor and delete it without interfering with the existing behavior of the do_backspace() method.

Fyi, I am still using Kivy 1.11.1 and Python 3.7.

Advertisement

Answer

Something like this might work:

def keyboard_on_key_down(self, window, keycode, text, modifiers):
    """Run when a key is pressed down"""
    # Do backspace for single and ctrl+backspace for token
    if keycode[1] == 'backspace':
        if 'ctrl' in modifiers:
            done = False
            while not done:
                self.do_backspace()
                cc, cr = self.cursor
                line = self._lines[cr]
                if cc == 0 or line[cc-1] == ' ':
                    done = True
            return True

    return super(MyTextInput, self).keyboard_on_key_down(window, keycode, text, modifiers)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement