1 /* 2 The zlib/libpng License 3 4 Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com) 5 6 This software is provided 'as-is', without any express or implied warranty. In no event will 7 the authors be held liable for any damages arising from the use of this software. 8 9 Permission is granted to anyone to use this software for any purpose, including commercial 10 applications, and to alter it and redistribute it freely, subject to the following 11 restrictions: 12 13 1. The origin of this software must not be misrepresented; you must not claim that 14 you wrote the original software. If you use this software in a product, 15 an acknowledgment in the product documentation would be appreciated but is 16 not required. 17 18 2. Altered source versions must be plainly marked as such, and must not be 19 misrepresented as being the original software. 20 21 3. This notice may not be removed or altered from any source distribution. 22 */ 23 #ifndef OIS_Keyboard_H 24 #define OIS_Keyboard_H 25 #include "OISObject.h" 26 #include "OISEvents.h" 27 28 namespace OIS 29 { 30 //! Keyboard scan codes 31 enum KeyCode 32 { 33 KC_UNASSIGNED = 0x00, 34 KC_ESCAPE = 0x01, 35 KC_1 = 0x02, 36 KC_2 = 0x03, 37 KC_3 = 0x04, 38 KC_4 = 0x05, 39 KC_5 = 0x06, 40 KC_6 = 0x07, 41 KC_7 = 0x08, 42 KC_8 = 0x09, 43 KC_9 = 0x0A, 44 KC_0 = 0x0B, 45 KC_MINUS = 0x0C, // - on main keyboard 46 KC_EQUALS = 0x0D, 47 KC_BACK = 0x0E, // backspace 48 KC_TAB = 0x0F, 49 KC_Q = 0x10, 50 KC_W = 0x11, 51 KC_E = 0x12, 52 KC_R = 0x13, 53 KC_T = 0x14, 54 KC_Y = 0x15, 55 KC_U = 0x16, 56 KC_I = 0x17, 57 KC_O = 0x18, 58 KC_P = 0x19, 59 KC_LBRACKET = 0x1A, 60 KC_RBRACKET = 0x1B, 61 KC_RETURN = 0x1C, // Enter on main keyboard 62 KC_LCONTROL = 0x1D, 63 KC_A = 0x1E, 64 KC_S = 0x1F, 65 KC_D = 0x20, 66 KC_F = 0x21, 67 KC_G = 0x22, 68 KC_H = 0x23, 69 KC_J = 0x24, 70 KC_K = 0x25, 71 KC_L = 0x26, 72 KC_SEMICOLON = 0x27, 73 KC_APOSTROPHE = 0x28, 74 KC_GRAVE = 0x29, // accent 75 KC_LSHIFT = 0x2A, 76 KC_BACKSLASH = 0x2B, 77 KC_Z = 0x2C, 78 KC_X = 0x2D, 79 KC_C = 0x2E, 80 KC_V = 0x2F, 81 KC_B = 0x30, 82 KC_N = 0x31, 83 KC_M = 0x32, 84 KC_COMMA = 0x33, 85 KC_PERIOD = 0x34, // . on main keyboard 86 KC_SLASH = 0x35, // / on main keyboard 87 KC_RSHIFT = 0x36, 88 KC_MULTIPLY = 0x37, // * on numeric keypad 89 KC_LMENU = 0x38, // left Alt 90 KC_SPACE = 0x39, 91 KC_CAPITAL = 0x3A, 92 KC_F1 = 0x3B, 93 KC_F2 = 0x3C, 94 KC_F3 = 0x3D, 95 KC_F4 = 0x3E, 96 KC_F5 = 0x3F, 97 KC_F6 = 0x40, 98 KC_F7 = 0x41, 99 KC_F8 = 0x42, 100 KC_F9 = 0x43, 101 KC_F10 = 0x44, 102 KC_NUMLOCK = 0x45, 103 KC_SCROLL = 0x46, // Scroll Lock 104 KC_NUMPAD7 = 0x47, 105 KC_NUMPAD8 = 0x48, 106 KC_NUMPAD9 = 0x49, 107 KC_SUBTRACT = 0x4A, // - on numeric keypad 108 KC_NUMPAD4 = 0x4B, 109 KC_NUMPAD5 = 0x4C, 110 KC_NUMPAD6 = 0x4D, 111 KC_ADD = 0x4E, // + on numeric keypad 112 KC_NUMPAD1 = 0x4F, 113 KC_NUMPAD2 = 0x50, 114 KC_NUMPAD3 = 0x51, 115 KC_NUMPAD0 = 0x52, 116 KC_DECIMAL = 0x53, // . on numeric keypad 117 KC_OEM_102 = 0x56, // < > | on UK/Germany keyboards 118 KC_F11 = 0x57, 119 KC_F12 = 0x58, 120 KC_F13 = 0x64, // (NEC PC98) 121 KC_F14 = 0x65, // (NEC PC98) 122 KC_F15 = 0x66, // (NEC PC98) 123 KC_KANA = 0x70, // (Japanese keyboard) 124 KC_ABNT_C1 = 0x73, // / ? on Portugese (Brazilian) keyboards 125 KC_CONVERT = 0x79, // (Japanese keyboard) 126 KC_NOCONVERT = 0x7B, // (Japanese keyboard) 127 KC_YEN = 0x7D, // (Japanese keyboard) 128 KC_ABNT_C2 = 0x7E, // Numpad . on Portugese (Brazilian) keyboards 129 KC_NUMPADEQUALS= 0x8D, // = on numeric keypad (NEC PC98) 130 KC_PREVTRACK = 0x90, // Previous Track (KC_CIRCUMFLEX on Japanese keyboard) 131 KC_AT = 0x91, // (NEC PC98) 132 KC_COLON = 0x92, // (NEC PC98) 133 KC_UNDERLINE = 0x93, // (NEC PC98) 134 KC_KANJI = 0x94, // (Japanese keyboard) 135 KC_STOP = 0x95, // (NEC PC98) 136 KC_AX = 0x96, // (Japan AX) 137 KC_UNLABELED = 0x97, // (J3100) 138 KC_NEXTTRACK = 0x99, // Next Track 139 KC_NUMPADENTER = 0x9C, // Enter on numeric keypad 140 KC_RCONTROL = 0x9D, 141 KC_MUTE = 0xA0, // Mute 142 KC_CALCULATOR = 0xA1, // Calculator 143 KC_PLAYPAUSE = 0xA2, // Play / Pause 144 KC_MEDIASTOP = 0xA4, // Media Stop 145 KC_VOLUMEDOWN = 0xAE, // Volume - 146 KC_VOLUMEUP = 0xB0, // Volume + 147 KC_WEBHOME = 0xB2, // Web home 148 KC_NUMPADCOMMA = 0xB3, // , on numeric keypad (NEC PC98) 149 KC_DIVIDE = 0xB5, // / on numeric keypad 150 KC_SYSRQ = 0xB7, 151 KC_RMENU = 0xB8, // right Alt 152 KC_PAUSE = 0xC5, // Pause 153 KC_HOME = 0xC7, // Home on arrow keypad 154 KC_UP = 0xC8, // UpArrow on arrow keypad 155 KC_PGUP = 0xC9, // PgUp on arrow keypad 156 KC_LEFT = 0xCB, // LeftArrow on arrow keypad 157 KC_RIGHT = 0xCD, // RightArrow on arrow keypad 158 KC_END = 0xCF, // End on arrow keypad 159 KC_DOWN = 0xD0, // DownArrow on arrow keypad 160 KC_PGDOWN = 0xD1, // PgDn on arrow keypad 161 KC_INSERT = 0xD2, // Insert on arrow keypad 162 KC_DELETE = 0xD3, // Delete on arrow keypad 163 KC_LWIN = 0xDB, // Left Windows key 164 KC_RWIN = 0xDC, // Right Windows key 165 KC_APPS = 0xDD, // AppMenu key 166 KC_POWER = 0xDE, // System Power 167 KC_SLEEP = 0xDF, // System Sleep 168 KC_WAKE = 0xE3, // System Wake 169 KC_WEBSEARCH = 0xE5, // Web Search 170 KC_WEBFAVORITES= 0xE6, // Web Favorites 171 KC_WEBREFRESH = 0xE7, // Web Refresh 172 KC_WEBSTOP = 0xE8, // Web Stop 173 KC_WEBFORWARD = 0xE9, // Web Forward 174 KC_WEBBACK = 0xEA, // Web Back 175 KC_MYCOMPUTER = 0xEB, // My Computer 176 KC_MAIL = 0xEC, // Mail 177 KC_MEDIASELECT = 0xED // Media Select 178 }; 179 180 /** 181 Specialised for key events 182 */ 183 class _OISExport KeyEvent : public EventArg 184 { 185 public: 186 KeyEvent(Object* obj, KeyCode kc, unsigned int txt) : EventArg(obj), key(kc), text(txt) {} 187 virtual ~KeyEvent() {} 188 189 //! KeyCode of event 190 const KeyCode key; 191 //! Text character, depends on current TextTranslationMode 192 unsigned int text; 193 }; 194 195 /** 196 To recieve buffered keyboard input, derive a class from this, and implement the 197 methods here. Then set the call back to your Keyboard instance with Keyboard::setEventCallback 198 */ 199 class _OISExport KeyListener 200 { 201 public: 202 virtual ~KeyListener() {} 203 virtual bool keyPressed(const KeyEvent &arg) = 0; 204 virtual bool keyReleased(const KeyEvent &arg) = 0; 205 }; 206 207 /** 208 Keyboard base class. To be implemented by specific system (ie. DirectX Keyboard) 209 This class is useful as you remain OS independent using this common interface. 210 */ 211 class _OISExport Keyboard : public Object 212 { 213 public: 214 virtual ~Keyboard() {}; 215 216 /** 217 @remarks 218 Returns true if key is donwn 219 @param key 220 A KeyCode to check 221 */ 222 virtual bool isKeyDown(KeyCode key) const = 0; 223 224 /** 225 @remarks 226 Register/unregister a Keyboard Listener - Only one allowed for simplicity. If broadcasting 227 is neccessary, just broadcast from the callback you registered. 228 @param keyListener 229 Send a pointer to a class derived from KeyListener or 0 to clear the callback 230 */ 231 virtual void setEventCallback(KeyListener *keyListener) { mListener = keyListener;} 232 233 /** 234 @remarks 235 Returns currently set callback.. or 0 236 */ 237 KeyListener* getEventCallback() const {return mListener;} 238 239 //! TextTranslation Mode 240 enum TextTranslationMode 241 { 242 Off, 243 Unicode, 244 Ascii 245 }; 246 247 /** 248 @remarks 249 Enable extra processing to translate KC_*** to an 250 actual text character based off of locale. Different 251 managers may implement none or all. Check the 252 translation mode after setting to be sure 253 @param mode 254 Off, Unicode, Ascii 255 */ 256 virtual void setTextTranslation(TextTranslationMode mode); 257 258 /** 259 @remarks 260 Returns current translation mode 261 */ 262 TextTranslationMode getTextTranslation() const {return mTextMode;} 263 264 /** 265 @remarks 266 Translates KeyCode to string representation. 267 For example, KC_ENTER will be "Enter" - Locale 268 specific of course. 269 @param kc 270 KeyCode to convert 271 @returns 272 The string as determined from the current locale 273 */ 274 virtual const std::string& getAsString(KeyCode kc) = 0; 275 276 //! Enum of bit position of modifer 277 enum Modifier 278 { 279 Shift = 0x0000001, 280 Ctrl = 0x0000010, 281 Alt = 0x0000100 282 }; 283 284 /** 285 @remarks 286 Check modifier status 287 */ 288 bool isModifierDown(Modifier mod) const; 289 290 /** 291 @remarks 292 Copies the state of the keys into the sent buffer 293 (in the form of 1 is down and 0 is up) 294 */ 295 virtual void copyKeyStates(char keys[256]) const = 0; 296 297 protected: 298 Keyboard(const std::string &vendor, bool buffered, int devID, InputManager* creator) 299 : Object(vendor, OISKeyboard, buffered, devID, creator), 300 mModifiers(0), mListener(0), mTextMode(Unicode) {} 301 302 //! Bit field that holds status of Alt, Ctrl, Shift 303 unsigned int mModifiers; 304 305 //! Used for buffered/actionmapping callback 306 KeyListener *mListener; 307 308 //! The current translation mode 309 TextTranslationMode mTextMode; 310 }; 311 } 312 #endif 313