Input.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include <Input.h>
  29. /**
  30. This map contains 4 different mappings from key identifiers to character codes. Each entry represents a different
  31. combination of shift and capslock state.
  32. */
  33. char ascii_map[4][51] =
  34. {
  35. // shift off and capslock off
  36. {
  37. 0,
  38. ' ',
  39. '0',
  40. '1',
  41. '2',
  42. '3',
  43. '4',
  44. '5',
  45. '6',
  46. '7',
  47. '8',
  48. '9',
  49. 'a',
  50. 'b',
  51. 'c',
  52. 'd',
  53. 'e',
  54. 'f',
  55. 'g',
  56. 'h',
  57. 'i',
  58. 'j',
  59. 'k',
  60. 'l',
  61. 'm',
  62. 'n',
  63. 'o',
  64. 'p',
  65. 'q',
  66. 'r',
  67. 's',
  68. 't',
  69. 'u',
  70. 'v',
  71. 'w',
  72. 'x',
  73. 'y',
  74. 'z',
  75. ';',
  76. '=',
  77. ',',
  78. '-',
  79. '.',
  80. '/',
  81. '`',
  82. '[',
  83. '\\',
  84. ']',
  85. '\'',
  86. 0,
  87. 0
  88. },
  89. // shift on and capslock off
  90. {
  91. 0,
  92. ' ',
  93. ')',
  94. '!',
  95. '@',
  96. '#',
  97. '$',
  98. '%',
  99. '^',
  100. '&',
  101. '*',
  102. '(',
  103. 'A',
  104. 'B',
  105. 'C',
  106. 'D',
  107. 'E',
  108. 'F',
  109. 'G',
  110. 'H',
  111. 'I',
  112. 'J',
  113. 'K',
  114. 'L',
  115. 'M',
  116. 'N',
  117. 'O',
  118. 'P',
  119. 'Q',
  120. 'R',
  121. 'S',
  122. 'T',
  123. 'U',
  124. 'V',
  125. 'W',
  126. 'X',
  127. 'Y',
  128. 'Z',
  129. ':',
  130. '+',
  131. '<',
  132. '_',
  133. '>',
  134. '?',
  135. '~',
  136. '{',
  137. '|',
  138. '}',
  139. '"',
  140. 0,
  141. 0
  142. },
  143. // shift on and capslock on
  144. {
  145. 0,
  146. ' ',
  147. ')',
  148. '!',
  149. '@',
  150. '#',
  151. '$',
  152. '%',
  153. '^',
  154. '&',
  155. '*',
  156. '(',
  157. 'a',
  158. 'b',
  159. 'c',
  160. 'd',
  161. 'e',
  162. 'f',
  163. 'g',
  164. 'h',
  165. 'i',
  166. 'j',
  167. 'k',
  168. 'l',
  169. 'm',
  170. 'n',
  171. 'o',
  172. 'p',
  173. 'q',
  174. 'r',
  175. 's',
  176. 't',
  177. 'u',
  178. 'v',
  179. 'w',
  180. 'x',
  181. 'y',
  182. 'z',
  183. ':',
  184. '+',
  185. '<',
  186. '_',
  187. '>',
  188. '?',
  189. '~',
  190. '{',
  191. '|',
  192. '}',
  193. '"',
  194. 0,
  195. 0
  196. },
  197. // shift off and capslock on
  198. {
  199. 0,
  200. ' ',
  201. '1',
  202. '2',
  203. '3',
  204. '4',
  205. '5',
  206. '6',
  207. '7',
  208. '8',
  209. '9',
  210. '0',
  211. 'A',
  212. 'B',
  213. 'C',
  214. 'D',
  215. 'E',
  216. 'F',
  217. 'G',
  218. 'H',
  219. 'I',
  220. 'J',
  221. 'K',
  222. 'L',
  223. 'M',
  224. 'N',
  225. 'O',
  226. 'P',
  227. 'Q',
  228. 'R',
  229. 'S',
  230. 'T',
  231. 'U',
  232. 'V',
  233. 'W',
  234. 'X',
  235. 'Y',
  236. 'Z',
  237. ';',
  238. '=',
  239. ',',
  240. '-',
  241. '.',
  242. '/',
  243. '`',
  244. '[',
  245. '\\',
  246. ']',
  247. '\'',
  248. 0,
  249. 0
  250. }
  251. };
  252. char keypad_map[2][18] =
  253. {
  254. {
  255. '0',
  256. '1',
  257. '2',
  258. '3',
  259. '4',
  260. '5',
  261. '6',
  262. '7',
  263. '8',
  264. '9',
  265. '\n',
  266. '*',
  267. '+',
  268. 0,
  269. '-',
  270. '.',
  271. '/',
  272. '='
  273. },
  274. {
  275. 0,
  276. 0,
  277. 0,
  278. 0,
  279. 0,
  280. 0,
  281. 0,
  282. 0,
  283. 0,
  284. 0,
  285. '\n',
  286. '*',
  287. '+',
  288. 0,
  289. '-',
  290. 0,
  291. '/',
  292. '='
  293. }
  294. };
  295. Rml::Core::Context* Input::context = nullptr;
  296. // Sets the context to send input events to.
  297. void Input::SetContext(Rml::Core::Context* _context)
  298. {
  299. context = _context;
  300. }
  301. // Returns the character code for a key identifer / key modifier combination.
  302. Rml::Core::Character Input::GetCharacterCode(Rml::Core::Input::KeyIdentifier key_identifier, int key_modifier_state)
  303. {
  304. using Rml::Core::Character;
  305. // Check if we have a keycode capable of generating characters on the main keyboard (ie, not on the numeric
  306. // keypad; that is dealt with below).
  307. if (key_identifier <= Rml::Core::Input::KI_OEM_102)
  308. {
  309. // Get modifier states
  310. bool shift = (key_modifier_state & Rml::Core::Input::KM_SHIFT) > 0;
  311. bool capslock = (key_modifier_state & Rml::Core::Input::KM_CAPSLOCK) > 0;
  312. // Return character code based on identifier and modifiers
  313. if (shift && !capslock)
  314. return (Character)ascii_map[1][key_identifier];
  315. if (shift && capslock)
  316. return (Character)ascii_map[2][key_identifier];
  317. if (!shift && capslock)
  318. return (Character)ascii_map[3][key_identifier];
  319. return (Character)ascii_map[0][key_identifier];
  320. }
  321. // Check if we have a keycode from the numeric keypad.
  322. else if (key_identifier <= Rml::Core::Input::KI_OEM_NEC_EQUAL)
  323. {
  324. if (key_modifier_state & Rml::Core::Input::KM_NUMLOCK)
  325. return (Character)keypad_map[0][key_identifier - Rml::Core::Input::KI_NUMPAD0];
  326. else
  327. return (Character)keypad_map[1][key_identifier - Rml::Core::Input::KI_NUMPAD0];
  328. }
  329. else if (key_identifier == Rml::Core::Input::KI_RETURN)
  330. return (Character)'\n';
  331. return Character::Null;
  332. }