Input subsystem¶
These are the devices that receive key commands from some external source and route them to your applications.
At the input system core, there’s InputListener
. It receives key events from drivers you use and routes them to currently active application.
Available input drivers:
InputListener¶
The i
variable you have supplied by main.py
load_app()
in your applications is an InputListener
instance. It’s operating on key names, such as “KEY_ENTER” or “KEY_UP”. You can assign callback once a keypress with a matching keyname is received, which is as simple as i.set_callback(key_name, callback)
.
You can also set a dictionary of "keyname":callback_function
mappings, this would be called a keymap
.
-
class
input.input.
InputListener
(drivers, keymap=None)[source]¶ A class which listens for input device events and calls corresponding callbacks if set
-
__init__
(drivers, keymap=None)[source]¶ Init function for creating KeyListener object. Checks all the arguments and sets keymap if supplied.
-
atexit
()[source]¶ Exits driver (if necessary) if something wrong happened or pyLCI exits. Also, stops the listener
-
check_special_callback
(key_name)[source]¶ Raises exceptions upon setting of a special callback on a reserved/taken keyname
-
event_loop
(index)[source]¶ Blocking event loop which just calls callbacks in the keymap once corresponding keys are received in the
self.queue
.
-
receive_key
(key)[source]¶ This is the method that receives keypresses from drivers and puts them into
self.queue
forself.event_loop
to receive
-
remove_streaming
()[source]¶ Removes a callback for streaming key events, if previously set by any app/UI element.
-
replace_keymap_entries
(keymap)[source]¶ Sets all the callbacks supplied, not removing previously set but overwriting those with same keycodes
-
set_keymap
(keymap)[source]¶ Sets all the callbacks supplied, removing the previously set keymap completely
-
set_maskable_callback
(key_name, callback)[source]¶ Sets a single maskable callback of the listener. Raises CallbackException if the callback is one of the reserved keys or already is in maskable/nonmaskable keymap.
A maskable callback is global (never cleared) and will be called upon a keypress unless a callback for the same keyname is already set in keymap.
-
set_nonmaskable_callback
(key_name, callback)[source]¶ Sets a single nonmaskable callback of the listener. Raises CallbackException if the callback is one of the reserved keys or already is in maskable/nonmaskable keymap.
A nonmaskable callback is global (never cleared) and will be called upon a keypress even if a callback for the same keyname is already set in
keymap
(callback from thekeymap
won’t be called).
-
Note
In v1.0 architecture, there’s a single InputListener
instance shared among all applications, so when you set some callbacks for your application and then exit it or execute your application’s menu element, there’s a very good chance your callbacks won’t be there anymore once you return.
You won’t need to think about it unless you’re setting InputListener
yourself - mostly it’s taken care of by UI objects, which set the keymaps themselves themselves (for example, Menu
UI element sets the callbacks each time a menu is activated and each time a menu element callback execution is finished (because a Menu
can’t be sure whatever got called by the callback didn’t set some of callbacks some other way, say, the element’s callback was activating a nested menu.)
If you do set callbacks/keymap yourself (very useful for making your own UI elements, or for applications needing custom keybindings), it’s important to remember that you need to stop InputListener
before and start it again afterwards, since the changes do not take place until that’s done. For example, this is how you would set your own callback:
i.stop_listen()
i.clear_keymap() #Useful because there might be callbacks left from whatever your function was called by
#... Set your callbacks
i.set_callback("KEY_ENTER", my_function)
i.listen()
Glue logic functions
Warning
Not for user interaction, are called by main.py
, which is pyLCI launcher.
-
input.input.
init
()[source]¶ This function is called by main.py to read the input configuration, pick the corresponding drivers and initialize InputListener.
It also sets
listener
globals ofinput
module with driver and listener respectively, as well as registerslistener.stop()
function to be called when script exits since it’s in a blocking non-daemon thread.