首先,简介一下键盘消息:
(1)系统消息:(系统键Alt,F10等),由系统内部处理
WM_SYSKEYDOWN
WM_SYSKEYUP
WM_SYSCHAR
(2)非系统消息:
WM_KEYDOWN
WM_KEYUP
WM_CHAR
二、添加消息(一般WM_KEYDOWN),在消息响应函数
OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
有三个变量,nChar这是虚拟键代码,另外两个不常用,用时参看MSDN。
如果要响应组合键,可以用GetAsyncKeyState()检测按键状态
如:
case VK_UP:
if (GetAsyncKeyState(VK_LSHIFT))
{
// 左shift+up
}
break;
三,下面介绍虚拟键代码:
0~9,A~Z 为ASCII码,(去除ASCII码前的0),如A为65,不是065;
其它的为VK码,如下所示:
VK值列表
VK_CANCEL Ctrl+Break
VK_TAB Tab
VK_RETURN Enter
VK_SHIFT Shift
VK_CONTROL Ctrl
VK_MENU Alt
VK_LBUTTON 01 鼠标左键
VK_RBUTTON 02 鼠标右键
VK_CANCEL 03 用于执行Ctrl+C或Ctrl+Break
VK_MBUTTON 04 鼠标中键
VK_BACK 08 Backspace键
VK_TAB 09 Tab键
VK_CLEAR 0C Clear键
VK_RETURN 0D Enter键
VK_SHIFT 10 Shift键
VK_CONTROL 11 Ctrl键
VK_MENU 12 Arl键
VK_PAUSE 13 Pause键
VK_CAPITAL 14 Capslock键
VK_ESCAPE 1B Ese键
VK_SPACE 20 SpaceBar键
VK_PRIOR 21 PgUp键
VK_NEXT 22 PgDn键
VK_END 23 End键
VK_HOME 24 Home键
VK_LEFT 25 Left Arrow键
VK_UP 26 Up Arrow键
VK_RIGHT 27 Right Arrow键
VK_DOWN 28 Down Arrow键
VK_SELECT 29 Select键
VK_EXECUTE 2B Execute键
VK_SNAPSHOT 2C PrintScreen键
VK_INSERT 2D Ins键
VK_DELECT 2E Del键
VK_HELP 2F Help键
VK_0 30 0键
.........
VK_9 39 9键
VK_A 41 A键
… … …
VK_Z 5A Z键
VK_NUMAPD0 60 数字板0键
… … …
VK_NUMAPD9 69 数字板9键
VK_MULTIPY 6A 乘号键
VK_ADD 6B 加号键
VK_SEPARATOR 6C Separator键
VK_SUBSTRACT 6D 减号键
VK_DECIMAL 6E 小数点键
VK_DIVIDE 6F 除号键
VK_F1 70 F1键
… … …
VK_F24 87 F24键
VK_NUMLOCK 90 NumLock键
VK_SCROLL 91 ScrollLock键
VK_ESCAPE Esc
VK_SPACE Space
VK_PRIOR Page Up
VK_NEXT Page Down
VK_END End
VK_HOME Home
VK_LEFT 向左方向键
VK_UP
VK_RIGHT
VK_DOWN
VK_DELETE Delete
VK_INSERT Inser
VK_NUMPAD0~VK_NUMPAD9 小键盘上的0-9键
VK_F1-VK_f12 F1-F12键
keybd_event(VK_LCONTROL,0,0,0); //模拟按下ctrl键
keybd_event(VK_LCONTROL,0,KEYEVENTF_KEYUP,0); //释放ctrl键
keybd_event(VK_SHIFT,0,KEYEVENTF_KEYUP,0); //释放shift键