Last active
July 21, 2017 19:01
-
-
Save Bakterija/bbe89c1b95857255e71abc215591783b to your computer and use it in GitHub Desktop.
Kivy on_textinput fix window patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.core.window.window_sdl2 import WindowSDL | |
from kivy.uix.floatlayout import FloatLayout | |
from kivy.properties import StringProperty | |
from kivy.core.window import Window | |
from kivy.app import runTouchApp | |
from kivy.logger import Logger | |
from kivy.lang import Builder | |
from kivy.clock import Clock | |
from time import time | |
K_TAB = 9 | |
def patch_window(): | |
def new_release_keyboard(self, *largs): | |
super(WindowSDL, self).release_keyboard(*largs) | |
# We comment this out make the event work again | |
# | |
# self._win.hide_keyboard() | |
self._sdl_keyboard = None | |
return True | |
WindowSDL.release_keyboard = new_release_keyboard | |
Logger.warning('window_patch: patched kivy Window.release_keyboard') | |
class RootWidget(FloatLayout): | |
scheduled_testing = False | |
lbltext = StringProperty('Text from Window.on_textinput event:\n') | |
last_text = StringProperty() | |
last_key = StringProperty() | |
instructions = StringProperty( | |
'What is this supposed to do?: \n' | |
'0: last_textinput label should be displaying your last input text\n' | |
'1: text from window widget should display all input text\n' | |
'\n\n' | |
'Instructions:\n\n0: Press a few letters, ' | |
'notice that everything works\n' | |
'1: Press TAB twice to toggle ' | |
'focus on TextInput widget\n' | |
'2: Press a few more letters, ' | |
'notice that sdl2 on_textinput event is no longer received\n' | |
'3: Press ctrl to apply window patch and toggle TextInput focus again\n' | |
'4: Press a few letters, notice that everything is fixed' | |
) | |
def __init__(self, **kwargs): | |
super(RootWidget, self).__init__(**kwargs) | |
Window.bind(on_textinput=self.on_textinput) | |
Window.bind(on_key_down=self.on_key_down) | |
def on_textinput(self, _, text): | |
Logger.info('RootWidget: on_textinput(%s)' % (text)) | |
self.lbltext += text | |
self.last_text = text | |
def on_key_down(self, _, k, *args): | |
Logger.info('RootWidget: on_key_down(%s)' % (k)) | |
self.last_key = str(k) | |
if k in (305, 306): | |
patch_window() | |
if k == K_TAB: | |
tw = self.ids.textinput | |
tw.focus = not tw.focus | |
runTouchApp(Builder.load_string(''' | |
<AppLabel@Label>: | |
markup: True | |
text_size: self.width, None | |
halign: 'left' | |
valign: 'top' | |
background_color: 0.2, 0.2, 0.2, 1 | |
canvas.before: | |
Color: | |
rgba: self.background_color | |
Rectangle: | |
pos: self.pos | |
size: self.size | |
RootWidget: | |
AppLabel: | |
size_hint: 1, 0.5 | |
pos: 0, root.top - self.height | |
text_size: self.size | |
text: root.instructions | |
AppLabel: | |
text: 'Last textinput: %s' % (root.last_text) | |
size_hint: 0.5, 0.1 | |
pos_hint: {'center_x': 0.25, 'center_y': 0.45} | |
background_color: 0.4, 0.2, 0.2, 1 | |
AppLabel: | |
text: 'Last key_down: %s' % (root.last_key) | |
size_hint: 0.5, 0.1 | |
pos_hint: {'center_x': 0.75, 'center_y': 0.45} | |
background_color: 0.2, 0.4, 0.2, 1 | |
AppLabel: | |
text: root.lbltext | |
size_hint: 1, 0.1 | |
pos_hint: {'center_x': 0.5, 'center_y': 0.35} | |
text_size: self.size | |
TextInput: | |
id: textinput | |
size_hint: 1, 0.3 | |
background_active: '' | |
background_normal: '' | |
background_disabled_normal: '' | |
background_color: (0.7, 0.3, 0.3, 1) if self.focus else (0.9, 0.9, 0.9, 1) | |
pos_hint: {'center_x': 0.5, 'center_y': 0.15} | |
hint_text: 'TextInput widget' | |
''')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment