积极答复者
GetAsyncKeyState延迟问题

问题
-
我在编辑框OnChange中加入如下代码:
if (!GetAsyncKeyState(VK_BACK)& 0x8000f) //如果用户按的不是退格键,则进入if语句
{
if (bCorrected) //判断用户是否输入正确,并发声
{
m_nCorrect+=1;
PlaySound(L"res\\Type.wav",NULL,SND_FILENAME|SND_ASYNC);
}
else
{
m_nWrong+=1;
PlaySound(L"res\\Error.wav",NULL,SND_FILENAME|SND_ASYNC);
}}
我举个现象例子:在没按退格键的前提下,我输入字符串发声都是正常的。
但当我按下退格键后,紧接再输入一个字符时,那么无论字符是什么,都不进行发声。
我怀疑是GetAsyncKeyState函数获得了上次退格键状态,而不是这次的。
在这里求教各位了!
答案
-
BOOL CXXDlg::PreTranslateMessage(MSG* pMsg) { CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1); ASSERT(pEdit && pEdit->GetSafeHwnd()); if((WM_KEYDOWN == pMsg->message) && (pEdit->GetSafeHwnd() == pMsg->hwnd)) { if(GetKeyState(VK_BACK) < 0) { AfxMessageBox(_T("BACK Key is Press Down!")); } } return CDialog::PreTranslateMessage(pMsg); } Try this~
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
- 已建议为答案 i1friend 2012年7月19日 8:51
- 已标记为答案 Elegentin XieModerator 2012年7月24日 2:34
全部回复
-
BOOL CXXDlg::PreTranslateMessage(MSG* pMsg) { CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1); ASSERT(pEdit && pEdit->GetSafeHwnd()); if((WM_KEYDOWN == pMsg->message) && (pEdit->GetSafeHwnd() == pMsg->hwnd)) { if(GetKeyState(VK_BACK) < 0) { AfxMessageBox(_T("BACK Key is Press Down!")); } } return CDialog::PreTranslateMessage(pMsg); } Try this~
Visual C++ enthusiast, like network programming and driver development. At present is being engaged in the WinCE/Windows Mobile platform embedded development.
- 已建议为答案 i1friend 2012年7月19日 8:51
- 已标记为答案 Elegentin XieModerator 2012年7月24日 2:34