如何用LowLevelKeyboardProc在c ++中重新映射键盘键?

我需要重新映射像左边的一些键,但我只是禁用它,所以代码禁用左Alt看起来像这样:

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam; if (p->vkCode == VK_LMENU) return 1; } return CallNextHookEx(hHook, nCode, wParam, lParam); } 

所以我尝试重新左转到左Ctrl Ctrl和使用像“keybd_event”和“SendMessageA”,但没有得到什么。

 LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam; if (p->vkCode == VK_LMENU) { keybd_event(VK_CONTROL, 0, 0, 0); // or use this is sameSendMessageA(0, WM_KEYUP, VK_CONTROL, 0); } } return CallNextHookEx(hHook, nCode, wParam, lParam); } 

如何重新映射左Alt到左Ctrl

Solutions Collecting From Web of "如何用LowLevelKeyboardProc在c ++中重新映射键盘键?"

这是一种重新映射AltCtrl的方式。 不要忘记默认的参数切换回alt如果你不定义wParam可能只是我的电脑好试试看

 include iostream include windows.h using namespace std; HHOOK hHook = 0; LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) { if (nCode == HC_ACTION) { KBDLLHOOKSTRUCT* p = (KBDLLHOOKSTRUCT*) lParam; if (p->vkCode == VK_LMENU) // VK_LMENU = ALT key { switch (wParam){ case WM_SYSKEYDOWN :{ // use SYSKEYDOWN cout << "Key down" << endl; keybd_event(VK_LCONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | 0, 0 ); break; } case WM_KEYUP: // use regular keyup { cout << "Key up" << endl; keybd_event( VK_LCONTROL, 0x1D, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0); return 1; break; } default: wParam = WM_SYSKEYDOWN; // if you do not specify it changes back to alt break; } return 1; } } return CallNextHookEx(hHook, nCode, wParam, lParam); } int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow) { hHook = SetWindowsHookEx(WH_KEYBOARD_LL, &LowLevelKeyboardProc, hThisInstance, NULL); if (hHook == NULL) { cout << "Error" << endl; return 1; } MSG messages; while (GetMessage (&messages, NULL, 0, 0)) { TranslateMessage(&messages); DispatchMessage(&messages); } return messages.wParam; }