/src/ois/includes/OISKeyboard.h

https://bitbucket.org/cabalistic/ogredeps/ · C Header · 312 lines · 203 code · 19 blank · 90 comment · 0 complexity · 97a4ffefa8372badc51c4cccbcd4cf7a MD5 · raw file

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