KeyboardManager: a class to manage the polling of a switch-matrix keyboard
LongKeyPressMonitor.cpp@3:1310c57aca77, 2011-02-03 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
osmeest | 3:1310c57aca77 | 1 | #include <kbd_mgr/LongKeyPressMonitor.h> |
osmeest | 3:1310c57aca77 | 2 | |
osmeest | 3:1310c57aca77 | 3 | namespace kbd_mgr { |
osmeest | 3:1310c57aca77 | 4 | |
osmeest | 3:1310c57aca77 | 5 | LongKeyPressMonitor::AutoRepeatSetupProxy LongKeyPressMonitor::autoRepeat(float initTime, float delay) |
osmeest | 3:1310c57aca77 | 6 | { |
osmeest | 3:1310c57aca77 | 7 | this->repeatInitTime_ = initTime; |
osmeest | 3:1310c57aca77 | 8 | this->repeatDelay_ = delay; |
osmeest | 3:1310c57aca77 | 9 | |
osmeest | 3:1310c57aca77 | 10 | return AutoRepeatSetupProxy(this); |
osmeest | 3:1310c57aca77 | 11 | } |
osmeest | 3:1310c57aca77 | 12 | |
osmeest | 3:1310c57aca77 | 13 | void LongKeyPressMonitor::addAutoRepeatKey(int firstKey, int lastKey) |
osmeest | 3:1310c57aca77 | 14 | { |
osmeest | 3:1310c57aca77 | 15 | for(int key = firstKey; key <= lastKey; ++key) { |
osmeest | 3:1310c57aca77 | 16 | this->repeatKeys_.insert(key); |
osmeest | 3:1310c57aca77 | 17 | this->longPressKeys_.erase(key); |
osmeest | 3:1310c57aca77 | 18 | } |
osmeest | 3:1310c57aca77 | 19 | } |
osmeest | 3:1310c57aca77 | 20 | |
osmeest | 3:1310c57aca77 | 21 | LongKeyPressMonitor::LongPressSetupProxy LongKeyPressMonitor::longKeyPress(float longPressTime) |
osmeest | 3:1310c57aca77 | 22 | { |
osmeest | 3:1310c57aca77 | 23 | this->longPressTime_ = longPressTime; |
osmeest | 3:1310c57aca77 | 24 | return LongPressSetupProxy(this); |
osmeest | 3:1310c57aca77 | 25 | } |
osmeest | 3:1310c57aca77 | 26 | |
osmeest | 3:1310c57aca77 | 27 | void LongKeyPressMonitor::addLongPressKey(int firstKey, int lastKey) |
osmeest | 3:1310c57aca77 | 28 | { |
osmeest | 3:1310c57aca77 | 29 | for(int key = firstKey; key <= lastKey; ++key) { |
osmeest | 3:1310c57aca77 | 30 | this->longPressKeys_.insert(key); |
osmeest | 3:1310c57aca77 | 31 | this->repeatKeys_.erase(key); |
osmeest | 3:1310c57aca77 | 32 | } |
osmeest | 3:1310c57aca77 | 33 | } |
osmeest | 3:1310c57aca77 | 34 | |
osmeest | 3:1310c57aca77 | 35 | void LongKeyPressMonitor::handleKeyPress(const KeyEvent &keypress) |
osmeest | 3:1310c57aca77 | 36 | { |
osmeest | 3:1310c57aca77 | 37 | if (keypress.event == KeyEvent::KeyDown) { |
osmeest | 3:1310c57aca77 | 38 | invokeHandler(keypress); |
osmeest | 3:1310c57aca77 | 39 | handleKeyDown(keypress); |
osmeest | 3:1310c57aca77 | 40 | } |
osmeest | 3:1310c57aca77 | 41 | else if (keypress.event == KeyEvent::KeyUp) { |
osmeest | 3:1310c57aca77 | 42 | handleKeyUp(keypress); |
osmeest | 3:1310c57aca77 | 43 | invokeHandler(keypress); |
osmeest | 3:1310c57aca77 | 44 | } |
osmeest | 3:1310c57aca77 | 45 | else { |
osmeest | 3:1310c57aca77 | 46 | invokeHandler(keypress); |
osmeest | 3:1310c57aca77 | 47 | } |
osmeest | 3:1310c57aca77 | 48 | } |
osmeest | 3:1310c57aca77 | 49 | |
osmeest | 3:1310c57aca77 | 50 | void LongKeyPressMonitor::handleKeyDown(const KeyEvent &keypress) |
osmeest | 3:1310c57aca77 | 51 | { |
osmeest | 3:1310c57aca77 | 52 | this->keyDownCount++; |
osmeest | 3:1310c57aca77 | 53 | if (this->keyDownCount == 1) { |
osmeest | 3:1310c57aca77 | 54 | handleFirstKeyDown(keypress); |
osmeest | 3:1310c57aca77 | 55 | } |
osmeest | 3:1310c57aca77 | 56 | else { |
osmeest | 3:1310c57aca77 | 57 | handleOtherKeyDown(keypress); |
osmeest | 3:1310c57aca77 | 58 | } |
osmeest | 3:1310c57aca77 | 59 | } |
osmeest | 3:1310c57aca77 | 60 | |
osmeest | 3:1310c57aca77 | 61 | void LongKeyPressMonitor::handleFirstKeyDown(const KeyEvent &keypress) |
osmeest | 3:1310c57aca77 | 62 | { |
osmeest | 3:1310c57aca77 | 63 | this->keypress = keypress; |
osmeest | 3:1310c57aca77 | 64 | |
osmeest | 3:1310c57aca77 | 65 | if (isAutoRepeatKey(keypress.keyCode)) { |
osmeest | 3:1310c57aca77 | 66 | this->state = RepeatInitWait; |
osmeest | 3:1310c57aca77 | 67 | this->timer.attach(this, &LongKeyPressMonitor::handleTimer, this->repeatInitTime_); |
osmeest | 3:1310c57aca77 | 68 | } |
osmeest | 3:1310c57aca77 | 69 | else if (isLongPressKey(keypress.keyCode)) { |
osmeest | 3:1310c57aca77 | 70 | this->state = LongPressWait; |
osmeest | 3:1310c57aca77 | 71 | this->timer.attach(this, &LongKeyPressMonitor::handleTimer, this->longPressTime_); |
osmeest | 3:1310c57aca77 | 72 | } |
osmeest | 3:1310c57aca77 | 73 | } |
osmeest | 3:1310c57aca77 | 74 | |
osmeest | 3:1310c57aca77 | 75 | void LongKeyPressMonitor::handleOtherKeyDown(const KeyEvent &keypress) |
osmeest | 3:1310c57aca77 | 76 | { |
osmeest | 3:1310c57aca77 | 77 | this->state = Invalid; |
osmeest | 3:1310c57aca77 | 78 | this->timer.detach(); |
osmeest | 3:1310c57aca77 | 79 | } |
osmeest | 3:1310c57aca77 | 80 | |
osmeest | 3:1310c57aca77 | 81 | void LongKeyPressMonitor::handleKeyUp(const KeyEvent &keypress) |
osmeest | 3:1310c57aca77 | 82 | { |
osmeest | 3:1310c57aca77 | 83 | this->keyDownCount--; |
osmeest | 3:1310c57aca77 | 84 | |
osmeest | 3:1310c57aca77 | 85 | if (this->state == Invalid || this->state == RepeatInitWait || this->state == LongPressWait) { |
osmeest | 3:1310c57aca77 | 86 | KeyEvent pressed(keypress, KeyEvent::KeyPress); |
osmeest | 3:1310c57aca77 | 87 | invokeHandler(pressed); |
osmeest | 3:1310c57aca77 | 88 | } |
osmeest | 3:1310c57aca77 | 89 | |
osmeest | 3:1310c57aca77 | 90 | if (this->keyDownCount == 0) { |
osmeest | 3:1310c57aca77 | 91 | handleLastKeyUp(keypress); |
osmeest | 3:1310c57aca77 | 92 | } |
osmeest | 3:1310c57aca77 | 93 | } |
osmeest | 3:1310c57aca77 | 94 | |
osmeest | 3:1310c57aca77 | 95 | void LongKeyPressMonitor::handleLastKeyUp(const KeyEvent &keypress) |
osmeest | 3:1310c57aca77 | 96 | { |
osmeest | 3:1310c57aca77 | 97 | this->state = Idle; |
osmeest | 3:1310c57aca77 | 98 | } |
osmeest | 3:1310c57aca77 | 99 | |
osmeest | 3:1310c57aca77 | 100 | void LongKeyPressMonitor::handleTimer() |
osmeest | 3:1310c57aca77 | 101 | { |
osmeest | 3:1310c57aca77 | 102 | switch(this->state) { |
osmeest | 3:1310c57aca77 | 103 | case RepeatInitWait: |
osmeest | 3:1310c57aca77 | 104 | case Repeating: |
osmeest | 3:1310c57aca77 | 105 | handleRepeatTimer(); |
osmeest | 3:1310c57aca77 | 106 | break; |
osmeest | 3:1310c57aca77 | 107 | case LongPressWait: |
osmeest | 3:1310c57aca77 | 108 | handleLongPressTimer(); |
osmeest | 3:1310c57aca77 | 109 | break; |
osmeest | 3:1310c57aca77 | 110 | default: |
osmeest | 3:1310c57aca77 | 111 | break; |
osmeest | 3:1310c57aca77 | 112 | } |
osmeest | 3:1310c57aca77 | 113 | } |
osmeest | 3:1310c57aca77 | 114 | |
osmeest | 3:1310c57aca77 | 115 | void LongKeyPressMonitor::handleRepeatTimer() |
osmeest | 3:1310c57aca77 | 116 | { |
osmeest | 3:1310c57aca77 | 117 | KeyEvent repeated(this->keypress, |
osmeest | 3:1310c57aca77 | 118 | (this->state == RepeatInitWait ? KeyEvent::KeyPress : KeyEvent::RepeatedKeyPress)); |
osmeest | 3:1310c57aca77 | 119 | invokeHandler(repeated); |
osmeest | 3:1310c57aca77 | 120 | |
osmeest | 3:1310c57aca77 | 121 | this->state = Repeating; |
osmeest | 3:1310c57aca77 | 122 | this->timer.attach(this, &LongKeyPressMonitor::handleTimer, this->repeatDelay_); |
osmeest | 3:1310c57aca77 | 123 | } |
osmeest | 3:1310c57aca77 | 124 | |
osmeest | 3:1310c57aca77 | 125 | void LongKeyPressMonitor::handleLongPressTimer() |
osmeest | 3:1310c57aca77 | 126 | { |
osmeest | 3:1310c57aca77 | 127 | KeyEvent longPressed(keypress, KeyEvent::LongKeyPress); |
osmeest | 3:1310c57aca77 | 128 | invokeHandler(longPressed); |
osmeest | 3:1310c57aca77 | 129 | this->state = LongPressReported; |
osmeest | 3:1310c57aca77 | 130 | } |
osmeest | 3:1310c57aca77 | 131 | |
osmeest | 3:1310c57aca77 | 132 | } // kbd_mgr |