Code for Doug's text entry device: a five-button mouse with shift and space keys.
Dependencies: PinDetect USBDevice mbed
KeyManager.h
00001 /** 00002 * Class KeyManager 00003 * 00004 * This class tracks the state of the seven input 00005 * buttons. It exposes (void) callback methods to 00006 * flag each button as active or not and provides 00007 * a mapping between key combinations and the 00008 * alphabet for text input. 00009 * 00010 */ 00011 00012 #ifndef KEYMANAGER 00013 #define KEYMANAGER 00014 00015 #include <string> 00016 #include <ctype.h> 00017 00018 class KeyManager { 00019 private: 00020 // key definitions 00021 bool keyModifier; 00022 bool keySpace; 00023 bool key0; 00024 bool key1; 00025 bool key2; 00026 bool key3; 00027 bool key4; 00028 00029 // static character map and constants 00030 static char characters[27]; 00031 static char space; 00032 static char unknown; 00033 00034 public: 00035 00036 // constructor 00037 KeyManager() { 00038 keyModifier = false; 00039 keySpace = false; 00040 key0 = false; 00041 key1 = false; 00042 key2 = false; 00043 key3 = false; 00044 key4 = false; 00045 } 00046 00047 // DEBUG: return the status of which keys are pressed 00048 char* getStatusString() { 00049 std::string status = "the following keys are pressed:"; 00050 if (key0) { 00051 status += " 0"; 00052 } 00053 if (key1) { 00054 status += " 1"; 00055 } 00056 if (key2) { 00057 status += " 2"; 00058 } 00059 if (key3) { 00060 status += " 3"; 00061 } 00062 if (key4) { 00063 status += " 4"; 00064 } 00065 status += "\n"; 00066 return (char*)status.c_str(); 00067 } 00068 00069 // get the numeric code corresponding to 00070 // the currently pressed keys. 00071 int getCode() { 00072 int result = 0; 00073 if (key0) { 00074 result = result | 1; 00075 } 00076 if (key1) { 00077 result = result | 2; 00078 } 00079 if (key2) { 00080 result = result | 4; 00081 } 00082 if (key3) { 00083 result = result | 8; 00084 } 00085 if (key4) { 00086 result = result | 16; 00087 } 00088 return result; 00089 } 00090 00091 // map the current code to its character 00092 char getCharacter() { 00093 if (keySpace) { 00094 return space; 00095 } else { 00096 int code = this->getCode(); 00097 if (code < 28 && !keyModifier) { 00098 return characters[code-1]; 00099 } else if (code < 28 && keyModifier) { 00100 return toupper(characters[code-1]); 00101 } else { 00102 return unknown; 00103 } 00104 } 00105 } 00106 00107 // check whether any of the character keys or the space 00108 // key are pressed 00109 bool keysPressed() { 00110 if (key0 || key1 || key2 || key3 || key4 || keySpace) { 00111 return true; 00112 } 00113 return false; 00114 } 00115 00116 // Interrupt callback functions 00117 void keyMOn() { keyModifier = true; } 00118 void keyMOff() { keyModifier = false; } 00119 void keySpaceOn() { keySpace = true; } 00120 void keySpaceOff() { keySpace = false; } 00121 void key0On() { key0 = true; } 00122 void key0Off() { key0 = false; } 00123 void key1On() { key1 = true; } 00124 void key1Off() { key1 = false; } 00125 void key2On() { key2 = true; } 00126 void key2Off() { key2 = false; } 00127 void key3On() { key3 = true; } 00128 void key3Off() { key3 = false; } 00129 void key4On() { key4 = true; } 00130 void key4Off() { key4 = false; } 00131 }; 00132 00133 // define statics 00134 char KeyManager::characters[27] = {'a','b','c','d','e','f','g', 00135 'h','i','j','k','l','m','n','o','p','q','r','s','t', 00136 'u','v','w','x','y','z','.' }; 00137 char KeyManager::space = ' '; 00138 char KeyManager::unknown = '?'; 00139 00140 #endif
Generated on Thu Jul 21 2022 14:44:34 by 1.7.2