Empirical
Classes | Namespaces
KeypressManager.h File Reference

KeypressManager is a tracker for keypresses in HTML5 pages. More...

#include <functional>
#include <map>
#include "events.h"
#include "JSWrap.h"

Go to the source code of this file.

Classes

class  emp::web::KeypressManager
 

Namespaces

 emp
 If we are in emscripten, make sure to include the header.
 
 emp::web
 

Detailed Description

KeypressManager is a tracker for keypresses in HTML5 pages.

Note
This file is part of Empirical, https://github.com/devosoft/Empirical
Date
2015-2017

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 html5::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 html5::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.