Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

152 řádky
6.9 KiB

  1. #include "LuaUtility.h"
  2. #include <windows.h>
  3. #include <WinUser.h>
  4. #include <string>
  5. static std::unordered_map<std::string, uint8_t> keyMap;
  6. LuaUtility::LuaUtility()
  7. {
  8. keyMap = GetKeyMap();
  9. }
  10. bool LuaUtility::IsKeyPressed(std::string keyName)
  11. {
  12. if (keyName.length() > 1) {
  13. std::transform(keyName.begin(), keyName.end(), keyName.begin(), [](unsigned char c) {return std::tolower(c); });
  14. auto keycode = GetKeyMap()[keyName];
  15. return (GetAsyncKeyState(GetKeyMap()[keyName]) & 0x8000) != 0;
  16. }
  17. else {
  18. std::transform(keyName.begin(), keyName.end(), keyName.begin(), [](unsigned char c) {return std::toupper(c); });
  19. return (GetAsyncKeyState(keyName.c_str()[0]) & 0x8000) != 0;
  20. }
  21. return false;
  22. }
  23. bool LuaUtility::IsKeyPressed(std::string keyName, std::string keyName2)
  24. {
  25. std::vector<uint8_t> keys;
  26. std::string current = keyName;
  27. for (int i = 0; i < 2; i++) {
  28. if (current.length() > 1) {
  29. std::transform(current.begin(), current.end(), current.begin(), [](unsigned char c) {return std::tolower(c); });
  30. keys.push_back(GetKeyMap()[current]);
  31. }
  32. else {
  33. std::transform(current.begin(), current.end(), current.begin(), [](unsigned char c) {return std::toupper(c); });
  34. keys.push_back(current.c_str()[0]);
  35. }
  36. current = keyName2;
  37. }
  38. return ((GetAsyncKeyState(keys[0]) & 0x8000) && GetAsyncKeyState(keys[1]) & 0x8000) != 0;
  39. }
  40. bool LuaUtility::IsKeyPressed(std::string keyName, std::string keyName2, std::string keyName3)
  41. {
  42. std::vector<uint8_t> keys;
  43. std::string current = keyName;
  44. for (int i = 0; i < 3; i++) {
  45. if (current.length() > 1) {
  46. std::transform(current.begin(), current.end(), current.begin(), [](unsigned char c) {return std::tolower(c); });
  47. keys.push_back(GetKeyMap()[current]);
  48. }
  49. else {
  50. std::transform(current.begin(), current.end(), current.begin(), [](unsigned char c) {return std::toupper(c); });
  51. keys.push_back(current.c_str()[0]);
  52. }
  53. if (i == 0)
  54. current = keyName2;
  55. else
  56. current = keyName3;
  57. }
  58. return ((GetAsyncKeyState(keys[0]) & 0x8000) && GetAsyncKeyState(keys[1] & 0x8000) && GetAsyncKeyState(keys[2]) & 0x8000) != 0;
  59. }
  60. bool LuaUtility::ContainsKey(std::string keyName) {
  61. if (keyMap.size() == 0)
  62. keyMap = GetKeyMap();
  63. std::transform(keyName.begin(), keyName.end(), keyName.begin(), [](unsigned char c) {return std::tolower(c); });
  64. return keyMap.find(keyName) != keyMap.end();
  65. }
  66. std::unordered_map<std::string, uint8_t> LuaUtility::GetKeyMap()
  67. {
  68. if (!keyMap.size()) {
  69. keyMap.insert(std::pair<std::string, uint8_t>("backspace", VK_BACK));
  70. keyMap.insert(std::pair<std::string, uint8_t>("tab", VK_TAB));
  71. keyMap.insert(std::pair<std::string, uint8_t>("clear", VK_CLEAR));
  72. keyMap.insert(std::pair<std::string, uint8_t>("return", VK_RETURN));
  73. keyMap.insert(std::pair<std::string, uint8_t>("enter", VK_RETURN));
  74. keyMap.insert(std::pair<std::string, uint8_t>("shift", VK_SHIFT));
  75. keyMap.insert(std::pair<std::string, uint8_t>("ctrl", VK_CONTROL));
  76. keyMap.insert(std::pair<std::string, uint8_t>("alt", VK_MENU));
  77. keyMap.insert(std::pair<std::string, uint8_t>("pause", VK_PAUSE));
  78. keyMap.insert(std::pair<std::string, uint8_t>("capslock", VK_CAPITAL));
  79. keyMap.insert(std::pair<std::string, uint8_t>("escape", VK_ESCAPE));
  80. keyMap.insert(std::pair<std::string, uint8_t>("space", VK_SPACE));
  81. keyMap.insert(std::pair<std::string, uint8_t>("pageup", VK_PRIOR));
  82. keyMap.insert(std::pair<std::string, uint8_t>("pagedown", VK_NEXT));
  83. keyMap.insert(std::pair<std::string, uint8_t>("end", VK_END));
  84. keyMap.insert(std::pair<std::string, uint8_t>("home", VK_HOME));
  85. keyMap.insert(std::pair<std::string, uint8_t>("left", VK_LEFT));
  86. keyMap.insert(std::pair<std::string, uint8_t>("up", VK_UP));
  87. keyMap.insert(std::pair<std::string, uint8_t>("right", VK_RIGHT));
  88. keyMap.insert(std::pair<std::string, uint8_t>("down", VK_DOWN));
  89. keyMap.insert(std::pair<std::string, uint8_t>("printscr", VK_SNAPSHOT));
  90. keyMap.insert(std::pair<std::string, uint8_t>("insert", VK_INSERT));
  91. keyMap.insert(std::pair<std::string, uint8_t>("delete", VK_DELETE));
  92. keyMap.insert(std::pair<std::string, uint8_t>("f1", VK_F1));
  93. keyMap.insert(std::pair<std::string, uint8_t>("f2", VK_F2));
  94. keyMap.insert(std::pair<std::string, uint8_t>("f3", VK_F3));
  95. keyMap.insert(std::pair<std::string, uint8_t>("f4", VK_F4));
  96. keyMap.insert(std::pair<std::string, uint8_t>("f5", VK_F5));
  97. keyMap.insert(std::pair<std::string, uint8_t>("f6", VK_F6));
  98. keyMap.insert(std::pair<std::string, uint8_t>("f7", VK_F7));
  99. keyMap.insert(std::pair<std::string, uint8_t>("f8", VK_F8));
  100. keyMap.insert(std::pair<std::string, uint8_t>("f9", VK_F9));
  101. keyMap.insert(std::pair<std::string, uint8_t>("f10", VK_F10));
  102. keyMap.insert(std::pair<std::string, uint8_t>("f11", VK_F11));
  103. keyMap.insert(std::pair<std::string, uint8_t>("f12", VK_F12));
  104. keyMap.insert(std::pair<std::string, uint8_t>("numpad0", VK_NUMPAD0));
  105. keyMap.insert(std::pair<std::string, uint8_t>("numpad1", VK_NUMPAD1));
  106. keyMap.insert(std::pair<std::string, uint8_t>("numpad2", VK_NUMPAD2));
  107. keyMap.insert(std::pair<std::string, uint8_t>("numpad3", VK_NUMPAD3));
  108. keyMap.insert(std::pair<std::string, uint8_t>("numpad4", VK_NUMPAD4));
  109. keyMap.insert(std::pair<std::string, uint8_t>("numpad5", VK_NUMPAD5));
  110. keyMap.insert(std::pair<std::string, uint8_t>("numpad6", VK_NUMPAD6));
  111. keyMap.insert(std::pair<std::string, uint8_t>("numpad7", VK_NUMPAD7));
  112. keyMap.insert(std::pair<std::string, uint8_t>("numpad8", VK_NUMPAD8));
  113. keyMap.insert(std::pair<std::string, uint8_t>("numpad9", VK_NUMPAD9));
  114. keyMap.insert(std::pair<std::string, uint8_t>("multiply", VK_MULTIPLY));
  115. keyMap.insert(std::pair<std::string, uint8_t>("add", VK_ADD));
  116. keyMap.insert(std::pair<std::string, uint8_t>("subtract", VK_SUBTRACT));
  117. keyMap.insert(std::pair<std::string, uint8_t>("decimal", VK_DECIMAL));
  118. keyMap.insert(std::pair<std::string, uint8_t>("divide", VK_DIVIDE));
  119. keyMap.insert(std::pair<std::string, uint8_t>("numlock", VK_NUMLOCK));
  120. keyMap.insert(std::pair<std::string, uint8_t>("0", 0x30));
  121. keyMap.insert(std::pair<std::string, uint8_t>("1", 0x31));
  122. keyMap.insert(std::pair<std::string, uint8_t>("2", 0x32));
  123. keyMap.insert(std::pair<std::string, uint8_t>("3", 0x33));
  124. keyMap.insert(std::pair<std::string, uint8_t>("4", 0x34));
  125. keyMap.insert(std::pair<std::string, uint8_t>("5", 0x35));
  126. keyMap.insert(std::pair<std::string, uint8_t>("6", 0x36));
  127. keyMap.insert(std::pair<std::string, uint8_t>("7", 0x37));
  128. keyMap.insert(std::pair<std::string, uint8_t>("8", 0x38));
  129. keyMap.insert(std::pair<std::string, uint8_t>("9", 0x39));
  130. keyMap.insert(std::pair<std::string, uint8_t>("scrollock", VK_SCROLL));
  131. keyMap.insert(std::pair<std::string, uint8_t>("lshift", VK_LSHIFT));
  132. keyMap.insert(std::pair<std::string, uint8_t>("rshift", VK_RSHIFT));
  133. keyMap.insert(std::pair<std::string, uint8_t>("lctrl", VK_LCONTROL));
  134. keyMap.insert(std::pair<std::string, uint8_t>("rctrl", VK_RCONTROL));
  135. keyMap.insert(std::pair<std::string, uint8_t>("lalt", VK_LMENU));
  136. keyMap.insert(std::pair<std::string, uint8_t>("ralt", VK_RMENU));
  137. }
  138. return keyMap;
  139. }