01基本键盘操作——判断按键消息

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;
	}
}



 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值