![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A DTMF sequence editor and player for HAM radio equipment command & control.
KeyboardManager/KeyMapper.cpp@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 | #include "kbd_mgr/KeyMapper.h" |
osmeest | 0:1324e7d9d471 | 2 | #include <algorithm> |
osmeest | 0:1324e7d9d471 | 3 | #include <functional> |
osmeest | 0:1324e7d9d471 | 4 | |
osmeest | 0:1324e7d9d471 | 5 | namespace kbd_mgr { |
osmeest | 0:1324e7d9d471 | 6 | |
osmeest | 0:1324e7d9d471 | 7 | void KeyMap::addMapping(KeyEvent::EventType event, int key, char ch) |
osmeest | 0:1324e7d9d471 | 8 | { |
osmeest | 0:1324e7d9d471 | 9 | const KeyMapping *mapping = getMapping(event, key); |
osmeest | 0:1324e7d9d471 | 10 | if (mapping) { |
osmeest | 0:1324e7d9d471 | 11 | KeyMapping *updatable = const_cast<KeyMapping*>(mapping); |
osmeest | 0:1324e7d9d471 | 12 | updatable->ch = ch; |
osmeest | 0:1324e7d9d471 | 13 | } |
osmeest | 0:1324e7d9d471 | 14 | else { |
osmeest | 0:1324e7d9d471 | 15 | KeyMapping o(event, key, ch); |
osmeest | 0:1324e7d9d471 | 16 | this->map_.push_back(o); |
osmeest | 0:1324e7d9d471 | 17 | } |
osmeest | 0:1324e7d9d471 | 18 | } |
osmeest | 0:1324e7d9d471 | 19 | |
osmeest | 0:1324e7d9d471 | 20 | void KeyMap::addMappings(KeyEvent::EventType event, const std::string &str) |
osmeest | 0:1324e7d9d471 | 21 | { |
osmeest | 0:1324e7d9d471 | 22 | for(int key = 0; key < str.size(); ++key) { |
osmeest | 0:1324e7d9d471 | 23 | addMapping(event, key, str[key]); |
osmeest | 0:1324e7d9d471 | 24 | } |
osmeest | 0:1324e7d9d471 | 25 | } |
osmeest | 0:1324e7d9d471 | 26 | |
osmeest | 0:1324e7d9d471 | 27 | char KeyMap::map(KeyEvent::EventType event, int key) const |
osmeest | 0:1324e7d9d471 | 28 | { |
osmeest | 0:1324e7d9d471 | 29 | const KeyMapping *mapping = getMapping(event, key); |
osmeest | 0:1324e7d9d471 | 30 | if (!mapping) { |
osmeest | 0:1324e7d9d471 | 31 | mapping = getMapping(KeyEvent::NoEvent, key); |
osmeest | 0:1324e7d9d471 | 32 | } |
osmeest | 0:1324e7d9d471 | 33 | return (mapping ? mapping->ch : '\0'); |
osmeest | 0:1324e7d9d471 | 34 | } |
osmeest | 0:1324e7d9d471 | 35 | |
osmeest | 0:1324e7d9d471 | 36 | const KeyMap::KeyMapping * KeyMap::getMapping(KeyEvent::EventType event, int key) const |
osmeest | 0:1324e7d9d471 | 37 | { |
osmeest | 0:1324e7d9d471 | 38 | std::pair<KeyEvent::EventType, int> criteria = std::make_pair(event, key); |
osmeest | 0:1324e7d9d471 | 39 | Map::const_iterator p = this->map_.begin(); |
osmeest | 0:1324e7d9d471 | 40 | while (p != this->map_.end() && !p->matches(criteria)) { |
osmeest | 0:1324e7d9d471 | 41 | ++p; |
osmeest | 0:1324e7d9d471 | 42 | } |
osmeest | 0:1324e7d9d471 | 43 | |
osmeest | 0:1324e7d9d471 | 44 | if (p == this->map_.end()) { |
osmeest | 0:1324e7d9d471 | 45 | return NULL; |
osmeest | 0:1324e7d9d471 | 46 | } |
osmeest | 0:1324e7d9d471 | 47 | return &(*p); |
osmeest | 0:1324e7d9d471 | 48 | } |
osmeest | 0:1324e7d9d471 | 49 | |
osmeest | 0:1324e7d9d471 | 50 | void KeyMapper::handleKeyPress(const KeyEvent &keypress) |
osmeest | 0:1324e7d9d471 | 51 | { |
osmeest | 0:1324e7d9d471 | 52 | char ch = map(keypress.event, keypress.keyCode); |
osmeest | 0:1324e7d9d471 | 53 | KeyEvent mapped(keypress, ch); |
osmeest | 0:1324e7d9d471 | 54 | invokeHandler(mapped); |
osmeest | 0:1324e7d9d471 | 55 | } |
osmeest | 0:1324e7d9d471 | 56 | |
osmeest | 0:1324e7d9d471 | 57 | } // kbd_mgr |