-
Notifications
You must be signed in to change notification settings - Fork 812
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add One-Hand mice wheel scroll diff and merge (#2435) (5). Alt+Wheel …
…Down and other commands now work not only in the editor but also in other windows.
- Loading branch information
Showing
7 changed files
with
213 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
#include <StdAfx.h> | ||
#include "LowLevelMouseHook.h" | ||
|
||
void CALLBACK CLowLevelMouseHook::TimerProc(HWND unnamedParam1, UINT unnamedParam2, UINT_PTR id, DWORD unnamedParam4HWND) | ||
{ | ||
KillTimer(nullptr, id); | ||
EndMenu(); | ||
m_bIgnoreRBUp = false; | ||
} | ||
|
||
LRESULT CALLBACK CLowLevelMouseHook::LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam) | ||
{ | ||
if (nCode < 0) | ||
return CallNextHookEx(m_hMouseHook, nCode, wParam, lParam); | ||
|
||
if (wParam == WM_RBUTTONDOWN) | ||
{ | ||
m_bRButtonDown = true; | ||
} | ||
else if (wParam == WM_RBUTTONUP) | ||
{ | ||
m_bRButtonDown = false; | ||
if (m_bIgnoreRBUp) | ||
{ | ||
LRESULT result = CallNextHookEx(m_hMouseHook, nCode, wParam, lParam); | ||
SetTimer(nullptr, 0, USER_TIMER_MINIMUM, TimerProc); | ||
return result; | ||
} | ||
} | ||
else if (wParam == WM_MOUSEWHEEL) | ||
{ | ||
MSLLHOOKSTRUCT* pMouseStruct = (MSLLHOOKSTRUCT*)lParam; | ||
int zDelta = GET_WHEEL_DELTA_WPARAM(pMouseStruct->mouseData); | ||
int nFlags = pMouseStruct->flags; | ||
|
||
if (GetKeyState(VK_MENU) & 0x8000) | ||
{ | ||
HWND hwndTarget = GetForegroundWindow(); | ||
// When hold Alt key, use nFlags to check MK_CONTROL MK_SHIFT holding got problem, Use GetKeyState() instead. | ||
const auto bShiftDown = GetKeyState(VK_SHIFT) & 0x8000; | ||
const auto bControlDown = GetKeyState(VK_CONTROL) & 0x8000; | ||
// zDelta > 0 scrool up, < 0 scrool down | ||
if (zDelta > 0) | ||
{ | ||
// Check Shift key hold for mice without HWheel function | ||
if (bShiftDown && bControlDown) | ||
{ | ||
// Alt+Ctrl+Shift+ScrollUp as Alt+Ctrl+Left | ||
PostMessage(hwndTarget, WM_COMMAND, ID_R2LNEXT, 0); | ||
return 1; | ||
} | ||
else if (bShiftDown) | ||
{ | ||
// Alt+Shift+ScrollUp as Alt+Left | ||
PostMessage(hwndTarget, WM_COMMAND, ID_R2L, 0); | ||
return 1; | ||
} | ||
else if (nFlags == 0) | ||
{ | ||
// Alt+ScrollUp as Alt+Up | ||
PostMessage(hwndTarget, WM_COMMAND, ID_PREVDIFF, 0); | ||
return 1; | ||
} | ||
} | ||
else if (zDelta < 0) | ||
{ | ||
// Check Shift key hold for mice without HWheel function | ||
if (bShiftDown && bControlDown) | ||
{ | ||
// Alt+Ctrl+Shift+ScrollDown as Alt+Ctrl+Right | ||
PostMessage(hwndTarget, WM_COMMAND, ID_L2RNEXT, 0); | ||
return 1; | ||
} | ||
else if (bShiftDown) | ||
{ | ||
// Alt+Shift+ScrollDown as Alt+Right | ||
PostMessage(hwndTarget, WM_COMMAND, ID_L2R, 0); | ||
return 1; | ||
} | ||
else if (nFlags == 0) | ||
{ | ||
// Alt+ScrollDown as Alt+Down | ||
PostMessage(hwndTarget, WM_COMMAND, ID_NEXTDIFF, 0); | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
// Hold mice right button for One-handed operation | ||
if (m_bRButtonDown) | ||
{ | ||
HWND hwndTarget = GetForegroundWindow(); | ||
if (zDelta > 0) | ||
{ | ||
// RButton+ScrollUp as Alt+Up | ||
m_bIgnoreRBUp = true; | ||
PostMessage(hwndTarget, WM_COMMAND, ID_PREVDIFF, 0); | ||
return 1; | ||
} | ||
else if (zDelta < 0) | ||
{ | ||
// RButton+ScrollDown as Alt+Down | ||
m_bIgnoreRBUp = true; | ||
PostMessage(hwndTarget, WM_COMMAND, ID_NEXTDIFF, 0); | ||
return 1; | ||
} | ||
} | ||
} | ||
else if (wParam == WM_MOUSEHWHEEL) | ||
{ | ||
MSLLHOOKSTRUCT* pMouseStruct = (MSLLHOOKSTRUCT*)lParam; | ||
int zDelta = GET_WHEEL_DELTA_WPARAM(pMouseStruct->mouseData); | ||
int nFlags = pMouseStruct->flags; | ||
|
||
if (GetKeyState(VK_MENU) & 0x8000) | ||
{ | ||
HWND hwndTarget = GetForegroundWindow(); | ||
const auto bControlDown = GetKeyState(VK_CONTROL) & 0x8000; | ||
// zDelta > 0 scrool right, < 0 scrool left | ||
if (zDelta > 0) | ||
{ | ||
if (bControlDown) | ||
{ | ||
// Alt+Ctrl+HScrollRight as Alt+Ctrl+Right | ||
PostMessage(hwndTarget, WM_COMMAND, ID_L2RNEXT, 0); | ||
return 1; | ||
} | ||
else if (nFlags == 0) | ||
{ | ||
// Alt+HScrollRight as Alt+Right | ||
PostMessage(hwndTarget, WM_COMMAND, ID_L2R, 0); | ||
return 1; | ||
} | ||
} | ||
else if (zDelta < 0) | ||
{ | ||
if (bControlDown) | ||
{ | ||
// Alt+Ctrl+HScrollLeft as Alt+Ctrl+Left | ||
PostMessage(hwndTarget, WM_COMMAND, ID_R2LNEXT, 0); | ||
return 1; | ||
} | ||
else if (nFlags == 0) | ||
{ | ||
// Alt+HScrollLeft as Alt+Left | ||
PostMessage(hwndTarget, WM_COMMAND, ID_R2L, 0); | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
// Hold mice right button for One-handed operation | ||
if (m_bRButtonDown) | ||
{ | ||
HWND hwndTarget = GetForegroundWindow(); | ||
if (zDelta > 0) | ||
{ | ||
// RButton+ScrollRight as Alt+Right | ||
m_bIgnoreRBUp = true; | ||
PostMessage(hwndTarget, WM_COMMAND, ID_L2R, 0); | ||
return 1; | ||
} | ||
else if (zDelta < 0) | ||
{ | ||
// RButton+ScrollLeft as Alt+Left | ||
m_bIgnoreRBUp = true; | ||
PostMessage(hwndTarget, WM_COMMAND, ID_R2L, 0); | ||
return 1; | ||
} | ||
} | ||
} | ||
return CallNextHookEx(m_hMouseHook, nCode, wParam, lParam); | ||
} | ||
|
||
void CLowLevelMouseHook::SetMouseHook() | ||
{ | ||
m_hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, nullptr, 0); | ||
} | ||
|
||
void CLowLevelMouseHook::UnhookMouseHook() | ||
{ | ||
if (m_hMouseHook) | ||
{ | ||
UnhookWindowsHookEx(m_hMouseHook); | ||
m_hMouseHook = nullptr; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CLowLevelMouseHook | ||
{ | ||
public: | ||
static void SetMouseHook(); | ||
static void UnhookMouseHook(); | ||
static bool IsRightWheelScrolling() { return m_bIgnoreRBUp; } | ||
private: | ||
static LRESULT CALLBACK LowLevelMouseProc(int nCode, WPARAM wParam, LPARAM lParam); | ||
static void CALLBACK TimerProc(HWND unnamedParam1, UINT unnamedParam2, UINT_PTR id, DWORD unnamedParam4HWND); | ||
inline static HHOOK m_hMouseHook; | ||
inline static bool m_bIgnoreRBUp; | ||
inline static bool m_bRButtonDown; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
5ddf723
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use the Windows file explorer right-click menu to open two WinMerge comparison windows. Then, when the right button of the second window is pressed, the scroll wheel is operated, and a menu will pop up when the right button is released.
The first comparison window opened can normally block this right button release action.
5ddf723
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My suggestion is not to use the WH_MOUSE_LL hook. Using a global hook may cause many side effects. For each MFC window that needs this function, you can handle pMsg->message == WM_MOUSEWHEEL and pMsg->message == WM_MOUSEHWHEEL in advance in PreTranslateMessage(pMsg).
5ddf723
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm currently in an environment without a mouse so I can't test it, but it's not good to have it be a global hook, so I changed it to use WH_MOUSE instead of WH_MOUSE_LL.