Window.on_keyboard event and TextInput multiplexing problem

1,048 views
Skip to first unread message

Alexey Smishlayev

unread,
Apr 5, 2013, 4:27:15 AM4/5/13
to kivy-...@googlegroups.com
Hello!
I bind my handler function on the Window.on_keyboard event to perform special actions, but the problem is when the TextInput is focused, the text is entered into the TextInput field and also captured by on_keyboard handler, so an action occurs. What can I do, so that pressed keys are processed in only one way (either input in the TextInput and not recognized as hotkey, or (better) ignored in the TextInput and handled as a hotkey) ?

Regards,
Alexey

Akshay Arora

unread,
Apr 5, 2013, 5:15:37 AM4/5/13
to kivy-...@googlegroups.com
You could override `_keyboard_on_key_down` to handle raw input in your TextInput.

Best Regards



--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Ben Rousch

unread,
Apr 5, 2013, 6:58:20 AM4/5/13
to kivy-...@googlegroups.com
I don't remember where I grabbed this from, but it's an example of
filtering characters before they are shown in a TextInput, as Akshay
suggests.
--
Ben Rousch
bro...@gmail.com
http://clusterbleep.net/
textinput-filter.py

Alexey Smishlayev

unread,
Apr 5, 2013, 4:26:13 PM4/5/13
to kivy-...@googlegroups.com
Hello!
Thank you for the ideas! I think the proper place to catch unwanted keys is before they are handled to the TextInput, but I will keep that filtering option in mind.
Currently I don't understand how key press handling and propagating takes place. There is an example in the keyboard documentation, where the comment says:

        # Return True to accept the key. Otherwise, it will be used by
        # the system.
        return True

I will greatly appreciate if someone could explain what does mean "accept the key" and "will be used by the system".
I have tried
MyApp(App):
    on_start
(self):
       
self._app_window.bind(on_key_down=_on_key_down_handler)

def _on_key_down_handler(instance, key, scancode, codepoint, modifier):
   
return True # tried both True and False


But in both cases the text is captured in TextInput.
Did I misunderstood the comment from the documentation above, that there is an option to signal, that the key pressed to be ignored by the system?

Regards,
Alexey

mugwart

unread,
Jan 24, 2014, 12:09:51 PM1/24/14
to kivy-...@googlegroups.com
Hi Alexey,

I might be able to help out here, I was fighting this today, unable to get access to the buildin keyboard listener on TextInput - I took most of the code out and rebuilt it in my widget.
As stated the code is heavily lifted from TextInput class, (is this okay to do this?) it is also very dirty as used just for building, haven't adapted for osx yet but I have custom shortcuts!

Is this the correct way to do this? Will it slow the app down in anyway?

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):
   
    text
= StringProperty()
   
   
def __init__(self, **kwargs):
       
super(textInsert, self).__init__(**kwargs)
       
       
self.custom_insteresting_keys=["s","y","q","w"] ## replace with dic later
       
       
self.textin=TextInput(
                              multiline
=True,
                              focus
=True,
                              cursor
=True
                             
)
       
self.textin.bind(text=self.onText)
       
self.add_widget(self.textin,0)
       
       
########################################
        keyboard
= Window.request_keyboard(self._keyboard_released, self)
       
self._keyboard = keyboard
        keyboard
.bind(on_key_down=self._keyboard_on_key_down)
       
########################################
   
   
def _keyboard_released(self):self.focus = False
       
   
def _keyboard_on_key_down(self, window, keycode, text, modifiers):
       
       
print "KEYDOWN", window, keycode, text, modifiers

        is_shortcut
= (modifiers == ['ctrl'])
       
       
if keycode[1] in list(self.custom_insteresting_keys) and is_shortcut:
           
print "CUSTOM SHORT CUT HAS BEEN MET"
           
       
return True
   

   
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()'''

   
class ROOT(App):
   
def build(self):
       
return textInsert()

if __name__ == '__main__':ROOT().run()

Regards
M

Reply all
Reply to author
Forward
0 new messages