WM_KEYDOWN:指示何时按非系统键
void CBaseKeyDemoView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar==VK_SHIFT) //判断Shift键是否被按下
{
//AfxMessageBox("dd");
bShiftdown=TRUE;
bShiftup=FALSE;
Invalidate(TRUE); //显示信息
}
CView::OnKeyDown(nChar, nRepCnt, nFlags);
}
WM_KEYUP:指示何时释放非系统键
void CBaseKeyDemoView::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if(nChar==VK_SHIFT) //判断Shift键是否被释放
{
//AfxMessageBox("dd");
bShiftup=TRUE;
Invalidate(TRUE); //显示信息
bShiftdown=FALSE;
}
CView::OnKeyUp(nChar, nRepCnt, nFlags);
}
WM_CHAR:将键盘事件传递给焦点窗口
void CBaseKeyDemoView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
if((nChar==98)||(nChar==66)) //判断是否敲击了字符键B键或b键
{
if(bShiftdown)
{
bShiftB=TRUE;
bShiftdown=FALSE;
Invalidate(TRUE); //显示信息
}
}
CView::OnChar(nChar, nRepCnt, nFlags);
}
显示信息
void CBaseKeyDemoView::OnDraw(CDC* pDC)
{
CBaseKeyDemoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(bShiftdown) //按下了Shift键
{
pDC->TextOut(20,20,"用户按下了Shift键!");
}
if(bShiftup) //释放了Shift键
{
pDC->TextOut(20,20,"用户释放了Shift键!");
}
if(bShiftB) //同时按下了Shift键和B键
{
pDC->TextOut(20,20,"用户同时按下Shift键和B键!");
bShiftB=FALSE;
}
}