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