KeyboardManager: a class to manage the polling of a switch-matrix keyboard

Dependents:   KeyboardTest

Committer:
osmeest
Date:
Thu Feb 03 22:01:57 2011 +0000
Revision:
3:1310c57aca77
improve code structure, add key mapping and long key press handling

Who changed what in which revision?

UserRevisionLine numberNew contents of line
osmeest 3:1310c57aca77 1 #ifndef KEY_MAPPER_H_
osmeest 3:1310c57aca77 2 #define KEY_MAPPER_H_
osmeest 3:1310c57aca77 3
osmeest 3:1310c57aca77 4 #include "kbd_mgr/KeyPressEventServer.h"
osmeest 3:1310c57aca77 5
osmeest 3:1310c57aca77 6 #include <vector>
osmeest 3:1310c57aca77 7 #include <string>
osmeest 3:1310c57aca77 8 #include <utility>
osmeest 3:1310c57aca77 9
osmeest 3:1310c57aca77 10 namespace kbd_mgr {
osmeest 3:1310c57aca77 11
osmeest 3:1310c57aca77 12 /**
osmeest 3:1310c57aca77 13 * @brief A class used to store a set of key mappings.
osmeest 3:1310c57aca77 14 * It associates a key event and a key code to a mapped character.
osmeest 3:1310c57aca77 15 * It also allows to specify a mapping for any unmatched key event of a key.
osmeest 3:1310c57aca77 16 * The KeyMap can be set at construction time with a string and an optional key event.
osmeest 3:1310c57aca77 17 * Additional mappings can be specified by using the () operator. This way, both individual mappings and string mappings are possible.
osmeest 3:1310c57aca77 18 * Eg. keymap(KeyEvent::LongKeyPress, 1, '@')(1, '*')
osmeest 3:1310c57aca77 19 * maps the key 1 to '*' for all key presses but to '@' when a long key press occurs.
osmeest 3:1310c57aca77 20 */
osmeest 3:1310c57aca77 21 class KeyMap {
osmeest 3:1310c57aca77 22 public:
osmeest 3:1310c57aca77 23 /**
osmeest 3:1310c57aca77 24 * @brief Creates an empty keymap.
osmeest 3:1310c57aca77 25 */
osmeest 3:1310c57aca77 26 KeyMap() : map_() { }
osmeest 3:1310c57aca77 27
osmeest 3:1310c57aca77 28 /**
osmeest 3:1310c57aca77 29 * @brief Creates a keymap based on a string.
osmeest 3:1310c57aca77 30 * Each character is mapped to the key at that position (key 1 maps to str[1]).
osmeest 3:1310c57aca77 31 */
osmeest 3:1310c57aca77 32 KeyMap(const std::string &str) : map_() {
osmeest 3:1310c57aca77 33 addMappings(KeyEvent::NoEvent, str);
osmeest 3:1310c57aca77 34 }
osmeest 3:1310c57aca77 35 /**
osmeest 3:1310c57aca77 36 * @brief Creates a keymap based on a string for a certain key event.
osmeest 3:1310c57aca77 37 * Each character is mapped to the key at that position (key 1 maps to str[1]).
osmeest 3:1310c57aca77 38 */
osmeest 3:1310c57aca77 39 KeyMap(KeyEvent::EventType event, const std::string &str) : map_() {
osmeest 3:1310c57aca77 40 addMappings(event, str);
osmeest 3:1310c57aca77 41 }
osmeest 3:1310c57aca77 42
osmeest 3:1310c57aca77 43 /**
osmeest 3:1310c57aca77 44 * @brief Adds key mappings based on the given string.
osmeest 3:1310c57aca77 45 */
osmeest 3:1310c57aca77 46 KeyMap& operator()(const std::string &str) {
osmeest 3:1310c57aca77 47 addMappings(KeyEvent::NoEvent, str);
osmeest 3:1310c57aca77 48 return *this;
osmeest 3:1310c57aca77 49 }
osmeest 3:1310c57aca77 50
osmeest 3:1310c57aca77 51 /**
osmeest 3:1310c57aca77 52 * @brief Adds key mappings based on the given string for a given event.
osmeest 3:1310c57aca77 53 */
osmeest 3:1310c57aca77 54 KeyMap& operator()(KeyEvent::EventType event, const std::string &str) {
osmeest 3:1310c57aca77 55 addMappings(event, str);
osmeest 3:1310c57aca77 56 return *this;
osmeest 3:1310c57aca77 57 }
osmeest 3:1310c57aca77 58
osmeest 3:1310c57aca77 59 /**
osmeest 3:1310c57aca77 60 * @brief Adds a key mapping for a key.
osmeest 3:1310c57aca77 61 */
osmeest 3:1310c57aca77 62 KeyMap& operator()(int key, char ch) {
osmeest 3:1310c57aca77 63 addMapping(KeyEvent::NoEvent, key, ch);
osmeest 3:1310c57aca77 64 return *this;
osmeest 3:1310c57aca77 65 }
osmeest 3:1310c57aca77 66
osmeest 3:1310c57aca77 67 /**
osmeest 3:1310c57aca77 68 * @brief Adds a key mapping for a key event & code combo.
osmeest 3:1310c57aca77 69 */
osmeest 3:1310c57aca77 70 KeyMap& operator()(KeyEvent::EventType event, int key, char ch) {
osmeest 3:1310c57aca77 71 addMapping(event, key, ch);
osmeest 3:1310c57aca77 72 return *this;
osmeest 3:1310c57aca77 73 }
osmeest 3:1310c57aca77 74
osmeest 3:1310c57aca77 75 /**
osmeest 3:1310c57aca77 76 * @brief Gets the mapped character for a key press event.
osmeest 3:1310c57aca77 77 */
osmeest 3:1310c57aca77 78 char map(KeyEvent::EventType event, int key) const;
osmeest 3:1310c57aca77 79
osmeest 3:1310c57aca77 80 private:
osmeest 3:1310c57aca77 81 struct KeyMapping {
osmeest 3:1310c57aca77 82 KeyEvent::EventType event;
osmeest 3:1310c57aca77 83 int key;
osmeest 3:1310c57aca77 84 char ch;
osmeest 3:1310c57aca77 85
osmeest 3:1310c57aca77 86 KeyMapping(KeyEvent::EventType event, int key, char ch) : event(event), key(key), ch(ch) { }
osmeest 3:1310c57aca77 87
osmeest 3:1310c57aca77 88 bool matches(const std::pair<KeyEvent::EventType, int> &arg) const {
osmeest 3:1310c57aca77 89 return this->event == arg.first && this->key == arg.second;
osmeest 3:1310c57aca77 90 }
osmeest 3:1310c57aca77 91 };
osmeest 3:1310c57aca77 92
osmeest 3:1310c57aca77 93 void addMapping(KeyEvent::EventType event, int key, char ch);
osmeest 3:1310c57aca77 94 void addMappings(KeyEvent::EventType event, const std::string &str);
osmeest 3:1310c57aca77 95 const KeyMapping * getMapping(KeyEvent::EventType event, int key) const;
osmeest 3:1310c57aca77 96
osmeest 3:1310c57aca77 97 typedef std::vector<KeyMapping> Map;
osmeest 3:1310c57aca77 98 Map map_;
osmeest 3:1310c57aca77 99 };
osmeest 3:1310c57aca77 100
osmeest 3:1310c57aca77 101 /**
osmeest 3:1310c57aca77 102 * @brief A key press event handler that adds a mapped key char to key events.
osmeest 3:1310c57aca77 103 * Mappings can be specified for any key press event or for some specific key press event.
osmeest 3:1310c57aca77 104 */
osmeest 3:1310c57aca77 105 class KeyMapper : public KeyPressEventServer, public KeyPressEventHandler {
osmeest 3:1310c57aca77 106 public:
osmeest 3:1310c57aca77 107
osmeest 3:1310c57aca77 108 KeyMapper(const KeyMap &keymap = KeyMap()) :
osmeest 3:1310c57aca77 109 keymap_(keymap)
osmeest 3:1310c57aca77 110 { }
osmeest 3:1310c57aca77 111
osmeest 3:1310c57aca77 112 const KeyMap & keymap() const { return this->keymap_; }
osmeest 3:1310c57aca77 113 char map(KeyEvent::EventType event, int key) const { return this->keymap_.map(event, key); }
osmeest 3:1310c57aca77 114
osmeest 3:1310c57aca77 115 virtual void handleKeyPress(const KeyEvent &keypress);
osmeest 3:1310c57aca77 116
osmeest 3:1310c57aca77 117 private:
osmeest 3:1310c57aca77 118 KeyMap keymap_;
osmeest 3:1310c57aca77 119 };
osmeest 3:1310c57aca77 120
osmeest 3:1310c57aca77 121 } // kbd_mgr
osmeest 3:1310c57aca77 122
osmeest 3:1310c57aca77 123 #endif // KEY_MAPPER_H_