Last active
February 5, 2016 16:25
-
-
Save hardinkm/aaff021a5a78b6c76581 to your computer and use it in GitHub Desktop.
KIVY Python Issue with keyboard listener and control of text in TextInput object
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.app import App | |
from kivy.uix.floatlayout import FloatLayout | |
from kivy.uix.textinput import TextInput | |
from kivy.uix.widget import Widget | |
from kivy.properties import StringProperty,ObjectProperty | |
from kivy.core.window import Window, Keyboard | |
class textInsert(FloatLayout): | |
def __init__(self, **kwargs): | |
super(textInsert, self).__init__(**kwargs) | |
text = StringProperty() | |
self.custom_insteresting_keys=["s","y","q","w"] ## replace with dic later | |
######################################## | |
keyboard = Window.request_keyboard(self._keyboard_released, self) | |
self._keyboard = keyboard | |
keyboard.bind(on_key_down=self._keyboard_on_key_down) | |
######################################## | |
self.textin=TextInput( | |
size_hint_x=None, | |
size_hint_y=None, | |
height=30, | |
width=120, | |
multiline=False | |
) | |
self.textin.bind(text=self.onText) | |
self.add_widget(self.textin) | |
#end def __init__ | |
def _keyboard_released(self): | |
self.focus = False | |
#end def _keyboard_released | |
def _keyboard_on_key_down(self, window, keycode, text, modifiers): | |
is_shortcut = (modifiers == ['ctrl']) | |
print('keycode[1]', keycode[1]) | |
#if keycode[1] in list(self.custom_insteresting_keys) and is_shortcut: | |
if keycode[1] in list(self.custom_insteresting_keys): | |
print("CUSTOM SHORT CUT HAS BEEN MET, TEXT SHOULD NOT BE ENTERED INTO TEXTINPUT") | |
return True | |
else: | |
print('returned false') | |
return False | |
#end if | |
#end def _keyboard_on_key_down | |
def onText(self,instance,value): | |
print("ON TEXT", value) | |
''' | |
#print self.textin._key_down(self) | |
if len(value) == 3: | |
self.textin.select_text( | |
(self.textin.cursor_index()-len(value)), | |
self.textin.cursor | |
) | |
self.textin.delete_selection()''' | |
#end def onText | |
class ROOT(App): | |
def build(self): | |
return textInsert() | |
if __name__ == '__main__': | |
ROOT().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment