How to set up a WM_KEYDOWN message interceptor in console application?

469 Views Asked by At

I'm working on a console game engine, and for it I'd like to make some control interface.

For it I chose approach of making a hidden window, which reads keys being pressed, then pushes them into a queue (this thing i'll implement separately) and then the engine itself just reads those keys via reading the queue and doing things described on each button pressed (control table). Here is what I mean:

bool _CreateMessageWnd(HWND* MWND)
{
    WNDCLASSEX wx = {0};
    wx.cbSize = sizeof(WNDCLASSEX);
    wx.lpfnWndProc = HandleMessageSetup;        // function which will handle messages
    wx.hInstance = GetModuleHandle(NULL);
    wx.lpszClassName = L"Dummy";
    if (RegisterClassEx(&wx)) {
        *MWND = CreateWindowExW(0, L"Dummy", L"dummy_name", 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, GetModuleHandle(NULL), NULL);
        return true;
    }
    return false;
}

This function creates a message window and sets HandleMessageSetup() as a proc func.

In a game loop (replicate with while(1)) I call for

void _DispMessage()
{
    MSG msg;
    if(GetMessageW(&msg, 0, 0, 0) > 0) 
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
}

and the message interceptor procedure looks like this:

LRESULT CALLBACK HandleMessageSetup(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
    {
        printf("Got a message! %u\n", uMsg);
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }

but all the messages it receives are 36, 129, 131, 1, which are creation messages of window itself, but no messages from console are present, any key I press, WM_KEYDOWN message does not appear.

from this question I learned about ChangeWindowMessageFilterEx() but neither ChangeWindowMessageFilterEx(MWND, WM_KEYDOWN,1,NULL); nor ChangeWindowMessageFilterEx(GetConsoleWindow(), WM_KEYDOWN,1,NULL); is working and still no messages received. How to overcome this trouble?

1

There are 1 best solutions below

0
Anders On

The console is not your window, it does not even live in your process.

You can read low-level console input with ReadConsoleInput and this might be enough for a text-based game but something faster should probably have its own custom window instead of fighting the console API.