|
- #include "LuaUtility.h"
- #include <windows.h>
- #include <WinUser.h>
- #include <string>
-
- static std::unordered_map<std::string, uint8_t> keyMap;
-
- LuaUtility::LuaUtility()
- {
- keyMap = GetKeyMap();
- }
-
- bool LuaUtility::IsKeyPressed(std::string keyName)
- {
- if (keyName.length() > 1) {
- std::transform(keyName.begin(), keyName.end(), keyName.begin(), [](unsigned char c) {return std::tolower(c); });
- auto keycode = GetKeyMap()[keyName];
- return (GetAsyncKeyState(GetKeyMap()[keyName]) & 0x8000) != 0;
- }
- else {
- std::transform(keyName.begin(), keyName.end(), keyName.begin(), [](unsigned char c) {return std::toupper(c); });
- return (GetAsyncKeyState(keyName.c_str()[0]) & 0x8000) != 0;
- }
- return false;
- }
-
- bool LuaUtility::IsKeyPressed(std::string keyName, std::string keyName2)
- {
- std::vector<uint8_t> keys;
- std::string current = keyName;
- for (int i = 0; i < 2; i++) {
- if (current.length() > 1) {
- std::transform(current.begin(), current.end(), current.begin(), [](unsigned char c) {return std::tolower(c); });
- keys.push_back(GetKeyMap()[current]);
- }
- else {
- std::transform(current.begin(), current.end(), current.begin(), [](unsigned char c) {return std::toupper(c); });
- keys.push_back(current.c_str()[0]);
- }
- current = keyName2;
- }
- return ((GetAsyncKeyState(keys[0]) & 0x8000) && GetAsyncKeyState(keys[1]) & 0x8000) != 0;
- }
-
- bool LuaUtility::IsKeyPressed(std::string keyName, std::string keyName2, std::string keyName3)
- {
- std::vector<uint8_t> keys;
- std::string current = keyName;
- for (int i = 0; i < 3; i++) {
- if (current.length() > 1) {
- std::transform(current.begin(), current.end(), current.begin(), [](unsigned char c) {return std::tolower(c); });
- keys.push_back(GetKeyMap()[current]);
- }
- else {
- std::transform(current.begin(), current.end(), current.begin(), [](unsigned char c) {return std::toupper(c); });
- keys.push_back(current.c_str()[0]);
- }
- if (i == 0)
- current = keyName2;
- else
- current = keyName3;
- }
- return ((GetAsyncKeyState(keys[0]) & 0x8000) && GetAsyncKeyState(keys[1] & 0x8000) && GetAsyncKeyState(keys[2]) & 0x8000) != 0;
- }
-
- bool LuaUtility::ContainsKey(std::string keyName) {
- if (keyMap.size() == 0)
- keyMap = GetKeyMap();
- std::transform(keyName.begin(), keyName.end(), keyName.begin(), [](unsigned char c) {return std::tolower(c); });
- return keyMap.find(keyName) != keyMap.end();
- }
-
- std::unordered_map<std::string, uint8_t> LuaUtility::GetKeyMap()
- {
- if (!keyMap.size()) {
- keyMap.insert(std::pair<std::string, uint8_t>("backspace", VK_BACK));
- keyMap.insert(std::pair<std::string, uint8_t>("tab", VK_TAB));
- keyMap.insert(std::pair<std::string, uint8_t>("clear", VK_CLEAR));
- keyMap.insert(std::pair<std::string, uint8_t>("return", VK_RETURN));
- keyMap.insert(std::pair<std::string, uint8_t>("enter", VK_RETURN));
- keyMap.insert(std::pair<std::string, uint8_t>("shift", VK_SHIFT));
- keyMap.insert(std::pair<std::string, uint8_t>("ctrl", VK_CONTROL));
- keyMap.insert(std::pair<std::string, uint8_t>("alt", VK_MENU));
- keyMap.insert(std::pair<std::string, uint8_t>("pause", VK_PAUSE));
- keyMap.insert(std::pair<std::string, uint8_t>("capslock", VK_CAPITAL));
- keyMap.insert(std::pair<std::string, uint8_t>("escape", VK_ESCAPE));
- keyMap.insert(std::pair<std::string, uint8_t>("space", VK_SPACE));
- keyMap.insert(std::pair<std::string, uint8_t>("pageup", VK_PRIOR));
- keyMap.insert(std::pair<std::string, uint8_t>("pagedown", VK_NEXT));
- keyMap.insert(std::pair<std::string, uint8_t>("end", VK_END));
- keyMap.insert(std::pair<std::string, uint8_t>("home", VK_HOME));
- keyMap.insert(std::pair<std::string, uint8_t>("left", VK_LEFT));
- keyMap.insert(std::pair<std::string, uint8_t>("up", VK_UP));
- keyMap.insert(std::pair<std::string, uint8_t>("right", VK_RIGHT));
- keyMap.insert(std::pair<std::string, uint8_t>("down", VK_DOWN));
- keyMap.insert(std::pair<std::string, uint8_t>("printscr", VK_SNAPSHOT));
- keyMap.insert(std::pair<std::string, uint8_t>("insert", VK_INSERT));
- keyMap.insert(std::pair<std::string, uint8_t>("delete", VK_DELETE));
-
- keyMap.insert(std::pair<std::string, uint8_t>("f1", VK_F1));
- keyMap.insert(std::pair<std::string, uint8_t>("f2", VK_F2));
- keyMap.insert(std::pair<std::string, uint8_t>("f3", VK_F3));
- keyMap.insert(std::pair<std::string, uint8_t>("f4", VK_F4));
- keyMap.insert(std::pair<std::string, uint8_t>("f5", VK_F5));
- keyMap.insert(std::pair<std::string, uint8_t>("f6", VK_F6));
- keyMap.insert(std::pair<std::string, uint8_t>("f7", VK_F7));
- keyMap.insert(std::pair<std::string, uint8_t>("f8", VK_F8));
- keyMap.insert(std::pair<std::string, uint8_t>("f9", VK_F9));
- keyMap.insert(std::pair<std::string, uint8_t>("f10", VK_F10));
- keyMap.insert(std::pair<std::string, uint8_t>("f11", VK_F11));
- keyMap.insert(std::pair<std::string, uint8_t>("f12", VK_F12));
-
- keyMap.insert(std::pair<std::string, uint8_t>("numpad0", VK_NUMPAD0));
- keyMap.insert(std::pair<std::string, uint8_t>("numpad1", VK_NUMPAD1));
- keyMap.insert(std::pair<std::string, uint8_t>("numpad2", VK_NUMPAD2));
- keyMap.insert(std::pair<std::string, uint8_t>("numpad3", VK_NUMPAD3));
- keyMap.insert(std::pair<std::string, uint8_t>("numpad4", VK_NUMPAD4));
- keyMap.insert(std::pair<std::string, uint8_t>("numpad5", VK_NUMPAD5));
- keyMap.insert(std::pair<std::string, uint8_t>("numpad6", VK_NUMPAD6));
- keyMap.insert(std::pair<std::string, uint8_t>("numpad7", VK_NUMPAD7));
- keyMap.insert(std::pair<std::string, uint8_t>("numpad8", VK_NUMPAD8));
- keyMap.insert(std::pair<std::string, uint8_t>("numpad9", VK_NUMPAD9));
-
- keyMap.insert(std::pair<std::string, uint8_t>("multiply", VK_MULTIPLY));
- keyMap.insert(std::pair<std::string, uint8_t>("add", VK_ADD));
- keyMap.insert(std::pair<std::string, uint8_t>("subtract", VK_SUBTRACT));
- keyMap.insert(std::pair<std::string, uint8_t>("decimal", VK_DECIMAL));
- keyMap.insert(std::pair<std::string, uint8_t>("divide", VK_DIVIDE));
- keyMap.insert(std::pair<std::string, uint8_t>("numlock", VK_NUMLOCK));
-
- keyMap.insert(std::pair<std::string, uint8_t>("0", 0x30));
- keyMap.insert(std::pair<std::string, uint8_t>("1", 0x31));
- keyMap.insert(std::pair<std::string, uint8_t>("2", 0x32));
- keyMap.insert(std::pair<std::string, uint8_t>("3", 0x33));
- keyMap.insert(std::pair<std::string, uint8_t>("4", 0x34));
- keyMap.insert(std::pair<std::string, uint8_t>("5", 0x35));
- keyMap.insert(std::pair<std::string, uint8_t>("6", 0x36));
- keyMap.insert(std::pair<std::string, uint8_t>("7", 0x37));
- keyMap.insert(std::pair<std::string, uint8_t>("8", 0x38));
- keyMap.insert(std::pair<std::string, uint8_t>("9", 0x39));
-
- keyMap.insert(std::pair<std::string, uint8_t>("scrollock", VK_SCROLL));
- keyMap.insert(std::pair<std::string, uint8_t>("lshift", VK_LSHIFT));
- keyMap.insert(std::pair<std::string, uint8_t>("rshift", VK_RSHIFT));
- keyMap.insert(std::pair<std::string, uint8_t>("lctrl", VK_LCONTROL));
- keyMap.insert(std::pair<std::string, uint8_t>("rctrl", VK_RCONTROL));
- keyMap.insert(std::pair<std::string, uint8_t>("lalt", VK_LMENU));
- keyMap.insert(std::pair<std::string, uint8_t>("ralt", VK_RMENU));
- }
- return keyMap;
- }
|