KeypressManager.hpp

KeypressManager is a tracker for keypresses in HTML5 pages.

When a KeypressManager is created, it can be given functions to run in response to different types of key presses via overloaded version of the AddKeydownCallback method. Each of these accepts an order parameter that is optional and, if provided, will indicate the order in which tests should be performed to resolve a keypress. If order is not provided, tests will occur in the order that they were given to the manager.

The specific versions of AddKeydownCallback are:

void AddKeydownCallback(std::function<bool(const KeyboardEvent &)> cb_fun, int order=-1)

Link a function to the KeypressManager that is called for any unresolved keypress. The function must take in an KeyboardEvent (which includes information about the specific key pressed as well as any modifiers such as SHIFT or CTRL) and it must return a boolean value indicating whether it has resolved the keypress.

void AddKeydownCallback(char key, std::function<void()> cb_fun, int order=-1)

Link a specific key to a target function to be called when that key is pressed. The function my return a void and take no arguments.

void AddKeydownCallback(const std::string & key_set, std::function<void()> cb_fun, int order=-1)

Same as the previous method, but will respond to any of the keys in the provided string.

Todo:

Technically we should make sure to remove the event listener in the destructor. This would require us to keep track of the function that it is calling so that we can pass it back in to trigger the removal.

class KeypressManager
#include <KeypressManager.hpp>

Public Functions

inline KeypressManager()
inline ~KeypressManager()
inline int GetFunCount() const
inline int GetNextOrder() const
inline void AddKeydownCallback(std::function<bool(const KeyboardEvent&)> cb_fun, int order = -1)

Link a function to the KeypressManager that is called for any unresolved keypress. The function must take in an KeyboardEvent (which includes information about the specific key pressed as well as any modifiers such as SHIFT or CTRL) and it must return a boolean value indicating whether it has resolved the keypress.

inline void AddKeydownCallback(char key, std::function<void()> cb_fun, int order = -1)

Link a specific key to a target function to be called when that key is pressed. The function my return a void and take no arguments. Specify keys as lowercase characters. To sepcify uppercase, you’ll need to monitor fo rthe shift modifier associated with a KeypressEvent.

inline void AddKeydownCallback(const std::string &key_set, const std::function<void()> &cb_fun, int order)

Provide a whole set of keys that should all trigger the same function, including an ordering for priority. Specify keys as lowercase characters. To sepcify uppercase, you’ll need to monitor fo rthe shift modifier associated with a KeypressEvent.

inline void AddKeydownCallback(const std::string &key_set, const std::function<void()> &cb_fun)

Provide a whole set of keys that should all trigger the same function; use default ordering.

Private Functions

inline bool DoCallback(const KeyboardEvent &evt_info)

Private Members

std::map<int, std::function<bool(const KeyboardEvent&)>> fun_map
int next_order
uint32_t callback_id