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:
JavaScript
x
10
10
1
class MyTextInput(TextInput):
2
def keyboard_on_key_down(self, window, keycode, text, modifiers):
3
"""Run when a key is pressed down"""
4
# Do backspace for single and ctrl+backspace for token
5
if keycode[1] == 'backspace':
6
if 'control' in modifiers:
7
pass # Delete previous token
8
9
return super(TabTextInput, self).keyboard_on_key_down(window, keycode, text, modifiers)
10
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:
JavaScript
1
16
16
1
def keyboard_on_key_down(self, window, keycode, text, modifiers):
2
"""Run when a key is pressed down"""
3
# Do backspace for single and ctrl+backspace for token
4
if keycode[1] == 'backspace':
5
if 'ctrl' in modifiers:
6
done = False
7
while not done:
8
self.do_backspace()
9
cc, cr = self.cursor
10
line = self._lines[cr]
11
if cc == 0 or line[cc-1] == ' ':
12
done = True
13
return True
14
15
return super(MyTextInput, self).keyboard_on_key_down(window, keycode, text, modifiers)
16