From 7949a6123bd2666a4cd02fb1f0eb22aa4c80ee51 Mon Sep 17 00:00:00 2001 From: York Waugh <67750590+YorkWaugh@users.noreply.github.com> Date: Thu, 9 May 2024 23:30:19 +0800 Subject: [PATCH] fix: modify the key release sequence in `SendKey` and discard `SendKeys` (#73) --- src/tabbookmark.h | 12 +++---- src/utils.h | 89 ++++++++++++++++++++++++----------------------- 2 files changed, 51 insertions(+), 50 deletions(-) diff --git a/src/tabbookmark.h b/src/tabbookmark.h index 9daab1c..4442e38 100644 --- a/src/tabbookmark.h +++ b/src/tabbookmark.h @@ -200,9 +200,9 @@ bool HandleBookmark(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) { if (top_container_view && is_on_bookmark && !is_on_new_tab) { if (config.is_bookmark_new_tab == "foreground") { - SendKeys(VK_MBUTTON, VK_SHIFT); + SendKey(VK_MBUTTON, VK_SHIFT); } else if (config.is_bookmark_new_tab == "background") { - SendKeys(VK_MBUTTON); + SendKey(VK_MBUTTON); } return true; } @@ -229,9 +229,9 @@ bool HandleBookmarkMenu(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) { !is_on_new_tab) { if (config.is_bookmark_new_tab == "foreground") { DebugLog(L"MButton + Shift"); - SendKeys(VK_MBUTTON, VK_SHIFT); + SendKey(VK_MBUTTON, VK_SHIFT); } else if (config.is_bookmark_new_tab == "background") { - SendKeys(VK_MBUTTON); + SendKey(VK_MBUTTON); } return true; } @@ -352,9 +352,9 @@ int HandleOpenUrlNewTab(WPARAM wParam) { NodePtr top_container_view = GetTopContainerView(GetForegroundWindow()); if (IsOmniboxFocus(top_container_view) && !IsOnNewTab(top_container_view)) { if (config.is_open_url_new_tab == "foreground") { - SendKeys(VK_MENU, VK_RETURN); + SendKey(VK_MENU, VK_RETURN); } else if (config.is_open_url_new_tab == "background") { - SendKeys(VK_SHIFT, VK_MENU, VK_RETURN); + SendKey(VK_SHIFT, VK_MENU, VK_RETURN); } return 1; } diff --git a/src/utils.h b/src/utils.h index b933959..3de50cf 100644 --- a/src/utils.h +++ b/src/utils.h @@ -319,48 +319,48 @@ bool IsFullScreen(HWND hwnd) { // Keyboard and mouse input functions. // Send the combined key operation. -class SendKeys { - public: - template - SendKeys(T... keys) { - std::vector keys_ = {keys...}; - for (auto& key : keys_) { - INPUT input = {0}; - input.type = INPUT_KEYBOARD; - input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY; - input.ki.wVk = key; - - // Correct the mouse message - switch (key) { - case VK_MBUTTON: - input.type = INPUT_MOUSE; - input.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN; - break; - } - - inputs_.push_back(input); - } - - SendInput((UINT)inputs_.size(), &inputs_[0], sizeof(INPUT)); - } - ~SendKeys() { - for (auto& input : inputs_) { - input.ki.dwFlags |= KEYEVENTF_KEYUP; - - // Correct the mouse message - switch (input.ki.wVk) { - case VK_MBUTTON: - input.mi.dwFlags = MOUSEEVENTF_MIDDLEUP; - break; - } - } - - SendInput((UINT)inputs_.size(), &inputs_[0], sizeof(INPUT)); - } - - private: - std::vector inputs_; -}; +// class SendKeys { +// public: +// template +// SendKeys(T... keys) { +// std::vector keys_ = {keys...}; +// for (auto& key : keys_) { +// INPUT input = {0}; +// input.type = INPUT_KEYBOARD; +// input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY; +// input.ki.wVk = key; + +// // Correct the mouse message +// switch (key) { +// case VK_MBUTTON: +// input.type = INPUT_MOUSE; +// input.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN; +// break; +// } + +// inputs_.push_back(input); +// } + +// SendInput((UINT)inputs_.size(), &inputs_[0], sizeof(INPUT)); +// } +// ~SendKeys() { +// for (auto& input : inputs_) { +// input.ki.dwFlags |= KEYEVENTF_KEYUP; + +// // Correct the mouse message +// switch (input.ki.wVk) { +// case VK_MBUTTON: +// input.mi.dwFlags = MOUSEEVENTF_MIDDLEUP; +// break; +// } +// } + +// SendInput((UINT)inputs_.size(), &inputs_[0], sizeof(INPUT)); +// } + +// private: +// std::vector inputs_; +// }; template void SendKey(T&&... keys) { @@ -397,10 +397,10 @@ void SendKey(T&&... keys) { input.ki.dwExtraInfo = MAGIC_CODE; break; } + input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY; inputs.emplace_back(std::move(input)); } - - for (auto& key = keys_.rbegin(); key != keys_.rend(); ++key) { + for (auto& key = keys_.begin(); key != keys_.end(); ++key) { INPUT input = {0}; // 修正鼠标消息 switch (*key) { @@ -430,6 +430,7 @@ void SendKey(T&&... keys) { input.ki.dwExtraInfo = MAGIC_CODE; break; } + input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP; inputs.emplace_back(std::move(input)); } ::SendInput((UINT)inputs.size(), &inputs[0], sizeof(INPUT));