Dealing with button input.
The Clicks framework is the preferred way to handle button interactions. It offers an API that is concerned with "clicks" instead of raw up & down button events. Each button has a ClickRecognizer, which is represented using the ClickRecognizerRef type, which are automatically created by the system. Each of these ClickRecognizers is a little state machine that processes the raw stream of up & down events for one of the buttons and based upon the raw, incoming events, it tries to distill and match the "click" events according the application-provided configuration.
There are a 4 types of "click" events:
The configuration of the click recognizers happens inside a callback function (of type ClickConfigProvider) that is associated with a Window using window_set_click_config_provider() or using window_set_click_config_provider_with_context().
For example, here, we first associate a click config provider callback with our window:
Then in the callback, we set our desired configuration for each button:
Then we implement the handlers for each configuration we have set up:
See templates/template_buttons/src/template_buttons.c for a complete example.
uint8_t click_number_of_clicks_counted | ( | ClickRecognizerRef | recognizer | ) |
Gets the click count. You can use this inside a click handler implementation to get the click count for multi_click and (repeated) click events.
recognizer | The click recognizer for which to get the click count |
ButtonId click_recognizer_get_button_id | ( | ClickRecognizerRef | recognizer | ) |
Gets the button identifier. You can use this inside a click handler implementation to get the button id for the click event.
recognizer | The click recognizer for which to get the button id that caused the click event |
typedef void(* ClickConfigProvider)(void *context) |
This callback gets called at times that a click recognizer has to be set up, for example when a new window comes into view. Subscribe to click events using window_single_click_subscribe() window_single_repeating_click_subscribe() window_multi_click_subscribe() window_long_click_subscribe() window_raw_click_subscribe() These subscriptions will get used by the click recognizers of each of the 4 buttons.
context | Pointer to application specific data, as configured using window_set_click_config_provider_with_context(), or defaults to the window if registered with window_set_click_config_provider(). |
typedef void(* ClickHandler)(ClickRecognizerRef recognizer, void *context) |
Function signature of the callback that handles a recognized click pattern.
recognizer | The click recognizer that detected a "click" pattern |
context | Pointer to application specified data as configured using window_set_click_config_provider_with_context() or as set manually in the ClickConfigProvider callback on the .context field of the ClickConfig. |
typedef void* ClickRecognizerRef |
Reference to opaque click recognizer When a ClickHandler callback is called, the recognizer that fired the handler is passed in.
enum ButtonId |
Button ID values.
Enumerator | |
---|---|
BUTTON_ID_BACK |
Back button. |
BUTTON_ID_UP |
Up button. |
BUTTON_ID_SELECT |
Select (middle) button. |
BUTTON_ID_DOWN |
Down button. |
NUM_BUTTONS |
Total number of buttons. |