Pebble Foundation Classes  0.2.0
C++ for Pebble
Clicks

Handling button click interactions. More...

Collaboration diagram for Clicks:

Classes

class  Pebble::ClickRecognizer
 
class  Pebble::ClickConfig
 

Typedefs

typedef void * ClickRecognizerRef
 
typedef void(* ClickHandler) (ClickRecognizerRef recognizer, void *context)
 
typedef void(* ClickConfigProvider) (void *context)
 

Enumerations

enum  ButtonId {
  BUTTON_ID_BACK = 0, BUTTON_ID_UP, BUTTON_ID_SELECT, BUTTON_ID_DOWN,
  NUM_BUTTONS
}
 

Functions

uint8_t click_number_of_clicks_counted (ClickRecognizerRef recognizer)
 
ButtonId click_recognizer_get_button_id (ClickRecognizerRef recognizer)
 
bool click_recognizer_is_repeating (ClickRecognizerRef recognizer)
 

Detailed Description

Handling button click interactions.

Each Pebble window handles Pebble's buttons while it is displayed. Raw button down and button up events are transformed into click events that can be transferred to your app:

To receive click events when a window is displayed, you must register a ClickConfigProvider for this window with AppWindow::SetClickConfigProvider(). Your ClickConfigProvider will be called every time the window becomes visible with one context argument.

In your ClickConfigProvider you call the window_single_click_subscribe(), window_single_repeating_click_subscribe(), window_multi_click_subscribe(), window_long_click_subscribe() and window_raw_click_subscribe() functions to register a handler for each event you wish to receive.

For convenience, click handlers are provided with a ClickRecognizerRef and a user-specified context.

The ClickRecognizerRef can be used in combination with click_number_of_clicks_counted(), click_recognizer_get_button_id() and click_recognizer_is_repeating() to get more information about the click. This is useful if you want different buttons or event types to share the same handler.

The user-specified context is the context of your ClickConfigProvider (see above). By default it points to the window. You can override it for all handlers with window_set_click_config_provider_with_context() or for a specific button with window_set_click_context().

User interaction in watchfaces

Watchfaces cannot use the buttons to interact with the user. Instead, you can use the AccelerometerService.

About the Back button

By default, the Back button will always pop to the previous window on the Window Stack (and leave the app if the current window is the only window). You can override the default back button behavior with window_single_click_subscribe() and window_multi_click_subscribe() but you cannot set a repeating, long or raw click handler on the back button because a long press will always terminate the app and return to the main menu.

See also

Refer to the (chapter "Clicks") for a conceptual overview of clicks and relevant code examples.

Each Pebble window handles Pebble's buttons while it is displayed. Raw button down and button up events are transformed into click events that can be transferred to your app:

To receive click events when a window is displayed, you must register a ClickConfigProvider for this window with window_set_click_config_provider(). Your ClickConfigProvider will be called every time the window becomes visible with one context argument. By default this context is a pointer to the window but you can change this with window_set_click_config_provider_with_context().

In your ClickConfigProvider you call the window_single_click_subscribe(), window_single_repeating_click_subscribe(), window_multi_click_subscribe(), window_long_click_subscribe() and window_raw_click_subscribe() functions to register a handler for each event you wish to receive.

For convenience, click handlers are provided with a ClickRecognizerRef and a user-specified context.

The ClickRecognizerRef can be used in combination with click_number_of_clicks_counted(), click_recognizer_get_button_id() and click_recognizer_is_repeating() to get more information about the click. This is useful if you want different buttons or event types to share the same handler.

The user-specified context is the context of your ClickConfigProvider (see above). By default it points to the window. You can override it for all handlers with window_set_click_config_provider_with_context() or for a specific button with window_set_click_context().

User interaction in watchfaces

Watchfaces cannot use the buttons to interact with the user. Instead, you can use the AccelerometerService.

About the Back button

By default, the Back button will always pop to the previous window on the Window Stack (and leave the app if the current window is the only window). You can override the default back button behavior with window_single_click_subscribe() and window_multi_click_subscribe() but you cannot set a repeating, long or raw click handler on the back button because a long press will always terminate the app and return to the main menu.

Usage example

First associate a click config provider callback with your window:

void app_init(void) {
...
...
}

Then in the callback, you set your desired configuration for each button:

