Skip to content

Commit 72c9398

Browse files
authored
chore: optimize the SendKey function (#74)
1 parent 91bef9d commit 72c9398

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/utils.h

+10-10
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,14 @@ void SendKey(T&&... keys) {
370370
switch (key) {
371371
case VK_RBUTTON:
372372
input.type = INPUT_MOUSE;
373-
input.mi.dwFlags = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE
373+
input.mi.dwFlags = GetSystemMetrics(SM_SWAPBUTTON) == TRUE
374374
? MOUSEEVENTF_LEFTDOWN
375375
: MOUSEEVENTF_RIGHTDOWN;
376376
input.mi.dwExtraInfo = MAGIC_CODE;
377377
break;
378378
case VK_LBUTTON:
379379
input.type = INPUT_MOUSE;
380-
input.mi.dwFlags = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE
380+
input.mi.dwFlags = GetSystemMetrics(SM_SWAPBUTTON) == TRUE
381381
? MOUSEEVENTF_RIGHTDOWN
382382
: MOUSEEVENTF_LEFTDOWN;
383383
input.mi.dwExtraInfo = MAGIC_CODE;
@@ -390,26 +390,26 @@ void SendKey(T&&... keys) {
390390
default:
391391
input.type = INPUT_KEYBOARD;
392392
input.ki.wVk = (WORD)key;
393+
input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
393394
input.ki.dwExtraInfo = MAGIC_CODE;
394395
break;
395396
}
396-
input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
397397
inputs.emplace_back(std::move(input));
398398
}
399-
for (auto& key = keys_.begin(); key != keys_.end(); ++key) {
399+
for (auto& key : keys_) {
400400
INPUT input = {0};
401401
// 修正鼠标消息
402-
switch (*key) {
402+
switch (key) {
403403
case VK_RBUTTON:
404404
input.type = INPUT_MOUSE;
405-
input.mi.dwFlags = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE
405+
input.mi.dwFlags = GetSystemMetrics(SM_SWAPBUTTON) == TRUE
406406
? MOUSEEVENTF_LEFTUP
407407
: MOUSEEVENTF_RIGHTUP;
408408
input.mi.dwExtraInfo = MAGIC_CODE;
409409
break;
410410
case VK_LBUTTON:
411411
input.type = INPUT_MOUSE;
412-
input.mi.dwFlags = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE
412+
input.mi.dwFlags = GetSystemMetrics(SM_SWAPBUTTON) == TRUE
413413
? MOUSEEVENTF_RIGHTUP
414414
: MOUSEEVENTF_LEFTUP;
415415
input.mi.dwExtraInfo = MAGIC_CODE;
@@ -422,14 +422,14 @@ void SendKey(T&&... keys) {
422422
default:
423423
input.type = INPUT_KEYBOARD;
424424
input.ki.dwFlags = KEYEVENTF_KEYUP;
425-
input.ki.wVk = (WORD)(*key);
425+
input.ki.wVk = (WORD)key;
426+
input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP;
426427
input.ki.dwExtraInfo = MAGIC_CODE;
427428
break;
428429
}
429-
input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP;
430430
inputs.emplace_back(std::move(input));
431431
}
432-
::SendInput((UINT)inputs.size(), &inputs[0], sizeof(INPUT));
432+
SendInput((UINT)inputs.size(), &inputs[0], sizeof(INPUT));
433433
}
434434

435435
// Send a single key operation.

0 commit comments

Comments
 (0)