Skip to content

Commit

Permalink
fix: modify the key release sequence in SendKey and discard `SendKe…
Browse files Browse the repository at this point in the history
…ys` (#73)
  • Loading branch information
YorkWaugh authored and Bush2021 committed May 9, 2024
1 parent 501b0c8 commit 7949a61
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 50 deletions.
12 changes: 6 additions & 6 deletions src/tabbookmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
89 changes: 45 additions & 44 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,48 +319,48 @@ bool IsFullScreen(HWND hwnd) {

// Keyboard and mouse input functions.
// Send the combined key operation.
class SendKeys {
public:
template <typename... T>
SendKeys(T... keys) {
std::vector<int> 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<INPUT> inputs_;
};
// class SendKeys {
// public:
// template <typename... T>
// SendKeys(T... keys) {
// std::vector<int> 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<INPUT> inputs_;
// };

template <typename... T>
void SendKey(T&&... keys) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 7949a61

Please sign in to comment.