From 140d505fe96ff36223cd24afaab53b8cf9a6041d Mon Sep 17 00:00:00 2001 From: tomcw Date: Sun, 29 Jul 2018 22:34:09 +0100 Subject: [PATCH] Keyboard: . reverted default so that ALT+TAB is not hooked (#556) . reverted default so that ALT GR's fake LCONTROL is not hooked (#558) . added new switches: -hook-alt-tab and -hook-altgr-control to support hooking these key combo's (#556) --- HookFilter/HookFilter.cpp | 10 +++++++--- source/Applewin.cpp | 19 +++++++++++++++++-- source/Applewin.h | 1 + source/Keyboard.cpp | 2 +- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/HookFilter/HookFilter.cpp b/HookFilter/HookFilter.cpp index 6a4525d9..fe3a045d 100644 --- a/HookFilter/HookFilter.cpp +++ b/HookFilter/HookFilter.cpp @@ -1,6 +1,8 @@ #include static HWND g_hFrameWindow = (HWND)0; +static bool g_bHookAltTab = false; +static bool g_bHookAltGrControl = false; // NB. __stdcall (or WINAPI) and extern "C": // . symbol is decorated as _@bytes @@ -27,13 +29,13 @@ extern "C" __declspec(dllexport) LRESULT CALLBACK LowLevelKeyboardProc( // . For: Microsoft PS/2/Win7-64, VAIO laptop/Win7-64, Microsoft USB/Win10-64 // NB. WM_KEYDOWN also includes a 9/10-bit? scanCode: LCONTROL=0x1D, RCONTROL=0x11D, RMENU=0x1D(not 0x21D) // . Can't suppress in app, since scanCode is not >= 0x200 - if (pKbdLlHookStruct->vkCode == VK_LCONTROL && pKbdLlHookStruct->scanCode >= 0x200) // GH#558 + if (g_bHookAltGrControl && pKbdLlHookStruct->vkCode == VK_LCONTROL && pKbdLlHookStruct->scanCode >= 0x200) // GH#558 { suppress = true; } // Suppress alt-tab - if (pKbdLlHookStruct->vkCode == VK_TAB && (pKbdLlHookStruct->flags & LLKHF_ALTDOWN)) + if (g_bHookAltTab && pKbdLlHookStruct->vkCode == VK_TAB && (pKbdLlHookStruct->flags & LLKHF_ALTDOWN)) { PostMessage(g_hFrameWindow, newMsg, VK_TAB, newlParam); suppress = true; @@ -69,7 +71,9 @@ extern "C" __declspec(dllexport) LRESULT CALLBACK LowLevelKeyboardProc( return CallNextHookEx(0/*parameter is ignored*/, nCode, wParam, lParam); } -extern "C" __declspec(dllexport) void __cdecl RegisterHWND(HWND hWnd) +extern "C" __declspec(dllexport) void __cdecl RegisterHWND(HWND hWnd, bool bHookAltTab, bool bHookAltGrControl) { g_hFrameWindow = hWnd; + g_bHookAltTab = bHookAltTab; + g_bHookAltGrControl = bHookAltGrControl; } diff --git a/source/Applewin.cpp b/source/Applewin.cpp index 9c64d904..46cfbda8 100644 --- a/source/Applewin.cpp +++ b/source/Applewin.cpp @@ -79,6 +79,8 @@ TCHAR g_sDebugDir [MAX_PATH] = TEXT(""); // TODO: Not currently used TCHAR g_sScreenShotDir[MAX_PATH] = TEXT(""); // TODO: Not currently used bool g_bCapturePrintScreenKey = true; static bool g_bHookSystemKey = true; +static bool g_bHookAltTab = false; +static bool g_bHookAltGrControl = false; TCHAR g_sCurrentDir[MAX_PATH] = TEXT(""); // Also Starting Dir. Debugger uses this when load/save bool g_bRestart = false; @@ -171,6 +173,11 @@ void SetLoadedSaveStateFlag(const bool bFlag) g_bLoadedSaveState = bFlag; } +bool GetHookAltGrControl(void) +{ + return g_bHookAltGrControl; +} + static void ResetToLogoMode(void) { g_nAppMode = MODE_LOGO; @@ -882,10 +889,10 @@ static bool HookFilterForKeyboard() _ASSERT(g_hFrameWindow); - typedef void (*RegisterHWNDProc)(HWND); + typedef void (*RegisterHWNDProc)(HWND, bool, bool); RegisterHWNDProc RegisterHWND = (RegisterHWNDProc) GetProcAddress(g_hinstDLL, "RegisterHWND"); if (RegisterHWND) - RegisterHWND(g_hFrameWindow); + RegisterHWND(g_hFrameWindow, g_bHookAltTab, g_bHookAltGrControl); HOOKPROC hkprcLowLevelKeyboardProc = (HOOKPROC) GetProcAddress(g_hinstDLL, "LowLevelKeyboardProc"); @@ -1294,6 +1301,14 @@ int APIENTRY WinMain(HINSTANCE passinstance, HINSTANCE, LPSTR lpCmdLine, int) { g_bHookSystemKey = false; } + else if (strcmp(lpCmdLine, "-hook-alt-tab") == 0) // GH#556 + { + g_bHookAltTab = true; + } + else if (strcmp(lpCmdLine, "-hook-altgr-control") == 0) // GH#556 + { + g_bHookAltGrControl = true; + } else if (strcmp(lpCmdLine, "-spkr-inc") == 0) { lpCmdLine = GetCurrArg(lpNextArg); diff --git a/source/Applewin.h b/source/Applewin.h index 43a6937b..a3732ecc 100644 --- a/source/Applewin.h +++ b/source/Applewin.h @@ -30,6 +30,7 @@ extern HINSTANCE g_hInstance; extern AppMode_e g_nAppMode; bool GetLoadedSaveStateFlag(void); void SetLoadedSaveStateFlag(const bool bFlag); +bool GetHookAltGrControl(void); extern TCHAR g_sProgramDir[MAX_PATH]; extern TCHAR g_sCurrentDir[MAX_PATH]; diff --git a/source/Keyboard.cpp b/source/Keyboard.cpp index 7ad5af59..c28afd46 100644 --- a/source/Keyboard.cpp +++ b/source/Keyboard.cpp @@ -319,7 +319,7 @@ void KeybQueueKeypress (WPARAM key, Keystroke_e bASCII) { if ( (GetKeyState(VK_SHIFT) >= 0) && !g_bCapsLock ) newKey += 'a' - 'A'; // convert to lowercase key - else if (GetKeyState(VK_CONTROL) < 0) + else if (GetHookAltGrControl() && GetKeyState(VK_CONTROL) < 0) newKey -= 'A' - 1; // convert to control-key }