From 87897c489d0797267892152f35c0e8cba34b222e Mon Sep 17 00:00:00 2001 From: Adachinski Date: Wed, 9 May 2018 03:59:18 +0900 Subject: [PATCH] Add on_textedit event to SDL2 Window (#5597) * Add on_textrdit event * Add on_textedit event * Add TextEdit Event * Add on_textedit sample --- examples/miscellaneous/on_textedit_event.py | 104 ++++++++++++++++++++ kivy/core/window/__init__.py | 19 +++- kivy/core/window/window_sdl2.py | 4 + 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 examples/miscellaneous/on_textedit_event.py diff --git a/examples/miscellaneous/on_textedit_event.py b/examples/miscellaneous/on_textedit_event.py new file mode 100644 index 000000000..c361f0774 --- /dev/null +++ b/examples/miscellaneous/on_textedit_event.py @@ -0,0 +1,104 @@ +# -*- coding: utf-8 -*- + +''' +on_textedit event sample. +''' +from kivy.app import App +from kivy.uix.widget import Widget +from kivy.lang import Builder +from kivy.properties import StringProperty +from kivy.core.text import LabelBase, DEFAULT_FONT +from kivy.uix.textinput import TextInput +from kivy.base import EventLoop + + +class TextInputIME(TextInput): + testtext = StringProperty() + + def __init__(self, **kwargs): + super(TextInputIME, self).__init__(**kwargs) + EventLoop.window.bind(on_textedit=self._on_textedit) + + def _on_textedit(self, window, text): + self.testtext = text + + +class MainWidget(Widget): + text = StringProperty() + + def __init__(self, **kwargs): + super(MainWidget, self).__init__(**kwargs) + self.text = '' + + def confim(self): + self.text = self.ids["text_box"].text + + def changeFont(self): + try: + LabelBase.register(DEFAULT_FONT, self.ids["text_font"].text) + except Exception: + self.ids["text_font"].text = "can't load font." + + +class TextEditTestApp(App): + def __init__(self, **kwargs): + super(TextEditTestApp, self).__init__(**kwargs) + + def build(self): + return MainWidget() + + +if __name__ == '__main__': + Builder.load_string(''' +: + BoxLayout: + orientation: 'vertical' + size: root.size + BoxLayout: + Label: + size_hint_x: 3 + text: "Multi language font file path" + TextInput: + id: text_font + size_hint_x: 5 + Button: + size_hint_x: 2 + text: "Change Font" + on_press: root.changeFont() + BoxLayout: + Label: + size_hint_x: 3 + text: "Text editing by IME" + Label: + size_hint_x: 7 + text:text_box.testtext + canvas.before: + Color: + rgb: 0.5765 ,0.5765 ,0.5843 + Rectangle: + pos: self.pos + size: self.size + BoxLayout: + Label: + size_hint_x: 3 + text: "Enter text ->" + TextInputIME: + id: text_box + size_hint_x: 7 + focus: True + BoxLayout: + Button: + size_hint_x: 3 + text: "Confirm text property" + on_press: root.confim() + Label: + size_hint_x: 7 + text: root.text + canvas.before: + Color: + rgb: 0.5765 ,0.5765 ,0.5843 + Rectangle: + pos: self.pos + size: self.size + ''') + TextEditTestApp().run() diff --git a/kivy/core/window/__init__.py b/kivy/core/window/__init__.py index f25e35873..e46ff062b 100755 --- a/kivy/core/window/__init__.py +++ b/kivy/core/window/__init__.py @@ -321,6 +321,13 @@ class WindowBase(EventDispatcher): You can listen to this one, and clean whatever you can. .. versionadded:: 1.9.0 + + `on_textedit(self, text)`: + Fired when inputting with IME. + The string inputting with IME is set as the parameter of + this event. + + .. versionadded:: 1.10.1 ''' __instance = None @@ -860,7 +867,8 @@ class WindowBase(EventDispatcher): 'on_key_down', 'on_key_up', 'on_textinput', 'on_dropfile', 'on_request_close', 'on_cursor_enter', 'on_cursor_leave', 'on_joy_axis', 'on_joy_hat', 'on_joy_ball', - 'on_joy_button_down', 'on_joy_button_up', 'on_memorywarning') + 'on_joy_button_down', 'on_joy_button_up', 'on_memorywarning', + 'on_textedit') def __new__(cls, **kwargs): if cls.__instance is None: @@ -1740,6 +1748,15 @@ class WindowBase(EventDispatcher): ''' pass + def on_textedit(self, text): + '''Event called when inputting with IME. + The string inputting with IME is set as the parameter of + this event. + + .. versionadded:: 1.10.1 + ''' + pass + @reify def dpi(self): '''Return the DPI of the screen. If the implementation doesn't support diff --git a/kivy/core/window/window_sdl2.py b/kivy/core/window/window_sdl2.py index b53e27695..e73d96066 100644 --- a/kivy/core/window/window_sdl2.py +++ b/kivy/core/window/window_sdl2.py @@ -669,6 +669,10 @@ class WindowSDL(WindowBase): text = args[0] self.dispatch('on_textinput', text) + elif action == 'textedit': + text = args[0] + self.dispatch('on_textedit', text) + # unhandled event ! else: Logger.trace('WindowSDL: Unhandled event %s' % str(event))