Skip to content

Commit

Permalink
Reformatting input device documentation a little bit, tweaking docstr…
Browse files Browse the repository at this point in the history
…ings
  • Loading branch information
CRImier committed Apr 11, 2018
1 parent 4451faa commit 1665bdc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
27 changes: 8 additions & 19 deletions docs/input.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,23 @@ Available input drivers:
* :ref:`input_adafruit`
* :ref:`input_pi_gpio`

=============
InputListener
=============
==========
InputProxy
==========

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``.
The ``i`` variable you have supplied by ``main.py`` ``load_app()`` in your applications is an ``InputProxy`` 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**.


.. automodule:: input.input

.. autoclass:: InputListener
.. autoclass:: InputProxy
:members:
:special-members:

.. note::
Sample usage - it should rarely be necessary for you to set callbacks directly, as this
is mostly taken care of by the UI elements.

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:

.. code-block:: python
i.stop_listen()
Expand All @@ -43,12 +38,6 @@ If you do set callbacks/keymap yourself (very useful for making your own UI elem
i.set_callback("KEY_ENTER", my_function)
i.listen()
.. rubric:: Glue logic functions

.. warning:: Not for user interaction, are called by ``main.py``, which is ZPUI launcher.

.. autofunction:: init

========
Drivers:
========
Expand Down
14 changes: 9 additions & 5 deletions input/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,27 @@ def __init__(self, context_alias):
self.context_alias = context_alias

def set_streaming(self, callback):
"""Sets a callback for streaming key events. This callback will be called
"""
Sets a callback for streaming key events. This callback will be called
each time a key is pressed that doesn't belong to one of the three keymaps.
The callback will be called with key_name as first argument but should
support arbitrary number of keyword arguments if compatibility with
future versions is desired. (basically, add **kwargs to it).
future versions is desired. (basically, add ``**kwargs`` to it).
If a callback was set before, replaces it. The callbacks set will not be
restored after being replaced by other callbacks. Care must be taken to
make sure that the callback is only executed when the app or UI element
that set it is active."""
that set it is active.
"""
self.streaming = callback

def remove_streaming(self):
"""Removes a callback for streaming key events, if previously set by any
"""
Removes a callback for streaming key events, if previously set by any
app/UI element. This is more of a convenience function, to avoid your
callback being called when your app or UI element is not active."""
callback being called when your app or UI element is not active.
"""
self.streaming = None

def set_callback(self, key_name, callback):
Expand Down

0 comments on commit 1665bdc

Please sign in to comment.