I'm trying to use the Windows API in C ++ and SetWindowsHookEx
WH_KEYBOARD_LL
does not seem to receive events from the right Shift key (the Shift key on the right side of the qwerty keyboard is lower than Enter ). It works with the left Shift key . How to fix this problem?
#include "stdafx.h" #include <cstdlib> #include <fstream> #include <iostream> #include <string> #include <windows.h> #include <string> #include <shlobj.h> #include <Shlwapi.h> #include <stdio.h> #include <aclapi.h> #include <tchar.h> #include <iostream> #include <fstream> #include <future> #include <stdlib.h> #include <random> #include <ctime> #include <time.h> #include <Lmcons.h> HHOOK kbdhook; /* Keyboard hook handle */ bool running; /* Used in main loop */ __declspec(dllexport) LRESULT CALLBACK handlekeys(int code, WPARAM wp, LPARAM lp) { static bool capslock = false; static bool shift = false; char tmp[0xFF] = {0}; std::string str; DWORD msg = 1; KBDLLHOOKSTRUCT st_hook = *((KBDLLHOOKSTRUCT*)lp); msg += (st_hook.scanCode << 16); msg += ((st_hook.flags & LLKHF_EXTENDED) << 24); GetKeyNameText(msg, tmp, 0xFF); str = std::string(tmp); if (code == HC_ACTION && (wp == WM_SYSKEYDOWN || wp == WM_KEYDOWN )) { MessageBox(NULL,str.c_str(),NULL,MB_OK); } return CallNextHookEx(kbdhook, code, wp, lp); } LRESULT CALLBACK windowprocedure(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { switch (msg) { case WM_CLOSE: case WM_DESTROY: running = false; break; default: /* Call default message handler */ return DefWindowProc(hwnd, msg, wp, lp); } return 0; } int WINAPI WinMain(HINSTANCE thisinstance, HINSTANCE previnstance, LPSTR cmdline, int ncmdshow) { HWND hwnd; HWND fgwindow = GetForegroundWindow(); MSG msg; WNDCLASSEX windowclass; HINSTANCE modulehandle; modulehandle = GetModuleHandle(NULL); kbdhook = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)handlekeys, modulehandle, NULL); running = true; while (running) { if (!GetMessage(&msg, NULL, 0, 0)) running = false; TranslateMessage(&msg); DispatchMessage(&msg); } return 0; }
To the right, shifts the warning line in the form. However, a left shift shows the "SHIFT" line in the warning. Someone tell me ???
PS:
If I delete the line using msg + = ((st_hook.flags and LLKHF_EXTENDED) <24); → "RIGHT SHIFT" appears, but undefined appears when you press the "Windows" key
source share