KeyBind.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include "KeyBind.h"
  2. #define IM_VK_KEYPAD_ENTER (VK_RETURN + 256)
  3. bool KeyBind::SetToPressedKey(const std::string& path, const std::string& name, short* key) {
  4. short newKey = getPressedHotkey();
  5. if (IsKeyPressed(LegacyToInput(newKey))) {
  6. if (newKey == VK_ESCAPE) {
  7. *key = -1;
  8. config::setValue(path, name, -1);
  9. return true;
  10. }
  11. *key = newKey;
  12. config::setValue(path, name, newKey);
  13. return true;
  14. }
  15. return false;
  16. }
  17. std::string KeyBind::getKeyName(short key) {
  18. if (key > 5)
  19. return ImGui::GetKeyName(LegacyToInput(key));
  20. switch (key) {
  21. case ImGuiMouseButton_Left:
  22. return "LMB";
  23. case ImGuiMouseButton_Right:
  24. return "RMB";
  25. case ImGuiMouseButton_Middle:
  26. return "MMB";
  27. case 3:
  28. return "Mouse X1";
  29. case 4:
  30. return "Mouse X2";
  31. }
  32. return "Unknown";
  33. }
  34. short KeyBind::getPressedHotkey() {
  35. auto& io = ImGui::GetIO();
  36. for (short i = ImGuiKey_NamedKey_BEGIN; i < ImGuiKey_NamedKey_END - 4; i++) {
  37. bool isKeyDown = io.KeysDown[i];
  38. if (isKeyDown)
  39. return InputToLegacy(i);
  40. }
  41. }
  42. short KeyBind::InputToLegacy(short inputkey) {
  43. auto& io = ImGui::GetIO();
  44. if (inputkey > 4)
  45. return io.KeyMap[inputkey];
  46. switch (inputkey) {
  47. case ImGuiMouseButton_Left:
  48. return VK_LBUTTON;
  49. case ImGuiMouseButton_Right:
  50. return VK_RBUTTON;
  51. case ImGuiMouseButton_Middle:
  52. return VK_MBUTTON;
  53. case 3:
  54. return VK_XBUTTON1;
  55. case 4:
  56. return VK_XBUTTON2;
  57. }
  58. LOG_DEBUG("Failed to find legacy input");
  59. return -1;
  60. }
  61. bool KeyBind::IsKeyDown(ImGuiKey key) {
  62. if (key > 6)
  63. return ImGui::IsKeyDown(key);
  64. switch (key) {
  65. case 1:
  66. case 2:
  67. return ImGui::IsMouseDown(key - 1);
  68. case 4:
  69. case 5:
  70. case 6:
  71. return ImGui::IsMouseDown(key - 2);
  72. }
  73. return false;
  74. }
  75. static bool KeyBind::IsKeyPressed(ImGuiKey key) {
  76. if (key > 6)
  77. return ImGui::IsKeyPressed(key, false);
  78. switch (key) {
  79. case 1:
  80. case 2:
  81. return ImGui::IsMouseClicked(key - 1);
  82. case 4:
  83. case 5:
  84. case 6:
  85. return ImGui::IsMouseClicked(key - 2);
  86. }
  87. return false;
  88. }
  89. ImGuiKey KeyBind::LegacyToInput(short key) {
  90. switch (key) {
  91. case VK_TAB: return ImGuiKey_Tab;
  92. case VK_LEFT: return ImGuiKey_LeftArrow;
  93. case VK_RIGHT: return ImGuiKey_RightArrow;
  94. case VK_UP: return ImGuiKey_UpArrow;
  95. case VK_DOWN: return ImGuiKey_DownArrow;
  96. case VK_PRIOR: return ImGuiKey_PageUp;
  97. case VK_NEXT: return ImGuiKey_PageDown;
  98. case VK_HOME: return ImGuiKey_Home;
  99. case VK_END: return ImGuiKey_End;
  100. case VK_INSERT: return ImGuiKey_Insert;
  101. case VK_DELETE: return ImGuiKey_Delete;
  102. case VK_BACK: return ImGuiKey_Backspace;
  103. case VK_SPACE: return ImGuiKey_Space;
  104. case VK_RETURN: return ImGuiKey_Enter;
  105. case VK_ESCAPE: return ImGuiKey_Escape;
  106. case VK_OEM_7: return ImGuiKey_Apostrophe;
  107. case VK_OEM_COMMA: return ImGuiKey_Comma;
  108. case VK_OEM_MINUS: return ImGuiKey_Minus;
  109. case VK_OEM_PERIOD: return ImGuiKey_Period;
  110. case VK_OEM_2: return ImGuiKey_Slash;
  111. case VK_OEM_1: return ImGuiKey_Semicolon;
  112. case VK_OEM_PLUS: return ImGuiKey_Equal;
  113. case VK_OEM_4: return ImGuiKey_LeftBracket;
  114. case VK_OEM_5: return ImGuiKey_Backslash;
  115. case VK_OEM_6: return ImGuiKey_RightBracket;
  116. case VK_OEM_3: return ImGuiKey_GraveAccent;
  117. case VK_CAPITAL: return ImGuiKey_CapsLock;
  118. case VK_SCROLL: return ImGuiKey_ScrollLock;
  119. case VK_NUMLOCK: return ImGuiKey_NumLock;
  120. case VK_SNAPSHOT: return ImGuiKey_PrintScreen;
  121. case VK_PAUSE: return ImGuiKey_Pause;
  122. case VK_NUMPAD0: return ImGuiKey_Keypad0;
  123. case VK_NUMPAD1: return ImGuiKey_Keypad1;
  124. case VK_NUMPAD2: return ImGuiKey_Keypad2;
  125. case VK_NUMPAD3: return ImGuiKey_Keypad3;
  126. case VK_NUMPAD4: return ImGuiKey_Keypad4;
  127. case VK_NUMPAD5: return ImGuiKey_Keypad5;
  128. case VK_NUMPAD6: return ImGuiKey_Keypad6;
  129. case VK_NUMPAD7: return ImGuiKey_Keypad7;
  130. case VK_NUMPAD8: return ImGuiKey_Keypad8;
  131. case VK_NUMPAD9: return ImGuiKey_Keypad9;
  132. case VK_DECIMAL: return ImGuiKey_KeypadDecimal;
  133. case VK_DIVIDE: return ImGuiKey_KeypadDivide;
  134. case VK_MULTIPLY: return ImGuiKey_KeypadMultiply;
  135. case VK_SUBTRACT: return ImGuiKey_KeypadSubtract;
  136. case VK_ADD: return ImGuiKey_KeypadAdd;
  137. case IM_VK_KEYPAD_ENTER: return ImGuiKey_KeypadEnter;
  138. case VK_LSHIFT: return ImGuiKey_LeftShift;
  139. case VK_LCONTROL: return ImGuiKey_LeftCtrl;
  140. case VK_LMENU: return ImGuiKey_LeftAlt;
  141. case VK_LWIN: return ImGuiKey_LeftSuper;
  142. case VK_RSHIFT: return ImGuiKey_RightShift;
  143. case VK_RCONTROL: return ImGuiKey_RightCtrl;
  144. case VK_RMENU: return ImGuiKey_RightAlt;
  145. case VK_RWIN: return ImGuiKey_RightSuper;
  146. case VK_APPS: return ImGuiKey_Menu;
  147. case '0': return ImGuiKey_0;
  148. case '1': return ImGuiKey_1;
  149. case '2': return ImGuiKey_2;
  150. case '3': return ImGuiKey_3;
  151. case '4': return ImGuiKey_4;
  152. case '5': return ImGuiKey_5;
  153. case '6': return ImGuiKey_6;
  154. case '7': return ImGuiKey_7;
  155. case '8': return ImGuiKey_8;
  156. case '9': return ImGuiKey_9;
  157. case 'A': return ImGuiKey_A;
  158. case 'B': return ImGuiKey_B;
  159. case 'C': return ImGuiKey_C;
  160. case 'D': return ImGuiKey_D;
  161. case 'E': return ImGuiKey_E;
  162. case 'F': return ImGuiKey_F;
  163. case 'G': return ImGuiKey_G;
  164. case 'H': return ImGuiKey_H;
  165. case 'I': return ImGuiKey_I;
  166. case 'J': return ImGuiKey_J;
  167. case 'K': return ImGuiKey_K;
  168. case 'L': return ImGuiKey_L;
  169. case 'M': return ImGuiKey_M;
  170. case 'N': return ImGuiKey_N;
  171. case 'O': return ImGuiKey_O;
  172. case 'P': return ImGuiKey_P;
  173. case 'Q': return ImGuiKey_Q;
  174. case 'R': return ImGuiKey_R;
  175. case 'S': return ImGuiKey_S;
  176. case 'T': return ImGuiKey_T;
  177. case 'U': return ImGuiKey_U;
  178. case 'V': return ImGuiKey_V;
  179. case 'W': return ImGuiKey_W;
  180. case 'X': return ImGuiKey_X;
  181. case 'Y': return ImGuiKey_Y;
  182. case 'Z': return ImGuiKey_Z;
  183. case VK_F1: return ImGuiKey_F1;
  184. case VK_F2: return ImGuiKey_F2;
  185. case VK_F3: return ImGuiKey_F3;
  186. case VK_F4: return ImGuiKey_F4;
  187. case VK_F5: return ImGuiKey_F5;
  188. case VK_F6: return ImGuiKey_F6;
  189. case VK_F7: return ImGuiKey_F7;
  190. case VK_F8: return ImGuiKey_F8;
  191. case VK_F9: return ImGuiKey_F9;
  192. case VK_F10: return ImGuiKey_F10;
  193. case VK_F11: return ImGuiKey_F11;
  194. case VK_F12: return ImGuiKey_F12;
  195. //case VK_LBUTTON: return ImGuiMouseButton_Left;
  196. //case VK_RBUTTON: return ImGuiMouseButton_Right;
  197. //case VK_MBUTTON: return ImGuiMouseButton_Middle;
  198. //case VK_XBUTTON1: return 3;
  199. //case VK_XBUTTON2: return 4;
  200. default: return ImGuiKey_None;
  201. }
  202. }