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