Kivy/キーボード入力で画面を遷移させるには?
解決したいこと
Enter が押されたら画面を遷移させたいです。
よろしくお願いします。
import kivy
kivy.require('1.0.8')
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.modules import keybinding
from kivy.properties import StringProperty, NumericProperty
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
import japanize_kivy
from kivy.lang import Builder
Builder.load_file('calculator.kv')
class MyKeyboardListener(Widget):
text = StringProperty()
screen_name = StringProperty()
def __init__(self, **kwargs):
super(MyKeyboardListener, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(
self._keyboard_closed, self, 'text')
if self._keyboard.widget:
# If it exists, this widget is a VKeyboard object which you can use
# to change the keyboard layout.
pass
self._keyboard.bind(on_key_down=self._on_keyboard_down)
def _keyboard_closed(self):
print('My keyboard have been closed!')
self._keyboard.unbind(on_key_down=self._on_keyboard_down)
self._keyboard = None
def _on_keyboard_down(self, keyboard, keycode, text, modifiers):
self.text = text
if keycode[1] == 'escape':
keyboard.release()
return True
class MainScreen(Screen):
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.add_widget(MyKeyboardListener(screen_name='main'))
def changer(self):
self.manager.current = 'sub'
class SubScreen(Screen):
def __init__(self, **kwargs):
super(SubScreen, self).__init__(**kwargs)
self.add_widget(MyKeyboardListener(screen_name='sub'))
class TestApp(App):
def __init__(self, **kwargs):
super(TestApp, self).__init__(**kwargs)
self.title = 'test'
def build(self):
# スクリーンに名前をつけて管理
self.sm = ScreenManager(transition=NoTransition())
self.sm.add_widget(MainScreen(name='main'))
self.sm.add_widget(SubScreen(name='sub'))
return self.sm
if __name__ == '__main__':
TestApp().run()
# from kivy.base import runTouchApp
# runTouchApp(MyKeyboardListener())
0