无聊地把整张虚拟键码表放进一个数组^_^
#include <iostream> #include <iomanip> #include <windows.h> #include <array> using namespace std; void gotoXY(short x, short y) { COORD position = {x, y}; HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleCursorPosition(hConsole, position); } array<string,256> VKCode={ "", /*001-*/"LBUTTON","RBUTTON","CANCEL","MBUTTON","XBUTTON1","XBUTTON2","","BACK","TAB","", /*011-*/"","CLEAR","RETURN","","","SHIFT","CONTROL","MENU","PAUSE","CAPITAL", /*021-*/"","","","","","","ESCAPE","","","", /*031-*/"","SPACE","PRIOR","NEXT","END","HOME","LEFT","UP","RIGHT","DOWN", /*041-*/"SELECT","PRINT","EXECUTE","SNAPSHOT","INSERT","DELETE","HELP","0","1","2", /*051-*/"3","4","5","6","7","8","9","","","", /*061-*/"","","","","A","B","C","D","E","F", /*071-*/"G","H","I","J","K","L","M","N","O","P", /*081-*/"Q","R","S","T","U","V","W","X","Y","Z", /*091-*/"LWIN","RWIN","APPS","","SLEEP", /*096-*/"NUMPAD0","NUMPAD1","NUMPAD2","NUMPAD3","NUMPAD4", /*101-*/"NUMPAD5","NUMPAD6","NUMPAD7","NUMPAD8","NUMPAD9", /*106-*/"MULTIPLY","ADD","SEPARATOR","SUBTRACT","DECIMAL", /*111-*/"DIVIDE","F1","F2","F3","F4","F5","F6","F7","F8","F9", /*121-*/"F10","F11","F12","F13","F14","F15","F16","F17","F18","F19", /*131-*/"F20","F21","F22","F23","F24","","","","","", /*141-*/"","","","NUMLOCK","SCROLL","","","","","", /*151-*/"","","","","","","","","","LSHIFT", /*161-*/"RSHIFT","LCONTROL","RCONTROL","LMENU","RMENU", /*166-*/"BROWSER_BACK","BROWSER_FORWARD","BROWSER_REFRESH","BROWSER_STOP","BROWSER_SEARCH", /*171-*/"BROWSER_FAVORITES","BROWSER_HOME","VOLUME_MUTE","VOLUME_DOWN","VOLUME_UP", /*176-*/"MEDIA_NEXT_TRACK","MEDIA_PREV_TRACK","MEDIA_STOP","MEDIA_PLAY_PAUSE","LAUNCH_MAIL", /*181-*/"LAUNCH_MEDIA_SELECT","LAUNCH_APP1","LAUNCH_APP2","","", /*186-*/"OEM_1","OEM_PLUS","OEM_COMMA","OEM_MINUS","OEM_PERIOD", /*191-*/"OEM_2","OEM_3","","","","","","","","", /*201-*/"","","","","","","","","","", /*211-*/"","","","","","","","","OEM_4","OEM_5", /*221-*/"OEM_6","OEM_7","OEM_8", /* 186【;:】VK_OEM_1 187【=+】VK_OEM_PLUS 188【,<】VK_OEM_COMMA 189【-_】VK_OEM_MINUS 190【.>】VK_OEM_PERIOD 191【/?】VK_OEM_2 192【`~】VK_OEM_3 219【[{】VK_OEM_4 220【\|】VK_OEM_5 221【]}】VK_OEM_6 222【'"】VK_OEM_7 空值为未定义值或非标准键盘的Key */ }; int main() { gotoXY(15,8); cout<<"请按键:"; while(1){ for (int i=1;i<=255;i++){ if (GetAsyncKeyState(i)&1){ gotoXY(24,8); cout<<"\t\t\t\t\b\b\b\b"<<dec<<i<<"(10)"<<hex<<i<<"(16):VK_"<<VKCode.at(i); } } } return 0; }