I am having a hard time changing between keyboard layouts in my app. In the below example I have two text fields and I want one to use a regular keyboard and the other to use a numberpad keyboard. When I click within one text field the keyboard changes, but focus is lost.
Is there a better way of changing a keyboard based on what field currently has focus?
from kivy.config import Config
Config.set('kivy', 'keyboard_mode', 'dock')
from kivy.app import App
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.clock import Clock
from kivy.properties import ObjectProperty
Window.size = (480, 600)
Window.borderless = False
KV = '''
AnchorLayout:
anchor_x: "center"
anchor_y: "center"
BoxLayout:
orientation: "vertical"
size_hint: 0.9, 0.5
MDTextField:
id: textField
hint_text: "Text field"
mode: "rectangle"
on_focus: app.changeKeyboard(textField, 'keyboard_text.json')
MDTextField:
id: numberField
hint_text: "Number field"
mode: "rectangle"
on_focus: app.changeKeyboard(textField, 'keyboard_number.json')
'''
class MyApp(MDApp):
textField = ObjectProperty()
numberField = ObjectProperty()
def build(self):
return Builder.load_string(KV)
def changeKeyboard(self, widget, layout):
self._keyboard = Window.request_keyboard(self._keyboard_closed, widget)
if self._keyboard.widget:
vkeyboard = self._keyboard.widget
vkeyboard.layout = layout
def _keyboard_closed(self):
print('My keyboard has been closed!')
self._keyboard = None
if __name__ == "__main__":
app = MyApp()
app.run()