WH_KEYBOARD_LL not working for regular letters and digits - c++

I'm using SetWindowsHookExA(WH_KEYBOARD_LL, HookCallback, GetModuleHandleA(NULL), 0); to set a global hook for capturing the keystrokes, but the result is strange.
The callback function can be executed when I press the "special" keys such as "Enter", "Tab", "Shift", "Ctrl" and other keys having a Virtual Key Code, while it fails to capture the keystrokes when I press the regular letters and digits.
I am confused about it and could anyone tell me the reason?
#include <Windows.h>
#include <iostream>
using namespace std;
HHOOK keyboardHook = 0;
LRESULT CALLBACK HookCallback(int code, WPARAM wParam, LPARAM lParam)
{
KBDLLHOOKSTRUCT *ks = (KBDLLHOOKSTRUCT*)lParam;
cout<< "[TEST] " << ks->vkCode << endl;
return CallNextHookEx(0, code, wParam, lParam);
}
int main()
{
keyboardHook = SetWindowsHookExA(WH_KEYBOARD_LL, HookCallback, GetModuleHandleA(NULL), 0);
if (keyboardHook == 0)
{
cout << "failed" << endl;
return -1;
}
cout << "ok" << endl;
MSG msg;
while(GetMessage(&msg, NULL, 0, 0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(keyboardHook);
return 0;
}

Related

In mouse hooking, the wheel delta is always 0

#include <iostream>
#include <windows.h>
LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam){
if(wParam == WM_MOUSEWHEEL){
std::cout << "wheel: " << GET_WHEEL_DELTA_WPARAM(wParam) << std::endl;
}else{
MOUSEHOOKSTRUCT* mouselparam = (MOUSEHOOKSTRUCT*)lParam;
std::cout << "etc: " << wParam << " - " << mouselparam->pt.x << "x" << mouselparam->pt.y << std::endl;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int main(int argc, char *argv[]) {
HHOOK hhkLowLevelMouse = SetWindowsHookEx(WH_MOUSE_LL, mouseProc, 0, 0);
MSG msg;
while (!GetMessage(&msg, NULL, NULL, NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(hhkLowLevelMouse);
return 0;
}
This is the full code.
"etc:" works perfectly as I intended.
"wheel:" always outputs 0.
am i missing something?
Using HIWORD instead of GET_WHEEL_DELTA_WPARAM gives the same result.
GET_WHEEL_DELTA_WPARAM() only works with the wParam of a real WM_MOUSEWHEEL window message, not the wParam of a WH_MOUSE_LL hook.
In the hook, the wParam is just the message identifier by itself, nothing more. ALL of the mouse details are stored in a MSLLHOOKSTRUCT struct pointed by the lParam. Which you attempted to look at for all mouse messages other than WM_MOUSEWHEEL, but you are looking at the wrong struct (MOUSEHOOKSTRUCT is used by WH_MOUSE, not WH_MOUSE_LL).
Per the LowLevelMouseProc callback function documentation:
wParam [in]
Type: WPARAM
The identifier of the mouse message. This parameter can be one of the following messages: WM_LBUTTONDOWN, WM_LBUTTONUP, WM_MOUSEMOVE, WM_MOUSEWHEEL, WM_MOUSEHWHEEL, WM_RBUTTONDOWN, or WM_RBUTTONUP.
lParam [in]
Type: LPARAM
A pointer to an MSLLHOOKSTRUCT structure.
And the MSLLHOOKSTRUCT structure documentation:
mouseData
Type: DWORD
If the message is WM_MOUSEWHEEL, the high-order word of this member is the wheel delta. The low-order word is reserved. A positive value indicates that the wheel was rotated forward, away from the user; a negative value indicates that the wheel was rotated backward, toward the user. One wheel click is defined as WHEEL_DELTA, which is 120.
Try this instead:
LRESULT CALLBACK mouseProc(int nCode, WPARAM wParam, LPARAM lParam){
if (nCode == HC_ACTION) {
MSLLHOOKSTRUCT* mouselparam = (MSLLHOOKSTRUCT*)lParam;
if (wParam == WM_MOUSEWHEEL) {
short delta = HIWORD(mouselparam->mouseData);
// alternatively, GET_WHEEL_DELTA_WPARAM() would also work here:
// short delta = GET_WHEEL_DELTA_WPARAM(mouselparam->mouseData);
std::cout << "wheel: " << delta << std::endl;
}else{
std::cout << "etc: " << wParam << " - " << mouselparam->pt.x << "x" << mouselparam->pt.y << std::endl;
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}

WH_KEYBOARD_LL hook not called in UMDF driver, but works in example console app

I'm trying to detect all key presses and mouse events by registering WH_KEYBOARD_LL and WH_MOUSE_LL hooks. Apparently these low-level hooks don't require the hook procedure to reside in a separate DLL.
I have this working in the following example app (a console application).
#include <iostream>
#include <Windows.h>
#include <Winuser.h>
#include <thread>
#include <chrono>
#include <sstream>
#include <atomic>
#include <cassert>
LRESULT CALLBACK wndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
std::cout << std::endl << "EVENT: " << hWnd << ", " << message << ", " << wParam << ", " << lParam << std::endl;
return 0;
}
LRESULT CALLBACK keyboardHook(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
std::cout << "Keyboard: " << nCode << ", " << wParam << ", " << lParam << std::endl;
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
LRESULT CALLBACK mouseHook(
_In_ int nCode,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
std::cout << "Mouse: " << nCode << ", " << wParam << ", " << lParam << std::endl;
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int main()
{
const wchar_t pszClassName[] = L"MyMessageWindow";
auto hInstance = GetModuleHandle(NULL);
WNDCLASSEXW wcl;
ZeroMemory(&wcl, sizeof(WNDCLASSEXW));
wcl.cbSize = sizeof(WNDCLASSEXW);
wcl.hInstance = hInstance;
wcl.lpszClassName = pszClassName;
wcl.lpfnWndProc = wndProc;
assert(RegisterClassExW(&wcl) != 0);
auto hwnd = CreateWindowW(
pszClassName,
pszClassName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
HWND_MESSAGE,
NULL,
NULL,
NULL
);
auto threadId = 0;
std::thread t([hwnd, threadId]() {
auto keyboardHookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, keyboardHook, NULL, threadId);
auto mouseHookHandle = SetWindowsHookEx(WH_MOUSE_LL, mouseHook, NULL, threadId);
while (true) {
MSG msg;
GetMessage(&msg, hwnd, 0, 0);
}
});
t.join();
return 0;
}
So this works, and I can see the keyboard and mouse events printed to the console regardless of what apps are in focus (if any), which is great.
However, when I do this inside a UMDF driver, the messages don't come through. The calls to SetWindowsHookEx appear to succeed without error.
I'm thinking of creating a minimal UMDF driver to test if it's possible, but thought I'd first ask here in case someone can tell me whether it's possible. If not, I have a few other approaches in mind.
Thanks

the WM_CLOSE event is never sent/received?

I'm learning DX12 and, in the process, learning "good old Win32".
i have trouble exiting the main loop and it seems related to the fact that i'm not receiving the WM_CLOSE message.
In C++, Windows 10, Console application.
#include <iostream>
#include <d3d12.h>
#include <dxgi1_4.h>
#include <tchar.h>
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
return ::DefWindowProc(hWnd, msg, wParam, lParam);
}
int main()
{
std::cout << "Hello World!\n";
WNDCLASSEX wc = {
sizeof(WNDCLASSEX),
CS_CLASSDC,
WndProc,
0L, 0L,
GetModuleHandle(NULL),
NULL, NULL, NULL, NULL,
_T("ker engine"),
NULL
};
std::cout << "Registering Class\n";
::RegisterClassEx(&wc);
std::cout << "Creating Window\n";
HWND hwnd = ::CreateWindow(
wc.lpszClassName,
_T("Ker Engine DX12"),
WS_OVERLAPPEDWINDOW,
100, 100, 1280, 800, NULL, NULL,
wc.hInstance, NULL
);
std::cout << "Show Window\n";
::ShowWindow(hwnd, SW_SHOWDEFAULT);
std::cout << "Update Window\n";
::UpdateWindow(hwnd);
std::cout << "Entering main loop\n";
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != (WM_QUIT))
{
if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
std::cout << msg.message << std::endl;
switch (msg.message)
{
case WM_CLOSE:
std::cout << "close received\n";
::PostQuitMessage(0);
break;
}
continue;
}
}
std::cout << "leaving main loop\n";
std::cout << "Destroy Window\n";
::DestroyWindow(hwnd);
std::cout << "Unregister Class\n";
::UnregisterClass(wc.lpszClassName, wc.hInstance);
std::cout << "Bye\n";
return 0;
}
When i press the X (close) red window button, the window is closed but :
"closed received" isn't printed
"leaving main loop" isn't printed.
The output is :
Entering main loop
[a lot of message code, in decimal]
160 (a lot of it) (WM_NCMOUSEMOVE)
161 (WM_NCLBUTTONDOWN)
275 (WM_TIMER)
no more output printed, i have to close the console manually.
no WM_CLOSE,or WM_DESTROY, or WM_QUIT. Between BUTTONDOW and whatever TIMER is supposed to be, there should be an event related to the fact that the windows was closed, doesn't it ?
I'm a beginner at this. I tried to search google and stackoverflow but i didn't understand if the context applied to me, or it was too specific/unrelated. it's probably a duplicate but i can't find it.
Am i losing/skiping message perhaps ? that's all i can think of.
Thanks to Simon Mourier comment and link to a tutorial, the problem was solved.
The message handling had to be done in WndProc, not in the "main loop".
I'm reposting the modified, cleaned, working, code :
#include <iostream>
#include <d3d12.h>
#include <dxgi1_4.h>
#include <tchar.h>
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
std::cout << "close received\n";
::PostQuitMessage(0);
return 0;
}
return ::DefWindowProc(hWnd, msg, wParam, lParam);
}
int main()
{
WNDCLASSEX wc = {
sizeof(WNDCLASSEX),
CS_CLASSDC,
WndProc,
0L, 0L,
GetModuleHandle(NULL),
NULL, NULL, NULL, NULL,
_T("ker engine"),
NULL
};
::RegisterClassEx(&wc);
HWND hwnd = ::CreateWindow(
wc.lpszClassName,
_T("Ker Engine DX12"),
WS_OVERLAPPEDWINDOW,
100, 100, 1280, 800, NULL, NULL,
wc.hInstance, NULL
);
::ShowWindow(hwnd, SW_SHOWDEFAULT);
::UpdateWindow(hwnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != (WM_QUIT))
{
if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
}
::DestroyWindow(hwnd);
::UnregisterClass(wc.lpszClassName, wc.hInstance);
return 0;
}

Cannot receive WM_CLIPBOARDUPDATE messages

I am trying to use Clipboard Format Listener for my C++ Console Application. The goal is to monitor every change in the clipboard. I create MessageOnly window, successfully call AddClipboardFormatListener in WM_CREATE, but never get WM_CLIPBOARDUPDATE message in WindowProc function.
#include <iostream>
#include "windows.h"
using namespace std;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
if (AddClipboardFormatListener(hwnd))
cout << " Listener started" << endl;
else
cout << " Start listener failed" << endl;
break;
case WM_DESTROY:
if (RemoveClipboardFormatListener(hwnd))
cout << " Listener stopped" << endl;
else
cout << " Stop listener failed" << endl;
break;
case WM_CLIPBOARDUPDATE:
// Clipboard content has changed
cout << " Clipboard updated" << endl;
break;
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
int main(int argc, char* argv[])
{
HWND hWindow = nullptr;
static const wchar_t* className = L"ClipboardListener";
WNDCLASSEX wx = {};
wx.cbSize = sizeof(WNDCLASSEX);
wx.lpfnWndProc = WindowProc;
wx.hInstance = GetModuleHandle(NULL);
wx.lpszClassName = className;
if (!RegisterClassEx(&wx)) {
cout << "Cannot register class" << endl;
}
else
{
hWindow = CreateWindowEx(
0,
className,
L"ClipboardListener",
0, 0, 0, 0, 0,
HWND_MESSAGE,
NULL, NULL, NULL);
}
if (!hWindow)
{
cout << "Cannot create window" << endl;
}
else
{
while (true)
{
// Peek for a WM_CLIPBOARDUPDATE message
MSG message = { 0 };
PeekMessage(&message, hWindow, WM_CLIPBOARDUPDATE, WM_CLIPBOARDUPDATE, PM_REMOVE);
if (message.message == WM_CLIPBOARDUPDATE)
{
cout << "Sample window received WM_CLIPBOARDUPDATE message" << endl;
}
}
}
cin.get();
DestroyWindow(hWindow);
return 0;
}
PeekMessage works well, but I don't want to use loop to receive messages.
If I delete PeekMessage or replace PM_REMOVE with PM_NOREMOVE, nothing changes.
Your message loop is wrong.
CreateWindowEx() sends a WM_CREATE message before exiting, which is why your WindowProc() receives that message.
However, PeekMessage() does not dispatch messages to windows, which is why your WindowProc() does not receive the WM_CLIPBOARDUPDATE messages. Your message loop needs to call DispatchMessage() for that. You should also be using GetMessage() instead of PeekMessage(), so that the loop makes the calling thread sleep when there are no messages to process.
A standard message loop looks more like this:
MSG message;
while (GetMessage(&message, NULL, 0, 0))
{
TranslateMessage(&message);
DispatchMessage(&message);
}

low level keyboard hook repeat keystrokes (unwanted)

my laptop space bar is broken and so I went on a venture to write a low level keyboard hook to disable it (As it insisted it was being pressed all the time) and change my full stop/period key into a new space bar.. that works fine but I have 2 issues.
1) The new space bar keystroke is sent twice, always - I don't know why
2) I'm trying to rewrite this hook to read alternative input and when I output that input to verify, it's doubled up in the output.
I'm not a strong c++ programmer nor am I a master if the Windows API so would love for a spot of guidance from you guys, if I may!
Code follows:-
#include<Windows.h>
#include <stdio.h>
#include <iostream>
using namespace std;
HHOOK hHook = NULL;
INPUT space[2];
bool sendingSpace=false;
void sendSpace()
{
cout << "Sending space\n";
space[0].type=INPUT_KEYBOARD;
space[0].ki.wVk=VK_SPACE;
space[0].ki.time=0;
space[1].type=INPUT_KEYBOARD;
space[1].ki.wVk=VK_SPACE;
space[1].ki.time=0;
space[1].ki.dwFlags=KEYEVENTF_KEYUP;
SendInput(2,space,sizeof(INPUT));
}
LRESULT CALLBACK MyLowLevelHook ( int nCode , WPARAM wParam , LPARAM lParam)
{
KBDLLHOOKSTRUCT* hs = (KBDLLHOOKSTRUCT*)lParam;
if(nCode <0)
return CallNextHookEx(hHook , nCode ,wParam , lParam);
switch(hs->vkCode)
{
case VK_SPACE:
if(!sendingSpace)
{
cout << "Ignoring space bar\n";
return 1;
}
else
{
return CallNextHookEx(hHook , nCode ,wParam , lParam);
}
break;
case VK_OEM_PERIOD:
sendingSpace=true;
sendSpace();
sendingSpace=false;
return 1;
break;
default:
cout << hs->vkCode << " ( " << (char)(hs->vkCode) << ")" << endl;
break;
}
return CallNextHookEx(hHook , nCode ,wParam , lParam);
}
int main()
{
//FreeConsole();
MSG msg;
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelHook , NULL,NULL);
while(!PeekMessage(&msg, NULL, 0, 0,PM_REMOVE)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(hHook);
return 0;
}
P.S Im only using PeekMessage as a trial and error attempt to solve my issue!
Thanks in advance for any guidance, help or fingers pointing in the right direction!
Timmy.
The keyboard hook sees an event for a key being pressed and a key being released. The wParam argument of the hook callback contains WM_KEYDOWN on a key down, and WM_KEYUP on a key up.
In this case, you're not separating them both out, and are sending a space character every time the key is pressed and then released.
What you should really do is SendInput a KEYEVENTF_KEYDOWN when you see a WM_KEYDOWN and to a SendInput of a KEYEVENTF_KEYUP on seeing a WM_KEYUP.
The easiest way to accomplish this is to add a parameter to the sendSpace() function, so that it looks like:
void sendSpace(WPARAM param)
{
INPUT space;
cout << "Sending space " << (param == WM_KEYDOWN ? "Down" : "Up") << endl;
space.type = INPUT_KEYBOARD;
space.ki.wVk = VK_SPACE;
space.ki.time = 0;
space.ki.dwFlags = (param == WM_KEYDOWN) ? KEYEVENTF_KEYDOWN : KEYEVENTF_KEYUP;
SendInput(1, &space, sizeof INPUT);
}