From 51669f36f3ad216591aaf43c9a8da77dd89b437b Mon Sep 17 00:00:00 2001 From: tomcw Date: Sat, 16 Jun 2018 10:24:05 +0100 Subject: [PATCH] Hook system keys: (#556) . added -no-hook-system-key to prevent hooking system keys . updated help . HookFilter.dll: changed to directly send virtual key code --- HookFilter/HookFilter.cpp | 10 +++------- help/CommandLine.html | 2 ++ source/Applewin.cpp | 37 ++++++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/HookFilter/HookFilter.cpp b/HookFilter/HookFilter.cpp index 0ec04004..fcf3bf33 100644 --- a/HookFilter/HookFilter.cpp +++ b/HookFilter/HookFilter.cpp @@ -20,28 +20,24 @@ extern "C" __declspec(dllexport) LRESULT CALLBACK LowLevelKeyboardProc( UINT newMsg = pKbdLlHookStruct->flags & LLKHF_UP ? WM_KEYUP : WM_KEYDOWN; LPARAM newlParam = newMsg == WM_KEYUP ? 3<<30 : 0; // b31:transition state, b30:previous key state - // Note about PostMessage() and use of VkKeyScan(): - // . Convert the ascii code to virtual key code, so that the message pump can do TranslateMessage() - // . NB. From MSDN for "WM_KEYDOWN" && "WM_KEYUP" : "Applications must pass wParam to TranslateMessage without altering it at all." - // Suppress alt-tab if (pKbdLlHookStruct->vkCode == VK_TAB && (pKbdLlHookStruct->flags & LLKHF_ALTDOWN)) { - PostMessage(g_hFrameWindow, newMsg, LOBYTE(VkKeyScan(0x09)), newlParam); + PostMessage(g_hFrameWindow, newMsg, VK_TAB, newlParam); suppress = true; } // Suppress alt-escape if (pKbdLlHookStruct->vkCode == VK_ESCAPE && (pKbdLlHookStruct->flags & LLKHF_ALTDOWN)) { - PostMessage(g_hFrameWindow, newMsg, LOBYTE(VkKeyScan(0x1B)), newlParam); + PostMessage(g_hFrameWindow, newMsg, VK_ESCAPE, newlParam); suppress = true; } // Suppress alt-space if (pKbdLlHookStruct->vkCode == VK_SPACE && (pKbdLlHookStruct->flags & LLKHF_ALTDOWN)) { - PostMessage(g_hFrameWindow, newMsg, LOBYTE(VkKeyScan(0x20)), newlParam); + PostMessage(g_hFrameWindow, newMsg, VK_SPACE, newlParam); suppress = true; } diff --git a/help/CommandLine.html b/help/CommandLine.html index 0d48ca57..f8c17778 100644 --- a/help/CommandLine.html +++ b/help/CommandLine.html @@ -42,6 +42,8 @@ Enable the dialog box to display the last file saved to

-no-printscreen-key
Prevent the PrintScreen key from being registered

+ -no-hook-system-key
+ Prevent certain system key combinations from being hooked (to prevent the emulator from trapping ALT+ESC, ALT+SPACE, ALT+TAB and CTRL+ESC). This means that the equivalent Open Apple+<key> combinations won't work within the emulator.

-use-real-printer
Enables Advanced configuration control to allow dumping to a real printer

-noreg
diff --git a/source/Applewin.cpp b/source/Applewin.cpp index 188ab1b7..de24d066 100644 --- a/source/Applewin.cpp +++ b/source/Applewin.cpp @@ -78,6 +78,8 @@ TCHAR g_sProgramDir[MAX_PATH] = TEXT(""); // Directory of where AppleWin exe 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; + TCHAR g_sCurrentDir[MAX_PATH] = TEXT(""); // Also Starting Dir. Debugger uses this when load/save bool g_bRestart = false; bool g_bRestartFullScreen = false; @@ -871,7 +873,7 @@ static HINSTANCE g_hinstDLL = 0; static HHOOK g_hhook = 0; // Pre: g_hFrameWindow must be valid -void HookFilterForKeyboard() +bool HookFilterForKeyboard() { g_hinstDLL = LoadLibrary(TEXT("HookFilter.dll")); @@ -890,16 +892,17 @@ void HookFilterForKeyboard() g_hinstDLL, 0); - if (g_hhook == 0 || g_hFrameWindow == 0) - { - std::string msg("Failed to install hook filter for system keys"); + if (g_hhook != 0 && g_hFrameWindow != 0) + return true; - DWORD dwErr = GetLastError(); - MessageBox(GetDesktopWindow(), msg.c_str(), "Warning", MB_ICONASTERISK | MB_OK); + std::string msg("Failed to install hook filter for system keys"); - msg += "\n"; - LogFileOutput(msg.c_str()); - } + DWORD dwErr = GetLastError(); + MessageBox(GetDesktopWindow(), msg.c_str(), "Warning", MB_ICONASTERISK | MB_OK); + + msg += "\n"; + LogFileOutput(msg.c_str()); + return false; } void UnhookFilterForKeyboard() @@ -1227,6 +1230,10 @@ int APIENTRY WinMain(HINSTANCE passinstance, HINSTANCE, LPSTR lpCmdLine, int) { g_bShowPrintScreenWarningDialog = false; } + else if (strcmp(lpCmdLine, "-no-hook-system-key") == 0) // Don't hook the System keys (eg. Left-ALT+ESC/SPACE/TAB) GH#556 + { + g_bHookSystemKey = false; + } else if (strcmp(lpCmdLine, "-spkr-inc") == 0) { lpCmdLine = GetCurrArg(lpNextArg); @@ -1419,7 +1426,11 @@ int APIENTRY WinMain(HINSTANCE passinstance, HINSTANCE, LPSTR lpCmdLine, int) LogFileOutput("Main: RegisterHotKeys()\n"); } - HookFilterForKeyboard(); // needs valid g_hFrameWindow (for message pump) + if (g_bHookSystemKey) + { + if (HookFilterForKeyboard()) // needs valid g_hFrameWindow (for message pump) + LogFileOutput("Main: HookFilterForKeyboard()\n"); + } // Need to test if it's safe to call ResetMachineState(). In the meantime, just call DiskReset(): DiskReset(); // Switch from a booting A][+ to a non-autostart A][, so need to turn off floppy motor @@ -1527,7 +1538,11 @@ int APIENTRY WinMain(HINSTANCE passinstance, HINSTANCE, LPSTR lpCmdLine, int) DSUninit(); LogFileOutput("Main: DSUninit()\n"); - UnhookFilterForKeyboard(); + if (g_bHookSystemKey) + { + UnhookFilterForKeyboard(); + LogFileOutput("Main: UnhookFilterForKeyboard()\n"); + } } while (g_bRestart);