Andron2525
Andron2525

Reputation: 61

How to disable _on_keyboard_press() when a textinput is focused in kivy?

In kivy, can you disable the keyboard events when typing?

In my app, I press space to go to the next screen, so space triggers the removing of the current main widget, and the loading of a new one. I can also press 'E' to display a popup, child to the main widget, with text fields inside of it.

My issue is, when my popup appears and I type into its input fields, everytime I press space or E it still triggers the next screen/popup functions

For now I did a dirty workaround with a global "pause" bool variable but I feel I'm not doing this the right way! So is there a way to disable keyboard events when typing, or any other proper way of dealing with this?

(over-)simplified version of my code:

class affichage(GridLayout):
    def __init__(self, **kwargs):
        super(affichage, self).__init__(**kwargs)
        self.popup = EditPopup(self.reponse, self.on_VALIDERpop)
        Window.bind(on_key_down=self._on_keyboard_down)

    def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
        if(text == ' '):  
            self.nextScreen()
        if(text == 'e'):
            self.popupOpen()

    def popupOpen(self):
        self.popup.open()     
        

class EditPopup(Popup):
    titre = StringProperty("")
    detail = StringProperty("")
    def __init__(self,titre, detail, on_VALIDER, **kwargs):
        super(EditPopup, self).__init__(**kwargs)
        self.titre = titre
        self.detail = detail
        self.call_on_VALIDER=on_VALIDER        
    
    def send_new_details(self, nouvTitre, nouvDescr):
        self.call_on_VALIDER(self.titre,
                            nouvTitre,nouvDescr])

Upvotes: 2

Views: 853

Answers (1)

Andron2525
Andron2525

Reputation: 61

So as suggested, I used the unbind function and it works: I called Window.unbind(on_key_down=self._on_keyboard_down) at the opening of the popup and then I bound my keyboard back with Window.bind(on_key_down=self._on_keyboard_down) at the dismissing of the popup.

I had to rewrite popup.open() and popup.dismiss(), here's how I did it: (Note: we are inside EditPopup, a child class of kivy popup)

def open(*args):
    self = args[0]
    if len(args) == 2:
        self.to_rebind = args[1]
        Window.unbind(on_key_down=self.to_rebind)
    super(EditPopup,self).open()

def dismiss(self):
    if hasattr(self, 'to_rebind'):
        Window.bind(on_key_down=self.to_rebind)
    super(EditPopup,self).dismiss()

Upvotes: 1

Related Questions