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