Skip to content

Commit 124e165

Browse files
feat(js): is_key_down
1 parent f6dd39f commit 124e165

File tree

4 files changed

+137
-47
lines changed

4 files changed

+137
-47
lines changed

src/shell/script/binding_qjs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ template<> struct js_bind<mb_shell::js::win32> {
916916
.static_fun<&mb_shell::js::win32::reg_set_dword>("reg_set_dword")
917917
.static_fun<&mb_shell::js::win32::reg_set_string>("reg_set_string")
918918
.static_fun<&mb_shell::js::win32::reg_set_qword>("reg_set_qword")
919+
.static_fun<&mb_shell::js::win32::is_key_down>("is_key_down")
919920
;
920921
}
921922

src/shell/script/binding_types.cc

Lines changed: 126 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,21 +1071,23 @@ int32_t win32::reg_get_dword(std::string key, std::string name) {
10711071
HKEY hKey;
10721072
std::wstring wkey = utf8_to_wstring(key);
10731073
std::wstring wname = utf8_to_wstring(name);
1074-
1075-
if (RegOpenKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
1074+
1075+
if (RegOpenKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, KEY_READ, &hKey) !=
1076+
ERROR_SUCCESS) {
10761077
return 0;
10771078
}
1078-
1079+
10791080
DWORD value = 0;
10801081
DWORD dataSize = sizeof(DWORD);
10811082
DWORD dataType = REG_DWORD;
1082-
1083-
if (RegQueryValueExW(hKey, wname.c_str(), nullptr, &dataType,
1084-
reinterpret_cast<LPBYTE>(&value), &dataSize) != ERROR_SUCCESS) {
1083+
1084+
if (RegQueryValueExW(hKey, wname.c_str(), nullptr, &dataType,
1085+
reinterpret_cast<LPBYTE>(&value),
1086+
&dataSize) != ERROR_SUCCESS) {
10851087
RegCloseKey(hKey);
10861088
return 0;
10871089
}
1088-
1090+
10891091
RegCloseKey(hKey);
10901092
return static_cast<int32_t>(value);
10911093
}
@@ -1094,28 +1096,31 @@ std::string win32::reg_get_string(std::string key, std::string name) {
10941096
HKEY hKey;
10951097
std::wstring wkey = utf8_to_wstring(key);
10961098
std::wstring wname = utf8_to_wstring(name);
1097-
1098-
if (RegOpenKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
1099+
1100+
if (RegOpenKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, KEY_READ, &hKey) !=
1101+
ERROR_SUCCESS) {
10991102
return "";
11001103
}
1101-
1104+
11021105
DWORD dataSize = 0;
11031106
DWORD dataType = REG_SZ;
1104-
1107+
11051108
// Get the size needed
1106-
if (RegQueryValueExW(hKey, wname.c_str(), nullptr, &dataType, nullptr, &dataSize) != ERROR_SUCCESS) {
1109+
if (RegQueryValueExW(hKey, wname.c_str(), nullptr, &dataType, nullptr,
1110+
&dataSize) != ERROR_SUCCESS) {
11071111
RegCloseKey(hKey);
11081112
return "";
11091113
}
1110-
1114+
11111115
std::vector<wchar_t> buffer(dataSize / sizeof(wchar_t) + 1, 0);
1112-
1113-
if (RegQueryValueExW(hKey, wname.c_str(), nullptr, &dataType,
1114-
reinterpret_cast<LPBYTE>(buffer.data()), &dataSize) != ERROR_SUCCESS) {
1116+
1117+
if (RegQueryValueExW(hKey, wname.c_str(), nullptr, &dataType,
1118+
reinterpret_cast<LPBYTE>(buffer.data()),
1119+
&dataSize) != ERROR_SUCCESS) {
11151120
RegCloseKey(hKey);
11161121
return "";
11171122
}
1118-
1123+
11191124
RegCloseKey(hKey);
11201125
return wstring_to_utf8(buffer.data());
11211126
}
@@ -1124,21 +1129,23 @@ int64_t win32::reg_get_qword(std::string key, std::string name) {
11241129
HKEY hKey;
11251130
std::wstring wkey = utf8_to_wstring(key);
11261131
std::wstring wname = utf8_to_wstring(name);
1127-
1128-
if (RegOpenKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS) {
1132+
1133+
if (RegOpenKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, KEY_READ, &hKey) !=
1134+
ERROR_SUCCESS) {
11291135
return 0;
11301136
}
1131-
1137+
11321138
ULONGLONG value = 0;
11331139
DWORD dataSize = sizeof(ULONGLONG);
11341140
DWORD dataType = REG_QWORD;
1135-
1136-
if (RegQueryValueExW(hKey, wname.c_str(), nullptr, &dataType,
1137-
reinterpret_cast<LPBYTE>(&value), &dataSize) != ERROR_SUCCESS) {
1141+
1142+
if (RegQueryValueExW(hKey, wname.c_str(), nullptr, &dataType,
1143+
reinterpret_cast<LPBYTE>(&value),
1144+
&dataSize) != ERROR_SUCCESS) {
11381145
RegCloseKey(hKey);
11391146
return 0;
11401147
}
1141-
1148+
11421149
RegCloseKey(hKey);
11431150
return static_cast<int64_t>(value);
11441151
}
@@ -1147,54 +1154,126 @@ void win32::reg_set_dword(std::string key, std::string name, int32_t value) {
11471154
HKEY hKey;
11481155
std::wstring wkey = utf8_to_wstring(key);
11491156
std::wstring wname = utf8_to_wstring(name);
1150-
1157+
11511158
// Create the key if it doesn't exist
1152-
if (RegCreateKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, nullptr,
1153-
REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &hKey, nullptr) != ERROR_SUCCESS) {
1159+
if (RegCreateKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, nullptr,
1160+
REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &hKey,
1161+
nullptr) != ERROR_SUCCESS) {
11541162
return;
11551163
}
1156-
1164+
11571165
DWORD dwValue = static_cast<DWORD>(value);
1158-
RegSetValueExW(hKey, wname.c_str(), 0, REG_DWORD,
1159-
reinterpret_cast<const BYTE*>(&dwValue), sizeof(DWORD));
1160-
1166+
RegSetValueExW(hKey, wname.c_str(), 0, REG_DWORD,
1167+
reinterpret_cast<const BYTE *>(&dwValue), sizeof(DWORD));
1168+
11611169
RegCloseKey(hKey);
11621170
}
11631171

1164-
void win32::reg_set_string(std::string key, std::string name, std::string value) {
1172+
void win32::reg_set_string(std::string key, std::string name,
1173+
std::string value) {
11651174
HKEY hKey;
11661175
std::wstring wkey = utf8_to_wstring(key);
11671176
std::wstring wname = utf8_to_wstring(name);
11681177
std::wstring wvalue = utf8_to_wstring(value);
1169-
1178+
11701179
// Create the key if it doesn't exist
1171-
if (RegCreateKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, nullptr,
1172-
REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &hKey, nullptr) != ERROR_SUCCESS) {
1180+
if (RegCreateKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, nullptr,
1181+
REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &hKey,
1182+
nullptr) != ERROR_SUCCESS) {
11731183
return;
11741184
}
1175-
1176-
RegSetValueExW(hKey, wname.c_str(), 0, REG_SZ,
1177-
reinterpret_cast<const BYTE*>(wvalue.c_str()),
1178-
static_cast<DWORD>((wvalue.size() + 1) * sizeof(wchar_t)));
1179-
1185+
1186+
RegSetValueExW(hKey, wname.c_str(), 0, REG_SZ,
1187+
reinterpret_cast<const BYTE *>(wvalue.c_str()),
1188+
static_cast<DWORD>((wvalue.size() + 1) * sizeof(wchar_t)));
1189+
11801190
RegCloseKey(hKey);
11811191
}
11821192

11831193
void win32::reg_set_qword(std::string key, std::string name, int64_t value) {
11841194
HKEY hKey;
11851195
std::wstring wkey = utf8_to_wstring(key);
11861196
std::wstring wname = utf8_to_wstring(name);
1187-
1197+
11881198
// Create the key if it doesn't exist
1189-
if (RegCreateKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, nullptr,
1190-
REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &hKey, nullptr) != ERROR_SUCCESS) {
1199+
if (RegCreateKeyExW(HKEY_CURRENT_USER, wkey.c_str(), 0, nullptr,
1200+
REG_OPTION_NON_VOLATILE, KEY_WRITE, nullptr, &hKey,
1201+
nullptr) != ERROR_SUCCESS) {
11911202
return;
11921203
}
1193-
1204+
11941205
ULONGLONG qwValue = static_cast<ULONGLONG>(value);
1195-
RegSetValueExW(hKey, wname.c_str(), 0, REG_QWORD,
1196-
reinterpret_cast<const BYTE*>(&qwValue), sizeof(ULONGLONG));
1197-
1206+
RegSetValueExW(hKey, wname.c_str(), 0, REG_QWORD,
1207+
reinterpret_cast<const BYTE *>(&qwValue), sizeof(ULONGLONG));
1208+
11981209
RegCloseKey(hKey);
11991210
}
1211+
bool win32::is_key_down(std::string key) {
1212+
auto key_lower =
1213+
key | std::views::transform(::tolower) | std::ranges::to<std::string>();
1214+
1215+
constexpr auto key_map =
1216+
std::to_array<std::pair<const char *, int>>({{"ctrl", VK_CONTROL},
1217+
{"shift", VK_SHIFT},
1218+
{"alt", VK_MENU},
1219+
{"space", VK_SPACE},
1220+
{"enter", VK_RETURN},
1221+
{"esc", VK_ESCAPE},
1222+
{"tab", VK_TAB},
1223+
{"backspace", VK_BACK},
1224+
{"delete", VK_DELETE},
1225+
{"left", VK_LEFT},
1226+
{"right", VK_RIGHT},
1227+
{"up", VK_UP},
1228+
{"down", VK_DOWN},
1229+
{"f1", VK_F1},
1230+
{"f2", VK_F2},
1231+
{"f3", VK_F3},
1232+
{"f4", VK_F4},
1233+
{"f5", VK_F5},
1234+
{"f6", VK_F6},
1235+
{"f7", VK_F7},
1236+
{"f8", VK_F8},
1237+
{"f9", VK_F9},
1238+
{"f10", VK_F10},
1239+
{"f11", VK_F11},
1240+
{"f12", VK_F12},
1241+
{"a", 'A'},
1242+
{"b", 'B'},
1243+
{"c", 'C'},
1244+
{"d", 'D'},
1245+
{"e", 'E'},
1246+
{"f", 'F'},
1247+
{"g", 'G'},
1248+
{"h", 'H'},
1249+
{"i", 'I'},
1250+
{"j", 'J'},
1251+
{"k", 'K'},
1252+
{"l", 'L'},
1253+
{"m", 'M'},
1254+
{"n", 'N'},
1255+
{"o", 'O'},
1256+
{"p", 'P'},
1257+
{"q", 'Q'},
1258+
{"r", 'R'},
1259+
{"s", 'S'},
1260+
{"t", 'T'},
1261+
{"u", 'U'},
1262+
{"v", 'V'},
1263+
{"w", 'W'},
1264+
{"x", 'X'},
1265+
{"y", 'Y'},
1266+
{"z", 'Z'}});
1267+
1268+
auto keycode = std::ranges::find_if(
1269+
key_map, [key_lower](const auto &pair) {
1270+
return key_lower == pair.first;
1271+
});
1272+
1273+
if (keycode != key_map.end()) {
1274+
return GetAsyncKeyState(keycode->second) & 0x8000;
1275+
}
1276+
1277+
return false;
1278+
}
12001279
} // namespace mb_shell::js

src/shell/script/binding_types.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1337,6 +1337,14 @@ export class win32 {
13371337
*/
13381338
static reg_set_qword: ((arg1: string, arg2: string, arg3: int64_t) => void)
13391339

1340+
1341+
/**
1342+
*
1343+
* @param key: string
1344+
* @returns boolean
1345+
*/
1346+
static is_key_down: ((arg1: string) => boolean)
1347+
13401348
}
13411349

13421350
export class infra {

src/shell/script/binding_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,8 @@ struct win32 {
638638
static void reg_set_string(std::string key, std::string name,
639639
std::string value);
640640
static void reg_set_qword(std::string key, std::string name, int64_t value);
641+
642+
static bool is_key_down(std::string key);
641643
};
642644

643645
struct infra {

0 commit comments

Comments
 (0)