Reputation: 1310
I have main Layout and some Popups, I want keys to do some functions when main Layout is focused, some another functions for one Popup when it's opened, some another for another Popup etc. How can I do that the best way?
Upvotes: 0
Views: 546
Reputation: 256
I have done something similar, however I did not use popups. My application has a ScreenManager with many Screens, each screen has sub-menu's and each sub-menu of each screen requires it's own key mappings.
To better handle the switching of these sets of key mappings, I created a KeyboardManager class and a KeyboardLayout class:
The KeyboardManager class has the keyboard_handler() function which you give to the Window. The KeyboardLayout class would contain a dictionary of all the bindings, i.e. {'a': a_key_callback, 'esc': esc_key_callback}
Each sub-menu in my app would have a KeyboardLayout instance, and in it I would define my key mappings into it's dictionary. When the sub-menu is brought up, I would attach the KeyboardLayout to the KeyboardManager (save a reference to the KeyboardLayout in the KeyboardManager). The KeyboardManager.keyboard_handler method then intercepts key events, looks up the function to be called in the currently attached KeyboardLayout and then calls that function. In the keyboard_handler you can decide what to do if a key mapping is not there.
In your case, you would need one KeyboardLayout for the main layout and one for the popup. When the popup is opened, attach it's KeyboardLayout to the KeyboardManager, when you leave the popup, attach the main layout's KeyboardLayout back to the KeyboardManager.
This is the method I applied and it worked very well for me. Very easy to manager once the classes are set up.
Upvotes: 1