![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
A DTMF sequence editor and player for HAM radio equipment command & control.
KeyboardManager/kbd_mgr/LongKeyPressMonitor.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 LONG_KEY_PRESS_MONITOR_H_ |
osmeest | 0:1324e7d9d471 | 2 | #define LONG_KEY_PRESS_MONITOR_H_ |
osmeest | 0:1324e7d9d471 | 3 | |
osmeest | 0:1324e7d9d471 | 4 | #include <kbd_mgr/KeyPressEventServer.h> |
osmeest | 0:1324e7d9d471 | 5 | #include <set> |
osmeest | 0:1324e7d9d471 | 6 | |
osmeest | 0:1324e7d9d471 | 7 | #include "mbed.h" |
osmeest | 0:1324e7d9d471 | 8 | |
osmeest | 0:1324e7d9d471 | 9 | namespace kbd_mgr { |
osmeest | 0:1324e7d9d471 | 10 | |
osmeest | 0:1324e7d9d471 | 11 | /** |
osmeest | 0:1324e7d9d471 | 12 | * @brief A key press event handler that detects long key presses and report them as specified. |
osmeest | 0:1324e7d9d471 | 13 | * This class offers two specific reactions for long key presses. Either a specific long key press is reported |
osmeest | 0:1324e7d9d471 | 14 | * or the key press event is auto repeated. The timing for both kind of reactions can be specified independently. |
osmeest | 0:1324e7d9d471 | 15 | * The monitor reacts on key code, not on mapped key char. If a mapped key char is present in the event, it is retained |
osmeest | 0:1324e7d9d471 | 16 | * in the generated key press events. |
osmeest | 0:1324e7d9d471 | 17 | */ |
osmeest | 0:1324e7d9d471 | 18 | class LongKeyPressMonitor : public KeyPressEventServer, public KeyPressEventHandler { |
osmeest | 0:1324e7d9d471 | 19 | public: |
osmeest | 0:1324e7d9d471 | 20 | LongKeyPressMonitor() : |
osmeest | 0:1324e7d9d471 | 21 | repeatKeys_(), repeatInitTime_(0), repeatDelay_(0), longPressKeys_(), longPressTime_(0), |
osmeest | 0:1324e7d9d471 | 22 | state(Idle), keyDownCount(0), timer() |
osmeest | 0:1324e7d9d471 | 23 | { } |
osmeest | 0:1324e7d9d471 | 24 | |
osmeest | 0:1324e7d9d471 | 25 | class AutoRepeatSetupProxy { |
osmeest | 0:1324e7d9d471 | 26 | public: |
osmeest | 0:1324e7d9d471 | 27 | AutoRepeatSetupProxy(LongKeyPressMonitor *monitor) : monitor_(monitor) { } |
osmeest | 0:1324e7d9d471 | 28 | AutoRepeatSetupProxy& operator()(int key) { this->monitor_->addAutoRepeatKey(key, key); return *this; } |
osmeest | 0:1324e7d9d471 | 29 | AutoRepeatSetupProxy& operator()(int firstKey, int lastKey) { this->monitor_->addAutoRepeatKey(firstKey, lastKey); return *this; } |
osmeest | 0:1324e7d9d471 | 30 | |
osmeest | 0:1324e7d9d471 | 31 | private: |
osmeest | 0:1324e7d9d471 | 32 | LongKeyPressMonitor *monitor_; |
osmeest | 0:1324e7d9d471 | 33 | }; |
osmeest | 0:1324e7d9d471 | 34 | |
osmeest | 0:1324e7d9d471 | 35 | friend class AutoRepeatSetupProxy; |
osmeest | 0:1324e7d9d471 | 36 | |
osmeest | 0:1324e7d9d471 | 37 | /** |
osmeest | 0:1324e7d9d471 | 38 | * @brief Sets up auto-repeat keys. |
osmeest | 0:1324e7d9d471 | 39 | * This method takes the timing parameters. It returns a special class that allows specifying the keys |
osmeest | 0:1324e7d9d471 | 40 | * between brackets. Eg: |
osmeest | 0:1324e7d9d471 | 41 | * - monitor.autoRepeat(0.3, 0.1)(1)(2)(4,8) |
osmeest | 0:1324e7d9d471 | 42 | * Sets up auto repeat after 300ms, every 100ms for keys 1, 2 and 4 to 8. |
osmeest | 0:1324e7d9d471 | 43 | */ |
osmeest | 0:1324e7d9d471 | 44 | AutoRepeatSetupProxy autoRepeat(float initTime, float delay); |
osmeest | 0:1324e7d9d471 | 45 | |
osmeest | 0:1324e7d9d471 | 46 | |
osmeest | 0:1324e7d9d471 | 47 | class LongPressSetupProxy { |
osmeest | 0:1324e7d9d471 | 48 | public: |
osmeest | 0:1324e7d9d471 | 49 | LongPressSetupProxy(LongKeyPressMonitor *monitor) : monitor_(monitor) { } |
osmeest | 0:1324e7d9d471 | 50 | LongPressSetupProxy& operator()(int key) { this->monitor_->addLongPressKey(key, key); return *this; } |
osmeest | 0:1324e7d9d471 | 51 | LongPressSetupProxy& operator()(int firstKey, int lastKey) { this->monitor_->addLongPressKey(firstKey, lastKey); return *this; } |
osmeest | 0:1324e7d9d471 | 52 | |
osmeest | 0:1324e7d9d471 | 53 | private: |
osmeest | 0:1324e7d9d471 | 54 | LongKeyPressMonitor *monitor_; |
osmeest | 0:1324e7d9d471 | 55 | }; |
osmeest | 0:1324e7d9d471 | 56 | |
osmeest | 0:1324e7d9d471 | 57 | friend class LongPressSetupProxy; |
osmeest | 0:1324e7d9d471 | 58 | |
osmeest | 0:1324e7d9d471 | 59 | /** |
osmeest | 0:1324e7d9d471 | 60 | * @brief Sets up long key press keys. |
osmeest | 0:1324e7d9d471 | 61 | * This method takes the timing parameters. It returns a special class that allows specifying the keys |
osmeest | 0:1324e7d9d471 | 62 | * between brackets. Eg: |
osmeest | 0:1324e7d9d471 | 63 | * - monitor.longKeyPress(0.5)(3)(12,14) |
osmeest | 0:1324e7d9d471 | 64 | * Sets up report of long key press after 500ms for keys 3 and 12 to 14. |
osmeest | 0:1324e7d9d471 | 65 | */ |
osmeest | 0:1324e7d9d471 | 66 | LongPressSetupProxy longKeyPress(float longPressTime); |
osmeest | 0:1324e7d9d471 | 67 | |
osmeest | 0:1324e7d9d471 | 68 | /** |
osmeest | 0:1324e7d9d471 | 69 | * @brief KeyPressEventHandler interface |
osmeest | 0:1324e7d9d471 | 70 | */ |
osmeest | 0:1324e7d9d471 | 71 | virtual void handleKeyPress(const KeyEvent &keypress); |
osmeest | 0:1324e7d9d471 | 72 | |
osmeest | 0:1324e7d9d471 | 73 | private: |
osmeest | 0:1324e7d9d471 | 74 | void addAutoRepeatKey(int firstKey, int lastKey); |
osmeest | 0:1324e7d9d471 | 75 | bool isAutoRepeatKey(int key) const { return this->repeatKeys_.find(key) != this->repeatKeys_.end(); } |
osmeest | 0:1324e7d9d471 | 76 | void addLongPressKey(int firstKey, int lastKey); |
osmeest | 0:1324e7d9d471 | 77 | bool isLongPressKey(int key) const { return this->longPressKeys_.find(key) != this->longPressKeys_.end(); } |
osmeest | 0:1324e7d9d471 | 78 | |
osmeest | 0:1324e7d9d471 | 79 | void handleKeyDown(const KeyEvent &keypress); |
osmeest | 0:1324e7d9d471 | 80 | void handleFirstKeyDown(const KeyEvent &keypress); |
osmeest | 0:1324e7d9d471 | 81 | void handleOtherKeyDown(const KeyEvent &keypress); |
osmeest | 0:1324e7d9d471 | 82 | void handleKeyUp(const KeyEvent &keypress); |
osmeest | 0:1324e7d9d471 | 83 | void handleLastKeyUp(const KeyEvent &keypress); |
osmeest | 0:1324e7d9d471 | 84 | |
osmeest | 0:1324e7d9d471 | 85 | void handleTimer(); |
osmeest | 0:1324e7d9d471 | 86 | void handleRepeatTimer(); |
osmeest | 0:1324e7d9d471 | 87 | void handleLongPressTimer(); |
osmeest | 0:1324e7d9d471 | 88 | |
osmeest | 0:1324e7d9d471 | 89 | typedef std::set<int> KeySet; |
osmeest | 0:1324e7d9d471 | 90 | |
osmeest | 0:1324e7d9d471 | 91 | KeySet repeatKeys_; |
osmeest | 0:1324e7d9d471 | 92 | float repeatInitTime_; |
osmeest | 0:1324e7d9d471 | 93 | float repeatDelay_; |
osmeest | 0:1324e7d9d471 | 94 | |
osmeest | 0:1324e7d9d471 | 95 | KeySet longPressKeys_; |
osmeest | 0:1324e7d9d471 | 96 | float longPressTime_; |
osmeest | 0:1324e7d9d471 | 97 | |
osmeest | 0:1324e7d9d471 | 98 | enum State { |
osmeest | 0:1324e7d9d471 | 99 | Idle, |
osmeest | 0:1324e7d9d471 | 100 | RepeatInitWait, |
osmeest | 0:1324e7d9d471 | 101 | Repeating, |
osmeest | 0:1324e7d9d471 | 102 | LongPressWait, |
osmeest | 0:1324e7d9d471 | 103 | LongPressReported, |
osmeest | 0:1324e7d9d471 | 104 | Invalid |
osmeest | 0:1324e7d9d471 | 105 | }; |
osmeest | 0:1324e7d9d471 | 106 | |
osmeest | 0:1324e7d9d471 | 107 | State state; |
osmeest | 0:1324e7d9d471 | 108 | KeyEvent keypress; |
osmeest | 0:1324e7d9d471 | 109 | int keyDownCount; |
osmeest | 0:1324e7d9d471 | 110 | Timeout timer; |
osmeest | 0:1324e7d9d471 | 111 | }; |
osmeest | 0:1324e7d9d471 | 112 | |
osmeest | 0:1324e7d9d471 | 113 | } // kbd_mgr |
osmeest | 0:1324e7d9d471 | 114 | |
osmeest | 0:1324e7d9d471 | 115 | #endif // LONG_KEY_PRESS_MONITOR_H_ |