控制台程序 还是 MFC ?
1. 如果是控制台程序,使用 GetAsyncKeyState 函数
我以前写个一个小程序,你可以参考一下
2. 如果是MFC程序,你需要在PreTranslateMessage(MSG* pMsg)中捕获按键的消息,然后设置焦点(SetFocus())。类似下面的样子:
BOOL CXXXDialog(or CXXXView)::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam == VK_LEFT) {
//左键 .....
return CWnd::PreTranslateMessage(pMsg);
}
else if (pMsg->wParam == VK_RIGHT)
{
//右键.....
return CWnd::PreTranslateMessage(pMsg);
}
else if (pMsg->wParam == VK_UP)
{
//上键.....
return CWnd::PreTranslateMessage(pMsg);
}
else if (pMsg->wParam == VK_DOWN)
{
//下键.....
return CWnd::PreTranslateMessage(pMsg);
}
else if (pMsg->wParam == VK_TAB)
{
//Tab 键....
return CWnd::PreTranslateMessage(pMsg);
}
else if (pMsg->wParam == VK_ESCAPE)
{
//ESC 键....
return CWnd::PreTranslateMessage(pMsg);
}
else if(pMsg->wParam == VK_RETURN )
{
//回车键....
return CWnd::PreTranslateMessage(pMsg);
}
}
a
return CWnd::PreTranslateMessage(pMsg);
}
温馨提示:答案为网友推荐,仅供参考