Running a C++ Event Loop WITHOUT using QT

六月ゝ 毕业季﹏ 提交于 2019-12-06 15:26:02

This is a sample that works.

#include <Windows.h>
#include <stdlib.h>
#include <iostream>

HHOOK g_hHook = NULL;
DWORD g_HookThread;

LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    PKBDLLHOOKSTRUCT hookstruct = reinterpret_cast<PKBDLLHOOKSTRUCT>(lParam);
    std::cout << hookstruct->vkCode << std::endl;
    if( wParam == WM_KEYUP ) {
        if( hookstruct->vkCode == VK_ESCAPE ) {
            PostThreadMessage( g_HookThread, WM_QUIT, NULL, NULL );
        }
    }
    return CallNextHookEx( g_hHook, nCode, wParam, lParam );
}

int main()
{
    g_HookThread = GetCurrentThreadId();
    g_hHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, 0, 0);

    MSG msg;
    while( GetMessage(&msg, NULL, NULL, NULL) ) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    UnhookWindowsHookEx(g_hHook);
    return 0;
}

edit: I originally commented that whatpulse switched to directinput, but now they seem to have switched back to lowlevel hooks.

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