keyboard.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #if defined(Hiro_Keyboard)
  2. Keyboard::State Keyboard::state;
  3. const vector<string> Keyboard::keys = {
  4. //physical keyboard buttons
  5. "Escape", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12",
  6. "PrintScreen", "ScrollLock", "Pause",
  7. "Insert", "Delete", "Home", "End", "PageUp", "PageDown",
  8. "Up", "Down", "Left", "Right",
  9. "Grave", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "Dash", "Equal", "Backspace",
  10. "Tab", "CapsLock", "LeftEnter", "LeftShift", "RightShift",
  11. "LeftControl", "RightControl", "LeftAlt", "RightAlt", "LeftSuper", "RightSuper", "Menu", "Space",
  12. "OpenBracket", "CloseBracket", "Backslash", "Semicolon", "Apostrophe", "Comma", "Period", "Slash",
  13. "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
  14. "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
  15. "NumLock", "Divide", "Multiply", "Subtract", "Add", "RightEnter", "Point",
  16. "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Zero",
  17. //group aliases
  18. "Shift", //"LeftShift" | "RightShift"
  19. "Control", //"LeftControl" | "RightControl"
  20. "Alt", //"LeftAlt" | "RightAlt"
  21. "Super", //"LeftSuper" | "RightSuper"
  22. "Enter", //"LeftEnter" | "RightEnter"
  23. };
  24. auto Keyboard::append(Hotkey hotkey) -> void {
  25. state.hotkeys.append(hotkey);
  26. }
  27. auto Keyboard::hotkey(unsigned position) -> Hotkey {
  28. if(position < hotkeyCount()) return state.hotkeys[position];
  29. return {};
  30. }
  31. auto Keyboard::hotkeyCount() -> unsigned {
  32. return state.hotkeys.size();
  33. }
  34. auto Keyboard::hotkeys() -> vector<Hotkey> {
  35. return state.hotkeys;
  36. }
  37. auto Keyboard::poll() -> vector<bool> {
  38. auto pressed = pKeyboard::poll();
  39. for(auto& hotkey : state.hotkeys) {
  40. bool active = hotkey.state->sequence.size() > 0;
  41. for(auto& key : hotkey.state->keys) {
  42. if(pressed[key]) continue;
  43. active = false;
  44. break;
  45. }
  46. if(hotkey.state->active != active) {
  47. hotkey.state->active = active;
  48. active ? hotkey.doPress() : hotkey.doRelease();
  49. }
  50. }
  51. return pressed;
  52. }
  53. auto Keyboard::pressed(const string& key) -> bool {
  54. if(auto code = keys.find(key)) return pKeyboard::pressed(*code);
  55. return false;
  56. }
  57. auto Keyboard::released(const string& key) -> bool {
  58. return !pressed(key);
  59. }
  60. auto Keyboard::remove(Hotkey hotkey) -> void {
  61. if(auto offset = state.hotkeys.find(hotkey)) {
  62. state.hotkeys.remove(*offset);
  63. }
  64. }
  65. #endif