2018-05-27 09:56:35 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
2018-05-28 16:27:38 +00:00
|
|
|
static HWND g_hFrameWindow = (HWND)0;
|
2018-07-29 21:34:09 +00:00
|
|
|
static bool g_bHookAltTab = false;
|
|
|
|
static bool g_bHookAltGrControl = false;
|
2018-05-28 16:27:38 +00:00
|
|
|
|
2018-05-27 09:56:35 +00:00
|
|
|
// NB. __stdcall (or WINAPI) and extern "C":
|
|
|
|
// . symbol is decorated as _<symbol>@bytes
|
|
|
|
// . so use the #pragma to create an undecorated alias for our symbol
|
|
|
|
extern "C" __declspec(dllexport) LRESULT CALLBACK LowLevelKeyboardProc(
|
|
|
|
_In_ int nCode,
|
|
|
|
_In_ WPARAM wParam,
|
2018-05-28 16:27:38 +00:00
|
|
|
_In_ LPARAM lParam)
|
2018-05-27 09:56:35 +00:00
|
|
|
{
|
|
|
|
#pragma comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__)
|
|
|
|
|
2018-05-28 16:27:38 +00:00
|
|
|
if (nCode == HC_ACTION)
|
2018-05-27 09:56:35 +00:00
|
|
|
{
|
|
|
|
bool suppress = false;
|
|
|
|
|
|
|
|
PKBDLLHOOKSTRUCT pKbdLlHookStruct = (PKBDLLHOOKSTRUCT) lParam;
|
2018-05-28 16:27:38 +00:00
|
|
|
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
|
|
|
|
|
2018-07-27 20:55:53 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
// NB. Alt Gr (Right-Alt): this normally send 2 WM_KEYDOWN messages for: VK_LCONTROL, then VK_RMENU
|
|
|
|
// Keyboard scanCodes: LCONTROL=0x1D, LCONTROL_from_RMENU=0x21D
|
|
|
|
// . 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
|
2018-07-29 21:34:09 +00:00
|
|
|
if (g_bHookAltGrControl && pKbdLlHookStruct->vkCode == VK_LCONTROL && pKbdLlHookStruct->scanCode >= 0x200) // GH#558
|
2018-07-27 20:55:53 +00:00
|
|
|
{
|
|
|
|
suppress = true;
|
|
|
|
}
|
|
|
|
|
2018-05-28 16:27:38 +00:00
|
|
|
// Suppress alt-tab
|
2018-07-29 21:34:09 +00:00
|
|
|
if (g_bHookAltTab && pKbdLlHookStruct->vkCode == VK_TAB && (pKbdLlHookStruct->flags & LLKHF_ALTDOWN))
|
2018-05-28 16:27:38 +00:00
|
|
|
{
|
2018-06-16 09:24:05 +00:00
|
|
|
PostMessage(g_hFrameWindow, newMsg, VK_TAB, newlParam);
|
2018-05-28 16:27:38 +00:00
|
|
|
suppress = true;
|
|
|
|
}
|
2018-05-27 09:56:35 +00:00
|
|
|
|
2018-05-28 16:27:38 +00:00
|
|
|
// Suppress alt-escape
|
|
|
|
if (pKbdLlHookStruct->vkCode == VK_ESCAPE && (pKbdLlHookStruct->flags & LLKHF_ALTDOWN))
|
|
|
|
{
|
2018-06-16 09:24:05 +00:00
|
|
|
PostMessage(g_hFrameWindow, newMsg, VK_ESCAPE, newlParam);
|
2018-05-27 09:56:35 +00:00
|
|
|
suppress = true;
|
2018-05-28 16:27:38 +00:00
|
|
|
}
|
2018-05-27 09:56:35 +00:00
|
|
|
|
2018-05-28 16:27:38 +00:00
|
|
|
// Suppress alt-space
|
|
|
|
if (pKbdLlHookStruct->vkCode == VK_SPACE && (pKbdLlHookStruct->flags & LLKHF_ALTDOWN))
|
|
|
|
{
|
2018-06-16 09:24:05 +00:00
|
|
|
PostMessage(g_hFrameWindow, newMsg, VK_SPACE, newlParam);
|
2018-05-27 09:56:35 +00:00
|
|
|
suppress = true;
|
2018-05-28 16:27:38 +00:00
|
|
|
}
|
2018-05-27 09:56:35 +00:00
|
|
|
|
2018-05-28 16:27:38 +00:00
|
|
|
// Suppress ctrl-escape
|
2018-07-15 20:00:01 +00:00
|
|
|
if (pKbdLlHookStruct->vkCode == VK_ESCAPE)
|
|
|
|
{
|
2018-07-27 20:55:53 +00:00
|
|
|
// But don't suppress CTRL+SHIFT+ESC
|
|
|
|
if (GetKeyState(VK_CONTROL) < 0 && GetKeyState(VK_SHIFT) >= 0)
|
2018-07-15 20:00:01 +00:00
|
|
|
suppress = true;
|
|
|
|
}
|
2018-05-27 09:56:35 +00:00
|
|
|
|
2018-05-28 16:27:38 +00:00
|
|
|
// Suppress keys by returning 1
|
2018-05-27 09:56:35 +00:00
|
|
|
if (suppress)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallNextHookEx(0/*parameter is ignored*/, nCode, wParam, lParam);
|
|
|
|
}
|
2018-05-28 16:27:38 +00:00
|
|
|
|
2018-07-29 21:34:09 +00:00
|
|
|
extern "C" __declspec(dllexport) void __cdecl RegisterHWND(HWND hWnd, bool bHookAltTab, bool bHookAltGrControl)
|
2018-05-28 16:27:38 +00:00
|
|
|
{
|
|
|
|
g_hFrameWindow = hWnd;
|
2018-07-29 21:34:09 +00:00
|
|
|
g_bHookAltTab = bHookAltTab;
|
|
|
|
g_bHookAltGrControl = bHookAltGrControl;
|
2018-05-28 16:27:38 +00:00
|
|
|
}
|