键盘
// 扫描码
#define VKK_ESC 0x011b //ESC
#define VKK_F1 0x3b00 //F1
#define VKK_F2 0x3c00 //F2
#define VKK_F3 0x3d00 //F3
#define VKK_F4 0x3e00 //F4
#define VKK_F5 0x3f00 //F5
#define VKK_F6 0x4000 //F6
#define VKK_F7 0x4100 //F7
#define VKK_F8 0x4200 //F8
#define VKK_F9 0x4300 //F9
#define VKK_F10 0x4400 //F10
#define VKK_~ 0x2960 //~
#define VKK_1 0x0231 //1
#define VKK_ESC 0x011b //ESC
#define VKK_F1 0x3b00 //F1
#define VKK_F2 0x3c00 //F2
#define VKK_F3 0x3d00 //F3
#define VKK_F4 0x3e00 //F4
#define VKK_F5 0x3f00 //F5
#define VKK_F6 0x4000 //F6
#define VKK_F7 0x4100 //F7
#define VKK_F8 0x4200 //F8
#define VKK_F9 0x4300 //F9
#define VKK_F10 0x4400 //F10
#define VKK_~ 0x2960 //~
#define VKK_1 0x0231 //1
#define VKK_2 0x0332 //2
#define VKK_3 0x0433 //3
#define VKK_4 0x0534 //4
#define VKK_5 0x0635 //5
#define VKK_6 0x0736 //6
#define VKK_7 0x0837 //7
#define VKK_8 0x0938 //8
#define VKK_9 0x0a39 //9
#define VKK_0 0x0b30 //0
#define VKK_- 0x0c2d //-
#define VKK_= 0x0d3d //=
#define VKK_\ 0x2b5c //\
#define VKK_BackSpace 0x0e08 //退格键
#define VKK_Tab 0x0f09 //Tab
#define VKK_q 0x1071 //q
#define VKK_3 0x0433 //3
#define VKK_4 0x0534 //4
#define VKK_5 0x0635 //5
#define VKK_6 0x0736 //6
#define VKK_7 0x0837 //7
#define VKK_8 0x0938 //8
#define VKK_9 0x0a39 //9
#define VKK_0 0x0b30 //0
#define VKK_- 0x0c2d //-
#define VKK_= 0x0d3d //=
#define VKK_\ 0x2b5c //\
#define VKK_BackSpace 0x0e08 //退格键
#define VKK_Tab 0x0f09 //Tab
#define VKK_q 0x1071 //q
#define VKK_w 0x1177 //w
#define VKK_e 0x1265 //e
#define VKK_r 0x1372 //r
#define VKK_t 0x1474 //t
#define VKK_y 0x1579 //y
#define VKK_u 0x1675 //u
#define VKK_i 0x1769 //i
#define VKK_o 0x186f //o
#define VKK_p 0x1970 //p
#define VKK_[ 0x1a5b //[
#define VKK_] 0x1b5d //]
#define VKK_a 0x1e61 //a
#define VKK_s 0x1f73 //s
#define VKK_d 0x2064 //d
#define VKK_f 0x2166 //f
#define VKK_e 0x1265 //e
#define VKK_r 0x1372 //r
#define VKK_t 0x1474 //t
#define VKK_y 0x1579 //y
#define VKK_u 0x1675 //u
#define VKK_i 0x1769 //i
#define VKK_o 0x186f //o
#define VKK_p 0x1970 //p
#define VKK_[ 0x1a5b //[
#define VKK_] 0x1b5d //]
#define VKK_a 0x1e61 //a
#define VKK_s 0x1f73 //s
#define VKK_d 0x2064 //d
#define VKK_f 0x2166 //f
#define VKK_g 0x2267 //g
#define VKK_h 0x2368 //h
#define VKK_j 0x246a //j
#define VKK_k 0x256b //k
#define VKK_l 0x266c //l
#define VKK_; 0x273b //;
#define VKK_' 0x2827 //'
#define VKK_Enter 0x1c0d //回车
#define VKK_z 0x2c7a //z
#define VKK_x 0x2d78 //x
#define VKK_c 0x2e63 //c
#define VKK_v 0x2f76 //v
#define VKK_b 0x3062 //b
#define VKK_n 0x316e //n
#define VKK_m 0x326d //m
#define VKK_h 0x2368 //h
#define VKK_j 0x246a //j
#define VKK_k 0x256b //k
#define VKK_l 0x266c //l
#define VKK_; 0x273b //;
#define VKK_' 0x2827 //'
#define VKK_Enter 0x1c0d //回车
#define VKK_z 0x2c7a //z
#define VKK_x 0x2d78 //x
#define VKK_c 0x2e63 //c
#define VKK_v 0x2f76 //v
#define VKK_b 0x3062 //b
#define VKK_n 0x316e //n
#define VKK_m 0x326d //m
#define VKK_ 0x332c //
#define VKK_. 0x342e //.
#define VKK_/ 0x352f ///
#define VKK_空格键 0x3920 //空格键
#define VKK_Insert 0x5200 //Insert
#define VKK_Home 0x4700 //Home
#define VKK_Page UP 0x4900 //Page UP
#define VKK_Delete 0x5300 //Delete
#define VKK_End 0x4f00 //End
#define VKK_PageDown 0x5100 //PageDown
#define VKK_上箭头 0x4800 //上箭头
#define VKK_左箭头 0x4b00 //左箭头
#define VKK_下箭头 0x5000 //下箭头
#define VKK_右箭头 0x4d00 //右箭头
#define VKK_/ 0x352f ///
#define VKK_. 0x342e //.
#define VKK_/ 0x352f ///
#define VKK_空格键 0x3920 //空格键
#define VKK_Insert 0x5200 //Insert
#define VKK_Home 0x4700 //Home
#define VKK_Page UP 0x4900 //Page UP
#define VKK_Delete 0x5300 //Delete
#define VKK_End 0x4f00 //End
#define VKK_PageDown 0x5100 //PageDown
#define VKK_上箭头 0x4800 //上箭头
#define VKK_左箭头 0x4b00 //左箭头
#define VKK_下箭头 0x5000 //下箭头
#define VKK_右箭头 0x4d00 //右箭头
#define VKK_/ 0x352f ///
#define VKK_* 0x372a //*
#define VKK_- (注意 0x4a2d //- (注意
#define VKK_7 0x4737 //7
#define VKK_8 0x4838 //8
#define VKK_9 0x4939 //9
#define VKK_4 0x4b34 //4
#define VKK_5 0x4c35 //5
#define VKK_6 0x4d36 //6
#define VKK_+ 0x4e2b //+
#define VKK_1 0x4f31 //1
#define VKK_2 0x5032 //2
#define VKK_3 0x5133 //3
#define VKK_0 0x5230 //0
#define VKK_Del 0x532e //Del
#define VKK_- (注意 0x4a2d //- (注意
#define VKK_7 0x4737 //7
#define VKK_8 0x4838 //8
#define VKK_9 0x4939 //9
#define VKK_4 0x4b34 //4
#define VKK_5 0x4c35 //5
#define VKK_6 0x4d36 //6
#define VKK_+ 0x4e2b //+
#define VKK_1 0x4f31 //1
#define VKK_2 0x5032 //2
#define VKK_3 0x5133 //3
#define VKK_0 0x5230 //0
#define VKK_Del 0x532e //Del
窗口属性(具备输入焦点)
窗口函数通过捕获WM_SETFOCUS和WM_KILLFOCUS消息确定当前窗口是否具有输入焦点
键盘消息 字符消息(组合)
按键消息(按下松开) 非系统按键消息(不用alt键组合)
系统按键消息(alt键相关windows内部)【更改系统键消息要调用DefWindowsProc()函数】
按键消息变量 wParam(识别按下键的虚拟码)
lParam(32位的变量) 0-15重复计数位
16-23OEM扫描码
24扩展键标志
25-28保留位
29关联码
30键的先前状态
31转换状态
WinMain函数的消息循环中包含TranslateMessage函数,把按键消息转换为字符消息,但只有当键盘驱动程序把键盘字符映射成ASCII码后才能产生WM_CHAR消息即字符消息。
字符消息 系统:WM_SYSCHAR WM_SYSDEADCHAR
非系统:WM_CHAR WM_DEADCHAR
键盘实例:
要求:设计一个窗口,在该窗口中练习键盘的响应,要求如下:
(1) 键盘上的向上箭头时,窗口中显示“You had hit the up key”
(2) 单机<Shift>键时,窗口中显示“You had hit the SHIFT key”
(3) 单机<Ctrl>键时,窗口中显示“You had hit the CTRL key”
(4) 单机<Ctrl+A>键时,窗口中显示“You had hit the Ctrl A key”
(5) 单机<Shift+B>键时,窗口中显示“You had hit the SHIFT B key”
程序:
long WINAPI WndProc(HWND hWnd,UINT iMessage,UINT wParam,LONG lParam)
{HDC hDC; //定义设备环境句柄
PAINTSTRUCT ps; //定义包含绘图信息的结构体变量
HPEN hPen; //定义画笔句柄
HBRUSH hBrush; //定义画刷句柄
//以下定义输出字符串
static char cUp[]="You had hit the UP key";
static char cCtrl[]="You had hit the Ctrl key";
static char cShift[]="You had hit the SHIFT key";
static char cCtrl_A[]="You had hit the CTRL A key";
static char cShift_B[]="You had hit the SHIFT B key";
//下面定义并初始化按键标志变量
static BOOL nUpKeyDown=FALSE;
static BOOL nShiftKeyDown=FALSE;
static BOOL nCtrlKeyDown=FALSE;
static BOOL nCtrlAKeyDown=FALSE;
static BOOL nShiftBKeyDown=FALSE;
switch(iMessage)
{
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_UP: //当按上箭头键时 ,变量置为真
nUpKeyDown=TRUE; break;
case VK_SHIFT: //当按shift键时,变量置为真
nShiftKeyDown=TRUE; break;
case VK_CONTROL: //当按control键时,变量置为真
nCtrlKeyDown=TRUE; break;
default: break;
}
}
break;
case WM_KEYUP:
InvalidateRect(hWnd,NULL,FALSE); break;
case WM_CHAR:
if(wParam==(65&VK_CONTROL))
{
if(nCtrlKeyDown==TRUE)
{nCtrlAKeyDown=TRUE; nCtrlKeyDown=FALSE;}
}
else if(wParam==98||wParam==66) //当按下b键时
{
if(nShiftKeyDown==TRUE) //检查shift键是否处于按下状态 {nShiftBKeyDown=TRUE; //当SHIFT键按下时 ,变量置为真
nShiftKeyDown=FALSE;}
}break;
case WM_PAINT: //处理绘图消息
hDC=BeginPaint(hWnd,&ps);
mousemove是什么键 hBrush=(HBRUSH)GetStockObject(WHITE_BRUSH); //创建白画刷
hPen=(HPEN)GetStockObject(WHITE_PEN); //创建白画笔 SelectObject(hDC,hPen); //选入白画刷
SelectObject(hDC,hBrush); //选入白画笔 SetTextColor(hDC,RGB(255,0,0)); //设置字体颜为红
//输出信息
if(nUpKeyDown==TRUE)
{
Rectangle(hDC,0,0,300,200);
TextOut(hDC,0,0,cUp,strlen(cUp)); //cUp[]="You had hit the UP key"
nUpKeyDown=FALSE;
}
else if(nCtrlAKeyDown==TRUE)
{
Rectangle(hDC,0,0,300,200);
TextOut(hDC,0,100,cCtrl_A,strlen(cCtrl_A));
nCtrlAKeyDown=FALSE;
nCtrlKeyDown=FALSE;
}
else if(nCtrlKeyDown==TRUE&&nCtrlAKeyDown==FALSE)
{
Rectangle(hDC,0,0,300,200);
TextOut(hDC,0,60,cCtrl,strlen(cCtrl));
nCtrlKeyDown=FALSE;
}
else if(nShiftBKeyDown==TRUE)
{
Rectangle(hDC,0,0,300,200);
TextOut(hDC,0,0,cShift_B,strlen(cShift_B));
nShiftBKeyDown=FALSE;
nShiftKeyDown=FALSE;
}
else if(nShiftBKeyDown==FALSE&&nShiftKeyDown==TRUE)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论