0

I am moving from one TextInput to another TextInput using enter key.
I want to move from last TextInput to Button using enter key .When I press again enter key then should be call root.abc() function.
Can someone tell me how to done it?

test.py

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (300, 100)

class User(Screen):

    def abc(self):
        print('Test')

class Test(App):

    def build(self):
        return self.root

if __name__ == '__main__':
    Test().run()

test.kv

User:
    BoxLayout:
        orientation: "vertical"

        TextInput:
            id:test1
            focus : True
            text: ' '
            width: 100
            multiline: False
            on_text_validate: test2.focus = True

        TextInput:
            id:test2
            text: ' '
            width: 100
            multiline: False
            on_text_validate: test3.focus = True

        Button:
            id:test3
            text: 'Ok'
            on_press : root.abc()
macson taylor
  • 171
  • 7
  • 21
  • Why do you want to call it from the last input? Why not the current Input? Is it necessary to use the input? The Input handles the keyboard event by default, supersaturating a key is a sign of a bad design, it has another bunch of keys at hand, why do not you use them? – eyllanesc Apr 09 '18 at 08:10
  • What thing do you want to move? Keep in mind that we are not in your head, be specific and clear. – eyllanesc Apr 09 '18 at 09:04
  • @eyllanesc Actually i was confused that how to `focus` button so i thought it would be done from last `TextInput` – macson taylor Apr 09 '18 at 09:17
  • @eyllanesc sorry for inconvenience.I want to `focus` Button from last TextInput using `enter` key after again press enter then function root.abc() should b call. – macson taylor Apr 09 '18 at 09:20
  • see https://stackoverflow.com/questions/17280341/how-do-you-check-for-keyboard-events-with-kivy might help ! – hchandad Apr 09 '18 at 19:13

1 Answers1

5

Here we are using a key down binding, check for button's focus and Enter key pressed.

Example

main.py

from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window
from kivy.properties import ObjectProperty

Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (300, 100)


class User(Screen):
    test3 = ObjectProperty(None)

    def __init__(self, **kwargs):
        super(User, self).__init__(**kwargs)
        Window.bind(on_key_down=self._on_keyboard_down)

    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if self.test3.focus and keycode == 40:  # 40 - Enter key pressed
            self.abc()

    def abc(self):
        print('Test')


class Test(App):

    def build(self):
        return self.root


if __name__ == '__main__':
    Test().run()

test.kv

#:kivy 1.10.0

User:
    test3: test3
    BoxLayout:
        orientation: "vertical"

        TextInput:
            id:test1
            focus : True
            text: ' '
            width: 100
            multiline: False
            on_text_validate: test2.focus = True

        TextInput:
            id:test2
            text: ' '
            width: 100
            multiline: False
            on_text_validate:
                test3.background_normal = ''
                test3.background_color = [0, 0, 1, 0.5]    # 50% translucent blue
                test3.focus = True

        Button:
            id:test3
            text: 'Ok'
            focus: False
            on_press : root.abc()

Output

Img01 - App Startup Img02 - test1-TextInput Img03 - test2-TextInput Img04 - Pressed Enter-OK Button

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • Do you know where I could find keycodes for other keys like backspace, space, etc.? I am finding different mappings, eg. enter=13 and so on elsewhere which isn't working – Sachit Nagpal Dec 07 '19 at 18:57