void config_provider(Window *window) {
// single click / repeat-on-hold config:
window_single_click_subscribe(BUTTON_ID_DOWN, down_single_click_handler);
window_single_repeating_click_subscribe(BUTTON_ID_SELECT, 1000, select_single_click_handler);
// multi click config:
window_multi_click_subscribe(BUTTON_ID_SELECT, 2, 10, 0, true, select_multi_click_handler);
// long click config:
window_long_click_subscribe(BUTTON_ID_SELECT, 700, select_long_click_handler, select_long_click_release_handler);
}

Now you implement the handlers for each click you've subscribed to and set up:

void down_single_click_handler(ClickRecognizerRef recognizer, void *context) {
... called on single click ...
Window *window = (Window *)context;
}
void select_single_click_handler(ClickRecognizerRef recognizer, void *context) {
... called on single click, and every 1000ms of being held ...
Window *window = (Window *)context;
}
void select_multi_click_handler(ClickRecognizerRef recognizer, void *context) {
... called for multi-clicks ...
Window *window = (Window *)context;
const uint16_t count = click_number_of_clicks_counted(recognizer);
}
void select_long_click_handler(ClickRecognizerRef recognizer, void *context) {
... called on long click start ...
Window *window = (Window *)context;
}
void select_long_click_release_handler(ClickRecognizerRef recognizer, void *context) {
... called when long click is released ...
Window *window = (Window *)context;
}

See also

Refer to the (chapter "Clicks") for a conceptual overview of clicks and relevant code examples.

Typedef Documentation

◆ ClickConfigProvider

typedef void(* ClickConfigProvider) (void *context)

This callback is called every time the window becomes visible (and when you call window_set_click_config_provider() if the window is already visible).

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.

Parameters
contextPointer to application specific data (see window_set_click_config_provider_with_context()).

◆ ClickHandler

typedef void(* ClickHandler) (ClickRecognizerRef recognizer, void *context)

Function signature of the callback that handles a recognized click pattern

Parameters
recognizerThe click recognizer that detected a "click" pattern
contextPointer to application specified data (see window_set_click_config_provider_with_context() and window_set_click_context()). This defaults to the window.
See also
ClickConfigProvider

◆ ClickRecognizerRef

typedef void* ClickRecognizerRef

Reference to opaque click recognizer When a ClickHandler callback is called, the recognizer that fired the handler is passed in.

See also
ClickHandler
click_number_of_clicks_counted()
click_recognizer_get_button_id()
click_recognizer_is_repeating()

Enumeration Type Documentation

◆ ButtonId

enum ButtonId

Button ID values

See also
click_recognizer_get_button_id()
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.

Function Documentation

◆ click_number_of_clicks_counted()

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.

Parameters
recognizerThe click recognizer for which to get the click count
Returns
The number of consecutive clicks, and for auto-repeating the number of repetitions.

◆ click_recognizer_get_button_id()

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.

Parameters
recognizerThe click recognizer for which to get the button id that caused the click event
Returns
the ButtonId of the click recognizer

◆ click_recognizer_is_repeating()

bool click_recognizer_is_repeating ( ClickRecognizerRef  recognizer)

Is this a repeating click. You can use this inside a click handler implementation to find out whether this is a repeating click or not.

Parameters
recognizerThe click recognizer for which to find out whether this is a repeating click.
Returns
true if this is a repeating click.
window_single_click_subscribe
void window_single_click_subscribe(ButtonId button_id, ClickHandler handler)
ClickConfigProvider
void(* ClickConfigProvider)(void *context)
Definition: pebble.h:4990
window_set_click_config_provider
void window_set_click_config_provider(Window *window, ClickConfigProvider click_config_provider)
window_long_click_subscribe
void window_long_click_subscribe(ButtonId button_id, uint16_t delay_ms, ClickHandler down_handler, ClickHandler up_handler)
click_number_of_clicks_counted
uint8_t click_number_of_clicks_counted(ClickRecognizerRef recognizer)
ClickRecognizerRef
void * ClickRecognizerRef
Definition: pebble.h:4970
BUTTON_ID_SELECT
@ BUTTON_ID_SELECT
Select (middle) button.
Definition: pebble.h:166
window_multi_click_subscribe
void window_multi_click_subscribe(ButtonId button_id, uint8_t min_clicks, uint8_t max_clicks, uint16_t timeout, bool last_click_only, ClickHandler handler)
BUTTON_ID_DOWN
@ BUTTON_ID_DOWN
Down button.
Definition: pebble.h:168
window_single_repeating_click_subscribe
void window_single_repeating_click_subscribe(ButtonId button_id, uint16_t repeat_interval_ms, ClickHandler handler)