WINDOWS HOOKS ARE WEIRD
dev.to·1d·
Discuss: DEV
🧊Iced
Preview
Report Post

The other day I decided to learn keyboard hooks because I was working on a small project to read key inputs. After some brainstorming and losing my mind, I came up with this.

Hook program
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION && wParam == WM_KEYDOWN) {
KBDLLHOOKSTRUCT *kb = (KBDLLHOOKSTRUCT*)lParam;

unsigned char keyboard_state[256];
wchar_t unicodeData[5] = {0};
GetKeyboardState(keyboard_state);

int unicodeKey = ToUnicodeEx(kb->vkCode, kb->scanCode, keyboard_state,
unicodeData, sizeof(unicodeData) / sizeof(wchar_t),
0, NULL);

if (unicodeKey > 0) {
printf("%ls", unicodeData);
}

if (kb->vkCode == VK_RETURN) {
printf("\n");
}
}
return CallNextHookEx(hook, nCode, wParam, lParam);
}

int main() {
hook = SetWindowsHookEx(WH_K...

Similar Posts

Loading similar posts...