0

我想在控制台应用程序中捕获键盘消息,所以我尝试了这个:

HWND GetConsoleHwnd(void)
{
    #define SIZEBUF 1024
    char szBuffer[SIZEBUF];

    GetConsoleTitle(szBuffer, SIZEBUF);

    #undef SIZEBUF
    return FindWindow(NULL, szBuffer);
}

LRESULT CALLBACK ConsoleProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch (msg)
    {
        case WM_KEYDOWN:
            switch (wParam)
            {
               //VK Cases
            }
            break;
    }

    return CallWindowProc(OldConsoleProc, hwnd, msg, wParam, lParam);
}

这主要是:

HWND hwndConsole = GetConsoleHwnd();
OldConsoleProc = (WNDPROC) SetWindowLong(hwndConsole, GWL_WNDPROC,
                               ConsoleProc);

这个全局变量:WNDPROC OldConsoleProc;

但它不起作用,我做错了什么?

4

1 回答 1

2

You can't subclass a window of another process this way. You can do it with hooks but I wouldn't recommend trying this on console window. ReadConsoleInput is low-level enough, and it's as far as you can get without ugly nonportable hacks (I'm not even sure there are some events reaching WndProc when the console window is full screen).

于 2013-02-09T12:37:06.120 回答