MSDN Documentation

TranslateMessage function

Header: Winuser.h

Library: User32.lib

DLL: User32.dll

Synopsis

BOOL TranslateMessage(
    const MSG *lpMsg
);

Parameters

lpMsgPointer to an MSG structure that contains message information retrieved from the calling thread's message queue.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Note that TranslateMessage always returns TRUE if the message is not a keyboard message.

Remarks

Example

#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
        case WM_CHAR:
            // Echo the typed character
            {
                wchar_t c = (wchar_t)wParam;
                OutputDebugStringW(&c);
            }
            return 0;
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, PWSTR, int)
{
    const wchar_t CLASS_NAME[] = L"SampleWindow";

    WNDCLASS wc = {0};
    wc.lpfnWndProc   = WndProc;
    wc.hInstance     = hInst;
    wc.lpszClassName = CLASS_NAME;
    RegisterClass(&wc);

    HWND hwnd = CreateWindowEx(
        0, CLASS_NAME, L"TranslateMessage Demo",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 500, 300,
        NULL, NULL, hInst, NULL);

    ShowWindow(hwnd, SW_SHOWDEFAULT);

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

See also

GetMessage DispatchMessage SendMessage MSG structure