How i could know if the key processed by the LowLevelKeyboardProc is from a WM_KEYDOWN or WM_KEYUP message when using the hook procedure WH_KEYBOARD?
wParam is returning the value of vkCode instead of WM_KEYDOWN/UP.
I'm using WH_KEYBOARD because I want to intercept only the keypress into the given thread as WH_KEYBOARD_LL only works globally.
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0 || nCode != HC_ACTION)
return CallNextHookEx(g->KeyboardHook, nCode, wParam, lParam);
KBDLLHOOKSTRUCT* pKeyBoard = (KBDLLHOOKSTRUCT*)lParam;
// How to know if the msg is from a WM_KEYDOWN/KEYUP ?
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int Main()
{
WNDCLASSEX wc{};
HWND hWnd = nullptr;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = 0;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = L"test";
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
hWnd = CreateWindowW(L"test", L"test", WS_OVERLAPPED | WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 828, 492, nullptr, nullptr, nullptr, nullptr);
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
g->KeyboardHook = SetWindowsHookEx(WH_KEYBOARD, LowLevelKeyboardProc, GetModuleHandle(NULL), GetWindowThreadProcessId(g->hWnd, NULL));
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0;
}