diff --git a/src/tabbookmark.h b/src/tabbookmark.h index 729a104..eecae4f 100644 --- a/src/tabbookmark.h +++ b/src/tabbookmark.h @@ -163,7 +163,9 @@ int HandleRightClick(WPARAM wParam, PMOUSEHOOKSTRUCT pmouse) { ExecuteCommand(IDC_NEW_TAB, hwnd); ExecuteCommand(IDC_WINDOW_CLOSE_OTHER_TABS, hwnd); } else { - SendKeys(VK_MBUTTON); + // Attempt new SendKey function which includes a `dwExtraInfo` + // value (MAGIC_CODE). + SendKey(VK_MBUTTON); } return 1; } @@ -263,6 +265,8 @@ LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam) { return CallNextHookEx(mouse_hook, nCode, wParam, lParam); } + // Defining a `dwExtraInfo` value to prevent hook the message sent by + // Chrome++ itself. if (pmouse->dwExtraInfo == MAGIC_CODE) { // DebugLog(L"MAGIC_CODE %x", wParam); goto next; diff --git a/src/utils.h b/src/utils.h index a2bfffe..229e001 100644 --- a/src/utils.h +++ b/src/utils.h @@ -363,6 +363,79 @@ class SendKeys { std::vector inputs_; }; +template +void SendKey(T&&... keys) { + std::vector::type> keys_ = { + std::forward(keys)...}; + std::vector inputs{}; + inputs.reserve(keys_.size() * 2); + for (auto& key : keys_) { + INPUT input = {0}; + // 修正鼠标消息 + switch (key) { + case VK_RBUTTON: + input.type = INPUT_MOUSE; + input.mi.dwFlags = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE + ? MOUSEEVENTF_LEFTDOWN + : MOUSEEVENTF_RIGHTDOWN; + input.mi.dwExtraInfo = MAGIC_CODE; + break; + case VK_LBUTTON: + input.type = INPUT_MOUSE; + input.mi.dwFlags = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE + ? MOUSEEVENTF_RIGHTDOWN + : MOUSEEVENTF_LEFTDOWN; + input.mi.dwExtraInfo = MAGIC_CODE; + break; + case VK_MBUTTON: + input.type = INPUT_MOUSE; + input.mi.dwFlags = MOUSEEVENTF_MIDDLEDOWN; + input.mi.dwExtraInfo = MAGIC_CODE; + break; + default: + input.type = INPUT_KEYBOARD; + input.ki.wVk = (WORD)key; + input.ki.dwExtraInfo = MAGIC_CODE; + break; + } + inputs.emplace_back(std::move(input)); + } + + for (auto& key = keys_.rbegin(); key != keys_.rend(); ++key) { + INPUT input = {0}; + // 修正鼠标消息 + switch (*key) { + case VK_RBUTTON: + input.type = INPUT_MOUSE; + input.mi.dwFlags = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE + ? MOUSEEVENTF_LEFTUP + : MOUSEEVENTF_RIGHTUP; + input.mi.dwExtraInfo = MAGIC_CODE; + break; + case VK_LBUTTON: + input.type = INPUT_MOUSE; + input.mi.dwFlags = ::GetSystemMetrics(SM_SWAPBUTTON) == TRUE + ? MOUSEEVENTF_RIGHTUP + : MOUSEEVENTF_LEFTUP; + input.mi.dwExtraInfo = MAGIC_CODE; + break; + case VK_MBUTTON: + input.type = INPUT_MOUSE; + input.mi.dwFlags = MOUSEEVENTF_MIDDLEUP; + input.mi.dwExtraInfo = MAGIC_CODE; + break; + default: + input.type = INPUT_KEYBOARD; + input.ki.dwFlags = KEYEVENTF_KEYUP; + input.ki.wVk = (WORD)(*key); + input.ki.dwExtraInfo = MAGIC_CODE; + break; + } + inputs.emplace_back(std::move(input)); + } + ::SendInput((UINT)inputs.size(), &inputs[0], sizeof(INPUT)); +} + // Send a single key operation. void SendOneMouse(int mouse) { // Swap the left and right mouse buttons (if defined).