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
Parent:
2:eb4cc53ff33d
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 KEY_PRESS_EVENT_HANDLER_H_
osmeest 3:1310c57aca77 2 #define KEY_PRESS_EVENT_HANDLER_H_
osmeest 3:1310c57aca77 3
osmeest 3:1310c57aca77 4 namespace kbd_mgr {
osmeest 3:1310c57aca77 5
osmeest 3:1310c57aca77 6 struct KeyEvent {
osmeest 3:1310c57aca77 7 enum KeyId {
osmeest 3:1310c57aca77 8 NoKey = -1
osmeest 3:1310c57aca77 9 };
osmeest 3:1310c57aca77 10
osmeest 3:1310c57aca77 11 enum EventType {
osmeest 3:1310c57aca77 12 NoEvent, KeyDown, KeyPress, RepeatedKeyPress, LongKeyPress, KeyUp
osmeest 3:1310c57aca77 13 };
osmeest 3:1310c57aca77 14
osmeest 3:1310c57aca77 15 int keyCode;
osmeest 3:1310c57aca77 16 char keyChar;
osmeest 3:1310c57aca77 17 EventType event;
osmeest 3:1310c57aca77 18
osmeest 3:1310c57aca77 19 KeyEvent() : keyCode(NoKey), keyChar(0), event(NoEvent) { }
osmeest 3:1310c57aca77 20
osmeest 3:1310c57aca77 21 /**
osmeest 3:1310c57aca77 22 * @brief Creates a raw key event (no char).
osmeest 3:1310c57aca77 23 */
osmeest 3:1310c57aca77 24 KeyEvent(int key, EventType event) : keyCode(key), keyChar(0), event(event) { }
osmeest 3:1310c57aca77 25
osmeest 3:1310c57aca77 26 /**
osmeest 3:1310c57aca77 27 * @brief Converts a raw key event into a mapped key.
osmeest 3:1310c57aca77 28 */
osmeest 3:1310c57aca77 29 KeyEvent(const KeyEvent &raw, char ch) : keyCode(raw.keyCode), keyChar(ch), event(raw.event) { }
osmeest 3:1310c57aca77 30
osmeest 3:1310c57aca77 31 /**
osmeest 3:1310c57aca77 32 * @brief Creates a key event with a different event code.
osmeest 3:1310c57aca77 33 */
osmeest 3:1310c57aca77 34 KeyEvent(const KeyEvent &other, EventType event) : keyCode(other.keyCode), keyChar(other.keyChar), event(event) { }
osmeest 3:1310c57aca77 35 };
osmeest 3:1310c57aca77 36
osmeest 3:1310c57aca77 37 /**
osmeest 3:1310c57aca77 38 * @brief Interface used to report key presses and releases.
osmeest 3:1310c57aca77 39 */
osmeest 3:1310c57aca77 40 class KeyPressEventHandler {
osmeest 3:1310c57aca77 41 public:
osmeest 3:1310c57aca77 42 virtual void handleKeyPress(const KeyEvent &keypress) = 0;
osmeest 3:1310c57aca77 43 virtual ~KeyPressEventHandler() { }
osmeest 3:1310c57aca77 44 };
osmeest 3:1310c57aca77 45
osmeest 3:1310c57aca77 46 template <class T>
osmeest 3:1310c57aca77 47 class MemberKeyPressEventHandler : public KeyPressEventHandler {
osmeest 3:1310c57aca77 48 public:
osmeest 3:1310c57aca77 49 typedef void (T::*MemberFunction)(const KeyEvent &keypress);
osmeest 3:1310c57aca77 50
osmeest 3:1310c57aca77 51 MemberKeyPressEventHandler(T *obj, MemberFunction fn) :
osmeest 3:1310c57aca77 52 object(obj), func(fn)
osmeest 3:1310c57aca77 53 { }
osmeest 3:1310c57aca77 54
osmeest 3:1310c57aca77 55 virtual void handleKeyPress(const KeyEvent &keypress) {
osmeest 3:1310c57aca77 56 (object->*func)(keypress);
osmeest 3:1310c57aca77 57 }
osmeest 3:1310c57aca77 58
osmeest 3:1310c57aca77 59 private:
osmeest 3:1310c57aca77 60 T *object;
osmeest 3:1310c57aca77 61 MemberFunction func;
osmeest 3:1310c57aca77 62 };
osmeest 3:1310c57aca77 63
osmeest 3:1310c57aca77 64 class FunctionKeyPressEventHandler : public KeyPressEventHandler {
osmeest 3:1310c57aca77 65 public:
osmeest 3:1310c57aca77 66 typedef void (*HandlerFunction)(const KeyEvent &keypress);
osmeest 3:1310c57aca77 67
osmeest 3:1310c57aca77 68 FunctionKeyPressEventHandler(HandlerFunction fn) :
osmeest 3:1310c57aca77 69 func(fn)
osmeest 3:1310c57aca77 70 { }
osmeest 3:1310c57aca77 71
osmeest 3:1310c57aca77 72 virtual void handleKeyPress(const KeyEvent &keypress) {
osmeest 3:1310c57aca77 73 func(keypress);
osmeest 3:1310c57aca77 74 }
osmeest 3:1310c57aca77 75
osmeest 3:1310c57aca77 76 private:
osmeest 3:1310c57aca77 77 HandlerFunction func;
osmeest 3:1310c57aca77 78 };
osmeest 3:1310c57aca77 79
osmeest 3:1310c57aca77 80 } // kbd_mgr
osmeest 3:1310c57aca77 81
osmeest 3:1310c57aca77 82 #endif // KEY_PRESS_EVENT_HANDLER_H_