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