How to remap keyboard key in c++ with LowLevelKeyboardProc?

两盒软妹~` 提交于 2019-12-05 18:53:23
outlooker

This is a way to remap Alt to Ctrl works great. Don't forget the default parameter switches back to alt if you do not define wParam may just be my computer well give it a try

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!