💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

341 lines
7.5 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "keyboard.hpp"
  20. #include "scancode.hpp"
  21. #include "event/event-dispatcher.hpp"
  22. #include "event/input-events.hpp"
  23. const char* keyboard::get_scancode_name(scancode scancode)
  24. {
  25. return scancode_names[static_cast<std::size_t>(scancode)];
  26. };
  27. scancode keyboard::get_scancode_from_name(const char* name)
  28. {
  29. auto it = scancode_map.find(std::string(name));
  30. if (it == scancode_map.end())
  31. {
  32. return scancode::unknown;
  33. }
  34. return it->second;
  35. }
  36. keyboard::keyboard()
  37. {}
  38. keyboard::~keyboard()
  39. {}
  40. void keyboard::press(scancode scancode)
  41. {
  42. if (!input_device::event_dispatcher)
  43. {
  44. return;
  45. }
  46. key_pressed_event event;
  47. event.keyboard = this;
  48. event.scancode = scancode;
  49. input_device::event_dispatcher->queue(event);
  50. }
  51. void keyboard::release(scancode scancode)
  52. {
  53. if (!input_device::event_dispatcher)
  54. {
  55. return;
  56. }
  57. key_released_event event;
  58. event.keyboard = this;
  59. event.scancode = scancode;
  60. input_device::event_dispatcher->queue(event);
  61. }
  62. std::map<std::string, scancode> keyboard::build_scancode_map()
  63. {
  64. std::map<std::string, scancode> scancode_map;
  65. for (std::size_t i = 0; i <= static_cast<std::size_t>(scancode::audio_fast_forward); ++i)
  66. {
  67. if (scancode_names[i] != nullptr)
  68. {
  69. std::string scancode_name = scancode_names[i];
  70. scancode_map[scancode_name] = static_cast<scancode>(i);
  71. }
  72. }
  73. return scancode_map;
  74. }
  75. const char* keyboard::scancode_names[] =
  76. {
  77. nullptr, // UNKNOWN
  78. "A", // A
  79. "B", // B
  80. "C", // C
  81. "D", // D
  82. "E", // E
  83. "F", // F
  84. "G", // G
  85. "H", // H
  86. "I", // I
  87. "J", // J
  88. "K", // K
  89. "L", // L
  90. "M", // M
  91. "N", // N
  92. "O", // O
  93. "P", // P
  94. "Q", // Q
  95. "R", // R
  96. "S", // S
  97. "T", // T
  98. "U", // U
  99. "V", // V
  100. "W", // W
  101. "X", // X
  102. "Y", // Y
  103. "Z", // Z
  104. "1", // ONE
  105. "2", // TWO
  106. "3", // THREE
  107. "4", // FOUR
  108. "5", // FIVE
  109. "6", // SIX
  110. "7", // SEVEN
  111. "8", // EIGHT
  112. "9", // NINE
  113. "0", // ZERO
  114. "Enter", // ENTER
  115. "Escape", // ESCAPE
  116. "Backspace", // BACKSPACE
  117. "Tab", // TAB
  118. "Space", // SPACE
  119. "-", // MINUS
  120. "=", // EQUALS
  121. "[", // LEFTBRACKET
  122. "]", // RIGHTBRACKET
  123. "\\", // BACKSLASH
  124. "#", // NONUSHASH
  125. ";", // SEMICOLON
  126. "'", // APOSTROPHE
  127. "`", // GRAVE
  128. ",", // COMMA
  129. ".", // PERIOD
  130. "/", // SLASH
  131. "Caps Lock", // CAPSLOCK
  132. "F1", // F1
  133. "F2", // F2
  134. "F3", // F3
  135. "F4", // F4
  136. "F5", // F5
  137. "F6", // F6
  138. "F7", // F7
  139. "F8", // F8
  140. "F9", // F9
  141. "F10", // F10
  142. "F11", // F11
  143. "F12", // F12
  144. "Print Screen", // PRINTSCREEN
  145. "Scroll Lock", // SCROLLLOCK
  146. "Pause", // PAUSE
  147. "Insert", // INSERT
  148. "Home", // HOME
  149. "Page Up", // PAGEUP
  150. "Delete", // DELETE
  151. "End", // END
  152. "Page Down", // PAGEDOWN
  153. "Right", // RIGHT
  154. "Left", // LEFT
  155. "Down", // DOWN
  156. "Up", // UP
  157. "Num Lock", // NUMLOCKCLEAR
  158. "Keypad /", // KP_DIVIDE
  159. "Keypad *", // KP_MULTIPLY
  160. "Keypad -", // KP_MINUS
  161. "Keypad +", // KP_PLUS
  162. "Keypad Enter", // KP_ENTER
  163. "Keypad 1", // KP_1
  164. "Keypad 2", // KP_2
  165. "Keypad 3", // KP_3
  166. "Keypad 4", // KP_4
  167. "Keypad 5", // KP_5
  168. "Keypad 6", // KP_6
  169. "Keypad 7", // KP_7
  170. "Keypad 8", // KP_8
  171. "Keypad 9", // KP_9
  172. "Keypad 0", // KP_0
  173. "Keypad .", // KP_PERIOD
  174. nullptr, // NONUSBACKSLASH
  175. "Application", // APPLICATION
  176. "Power", // POWER
  177. "Keypad =", // KP_EQUALS
  178. "F13", // F13
  179. "F14", // F14
  180. "F15", // F15
  181. "F16", // F16
  182. "F17", // F17
  183. "F18", // F18
  184. "F19", // F19
  185. "F20", // F20
  186. "F21", // F21
  187. "F22", // F22
  188. "F23", // F23
  189. "F24", // F24
  190. "Execute", // EXECUTE
  191. "Help", // HELP
  192. "Menu", // MENU
  193. "Select", // SELECT
  194. "Stop", // STOP
  195. "Again", // AGAIN
  196. "Undo", // UNDO
  197. "Cut", // CUT
  198. "Copy", // COPY
  199. "Paste", // PASTE
  200. "Find", // FIND
  201. "Mute", // MUTE
  202. "Volume Up", // VOLUMEUP
  203. "Volume Down", // VOLUMEDOWN
  204. nullptr, // LOCKINGCAPSLOCK
  205. nullptr, // LOCKINGNUMLOCK
  206. nullptr, // LOCKINGSCROLLLOCK
  207. "Keypad ,", // KP_COMMA
  208. "Keypad = (AS400)", // KP_EQUALSAS400
  209. nullptr, // INTERNATIONAL1
  210. nullptr, // INTERNATIONAL2
  211. nullptr, // INTERNATIONAL3
  212. nullptr, // INTERNATIONAL4
  213. nullptr, // INTERNATIONAL5
  214. nullptr, // INTERNATIONAL6
  215. nullptr, // INTERNATIONAL7
  216. nullptr, // INTERNATIONAL8
  217. nullptr, // INTERNATIONAL9
  218. nullptr, // LANG1
  219. nullptr, // LANG2
  220. nullptr, // LANG3
  221. nullptr, // LANG4
  222. nullptr, // LANG5
  223. nullptr, // LANG6
  224. nullptr, // LANG7
  225. nullptr, // LANG8
  226. nullptr, // LANG9
  227. "Alt Erase", // ALTERASE
  228. "Sys Req", // SYSREQ
  229. "Cancel", // CANCEL
  230. "Clear", // CLEAR
  231. "Prior", // PRIOR
  232. "Return", // RETURN2
  233. "Separator", // SEPARATOR
  234. "Out", // OUT
  235. "Oper", // OPER
  236. "Clear/Again", // CLEARAGAIN
  237. "CrSel", // CRSEL
  238. "ExSel", // EXSEL
  239. "Keypad 00", // KP_00
  240. "Keypad 000", // KP_000
  241. "Thousands Separator", // THOUSANDSSEPARATOR
  242. "Decimal Separator", // DECIMALSEPARATOR
  243. "Currency Unit", // CURRENCYUNIT
  244. "Currency Sub-Unit", // CURRENCYSUBUNIT
  245. "Keypad (", // KP_LEFTPAREN
  246. "Keypad )", // KP_RIGHTPAREN
  247. "Keypad {", // KP_LEFTBRACE
  248. "Keypad }", // KP_RIGHTBRACE
  249. "Keypad Tab", // KP_TAB
  250. "Keypad Backspace", // KP_BACKSPACE
  251. "Keypad A", // KP_A
  252. "Keypad B", // KP_B
  253. "Keypad C", // KP_C
  254. "Keypad D", // KP_D
  255. "Keypad E", // KP_E
  256. "Keypad F", // KP_F
  257. "Keypad XOR", // KP_XOR
  258. "Keypad ^", // KP_POWER
  259. "Keypad %", // KP_PERCENT
  260. "Keypad <", // KP_LESS
  261. "Keypad >", // KP_GREATER
  262. "Keypad &", // KP_AMPERSAND
  263. "Keypad &&", // KP_DBLAMPERSAND
  264. "Keypad |", // KP_VERTICALBAR
  265. "Keypad ||", // KP_DBLVERTICALBAR
  266. "Keypad :", // KP_COLON
  267. "Keypad #", // KP_HASH
  268. "Keypad Space", // KP_SPACE
  269. "Keypad @", // KP_AT
  270. "Keypad !", // KP_EXCLAM
  271. "Keypad Mem Store", // KP_MEMSTORE
  272. "Keypad Mem Recall", // KP_MEMRECALL
  273. "Keypad Mem Clear", // KP_MEMCLEAR
  274. "Keypad Mem Add", // KP_MEMADD
  275. "Keypad Mem Subtract", // KP_MEMSUBTRACT
  276. "Keypad Mem Multiply", // KP_MEMMULTIPLY
  277. "Keypad Mem Divide", // KP_MEMDIVIDE
  278. "Keypad +/-", // KP_PLUSMINUS
  279. "Keypad Clear", // KP_CLEAR
  280. "Keypad Clear Entry", // KP_CLEARENTRY
  281. "Keypad Binary", // KP_BINARY
  282. "Keypad Octal", // KP_OCTAL
  283. "Keypad Decimal", // KP_DECIMAL
  284. "Keypad Hexadecimal", // KP_HEXADECIMAL
  285. "Left Ctrl", // LCTRL
  286. "Left Shift", // LSHIFT
  287. "Left Alt", // LALT
  288. "Left GUI", // LGUI
  289. "Right Ctrl", // RCTRL
  290. "Right Shift", // RSHIFT
  291. "Right Alt", // RALT
  292. "Right GUI", // RGUI
  293. "Mode Switch", // MODE
  294. "Audio Next", // AUDIONEXT
  295. "Audio Prev", // AUDIOPREV
  296. "Audio Stop", // AUDIOSTOP
  297. "Audio Play", // AUDIOPLAY
  298. "Audio Mute", // AUDIOMUTE
  299. "Media Select", // MEDIASELECT
  300. "WWW", // WWW
  301. "Mail", // MAIL
  302. "Calculator", // CALCULATOR
  303. "Computer", // COMPUTER
  304. "AC Search", // AC_SEARCH
  305. "AC Home", // AC_HOME
  306. "AC Back", // AC_BACK
  307. "AC Forward", // AC_FORWARD
  308. "AC Stop", // AC_STOP
  309. "AC Refresh", // AC_REFRESH
  310. "AC Bookmarks", // AC_BOOKMARKS
  311. "Brightness Down", // BRIGHTNESSDOWN
  312. "Brightness Up", // BRIGHTNESSUP
  313. "Display Switch", // DISPLAYSWITCH
  314. "KBD Illum Toggle", // KBDILLUMTOGGLE
  315. "KBD Illum Down", // KBDILLUMDOWN
  316. "KBD Illum Up", // KBDILLUMUP
  317. "Eject", // EJECT
  318. "Sleep", // SLEEP
  319. "App 1", // APP1
  320. "App 2", // APP2
  321. "Audio Rewind", // AUDIOREWIND
  322. "Audio Fast-Forward", // AUDIOFASTFORWARD
  323. };
  324. std::map<std::string, scancode> keyboard::scancode_map = keyboard::build_scancode_map();