1 虚拟按键Virtual-Key的应用场景

在开发Win32程序时当需要程序接受键盘按键消息时,通常需要WM_KEYDOWN消息的wParam参数判断到底是按下了键盘的哪一个键,
示例代码可参考:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
    PAINTSTRUCT ps;

    switch (message)
    {
    case WM_KEYDOWN:
    {
        // W键
        if (wParam == 0x57)
        {
        }
        // A键
        else if (wParam == 0x41)
        {
        }
        // S键
        else if (wParam == 0x53)
        {
        }
        // D键
        else if (wParam = 0x44)
        {
        }

        break;
    }


    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

这里WM_KEYDOWN消息的wParam参数代表着键盘上按键的虚拟按键值Virtual-Key Codes,通过虚拟按键值我们可以确定键盘上是哪一个键被按下,并且做出相应的逻辑。

2 Windows虚拟按键Virtual-Key Codes大全

虚拟按键值 描述
VK_LBUTTON(0x01) 鼠标左键
VK_RBUTTON(0x02) 鼠标右键
VK_CANCEL(0x03) Control-break processing
VK_MBUTTON(0x04) 鼠标中键
VK_XBUTTON1(0x05) X1 mouse button
VK_XBUTTON2(0x06) X2 mouse button
VK_BACK(0x08) BackSpace删除键
VK_TAB(0x09) Tab键
VK_CLEAR(0xxC) CLEAR key
VK_RETURN(0x0D) Enter键
VK_SHIFT(0x10) Shift键
VK_CONTROL(0x11) Ctrl键
VK_MENU(0x12) Alt键
VK_PAUSE(0x13) Pause键
VK_CAPITAL(0x14) Caps键
VK_ESCAPE(0x1B) Esc键
VK_SPACE(0x20) Space 空格键
VK_PRIOR(0x21) Page Up键
VK_NEXT(0x22) Page Down键
VK_END(0x23) End键
VK_HOME(0x24) Home键
VK_LEFT(0x25) 方向左键
VK_UP(0x26) 方向上键
VK_RIGHT(0x27) 方向右键
VK_DOWN(0x28) 方向下键
VK_SELECT(0x29) Select Key
VK_PRINT(0x2A) Print键
VK_EXECUTE(0x2B) Execute键
VK_SNAPSHOT(0x2C) Print Screen键
VK_INSERT(0x2D) Ins键
VK_DELETE(0x2E) Del键
VK_HELP(0x2F) Help键
0x30 0键
0x31 1键
0x32 2键
0x33 3键
0x34 4键
0x35 5键
0x36 6键
0x37 7键
0x38 8键
0x39 9键
0x41 A键
0x42 B键
0x43 C键
0x44 D键
0x45 E键
0x46 F键
0x47 G键
0x48 H键
0x49 I键
0x4A J键
0x4B K键
0x4C L键
0x4D M键
0x4E N键
0x4F O键
0x50 P键
0x51 Q键
0x52 R键
0x53 S键
0x54 T键
0x55 U键
0x56 V键
0x57 W键
0x58 X键
0x59 Y键
0x5A Z键
VK_NUMPAD0(0x60) 数字键盘0键
VK_NUMPAD1(0x61) 数字键盘1键
VK_NUMPAD2(0x62) 数字键盘2键
VK_NUMPAD3(0x63) 数字键盘3键
VK_NUMPAD4(0x64) 数字键盘4键
VK_NUMPAD5(0x65) 数字键盘5键
VK_NUMPAD6(0x66) 数字键盘6键
VK_NUMPAD7(0x67) 数字键盘7键
VK_NUMPAD8(0x68) 数字键盘8键
VK_NUMPAD9(0x69) 数字键盘9键
VK_MULTIPLY(0x6A) Multiply key
VK_ADD(0x6B) Add key
VK_SEPARATOR(0x6C) Separator key
VK_SUBTRACT(0x6D) Subtract key
VK_DECIMAL(0x6E) Decimal key
VK_DIVIDE(0x6F) Divide key
VK_F1(0x70) F1键
VK_F2(0x71) F2键
VK_F3(0x72) F3键
VK_F4(0x73) F4键
VK_F5(0x74) F5键
VK_F6(0x75) F6键
VK_F7(0x76) F7键
VK_F8(0x77) F8键
VK_F9(0x78) F9键
VK_F10(0x79) F10键
VK_F11(0x7A) F11键
VK_F12(0x7B) F12键
VK_NUMLOCK(0x90) Num Lock键
VK_SCROLL(0x91) Scroll Lock键
VK_LSHIFT(0xA0) 左Shift键
VK_RSHIFT(0xA1) 右Shift键
VK_LCONTROL(0xA2) 左Ctrl键
VK_RCONTROL(0xA3) 右Ctrl键

参考链接