💿🐜 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.

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