Problems with modal dialog and PreTranslateMessage
I've got a problem with displaying modal dialog after key pressing. I'm doing it in thath way: first any key pressing is receiving in PreTranslateMessage function. Then i'm sending new user message which i'm handling in function "OnFunctionKeyPressed" that recognizing specific key. And in the end - after pressing i.e. F7 key - i want to display modal dialog. After DoModal - i'm receiving following error stack:
Code:
> SPR System.exe!CWnd::AssertValid() Line 896 + 0x24 bytes C++
SPR System.exe!AfxAssertValidObject(const CObject * pOb=0x025d48a8, const char * lpszFileName=0x0080c570, int nLine=2433) Line 107 C++
SPR System.exe!CWnd::GetTopLevelParent() Line 2435 C++
SPR System.exe!AfxInternalPreTranslateMessage(tagMSG * pMsg=0x002fe7f0) Line 241 + 0x8 bytes C++
SPR System.exe!CWinThread::PreTranslateMessage(tagMSG * pMsg=0x002fe7f0) Line 777 + 0x9 bytes C++
SPR System.exe!CSystemRTApp::PreTranslateMessage(tagMSG * pMsg=0x002fe7f0) Line 1132 C++
SPR System.exe!AfxPreTranslateMessage(tagMSG * pMsg=0x002fe7f0) Line 252 + 0x11 bytes C++
SPR System.exe!AfxInternalPumpMessage() Line 178 + 0x18 bytes C++
SPR System.exe!CWinThread::PumpMessage() Line 900 C++
SPR System.exe!AfxPumpMessage() Line 190 + 0xd bytes C++
SPR System.exe!CWnd::RunModalLoop(unsigned long dwFlags=4) Line 4386 + 0x5 bytes C++
SPR System.exe!CDialog::DoModal() Line 584 + 0xc bytes C++
Error in assertion
Code:
ASSERT(::IsWindow(m_hWnd));
function with key handling:
Code:
BOOL CDialogDMMenu::PreTranslateMessage(MSG* pMsg) {
if ( pMsg->message == WM_KEYDOWN ) {
::SendMessage( GetSafeHwnd(), WM_DMMENUKEYPRESSED, pMsg->wParam, 0 );
}
return CDialog::PreTranslateMessage(pMsg);
}
next function:
Code:
afx_msg LRESULT CDialogDMMenu::OnFunctionKeyPressed(WPARAM wParam, LPARAM lParam ) {
BYTE key = 0x00;
switch ( wParam ) {
case VK_F1 : key=0x01; break;
case VK_F2 : key=0x02; break;
case VK_F3 : key=0x03; break;
case VK_F4 : key=0x04; break;
case VK_F5 : key=0x05; break;
case VK_F6 : key=0x06; break;
case VK_F7 : key=0x07; break;
case VK_F8 : key=0x08; break;
case VK_F9 : key=0x09; break;
case VK_F10 : key=0x10; break;
case VK_F11 : key=0x11; break;
case VK_F12 : key=0x12; break;
case VK_ESCAPE : OnCancel(); break;
default : break;
}
functionKeyPressed(key);
return 0;
}
and ...
Code:
void CDialogDMMenu::functionKeyPressed(BYTE key) {
CDialogNewValue newValDialog(key);
if ( newValDialog.DoModal() == IDOK ) {
val = newValDialog.m_fValue;
CRealtime::getInstance()->getReader()->setCommand( command, val );
}
}
Where is error? Is there any other way to handle key pressing in application... Thank you. Regards...
Re: Problems with modal dialog and PreTranslateMessage
Dont play around PreTranslateMessage override, unless absolutely needed. In your case you are just handling WM_KEYDOWN, which can be handled by OnKeyDown.
In OnFunctionKeyPressed, what if used presses ESCAPE or ANY other non-function key?
Re: Problems with modal dialog and PreTranslateMessage
Is there a reason why you don't use WM_KEYUP/DOWN/PRESSED ? Saves you a lot of time implementing it.
edit : ajay beat me to it ;)
Re: Problems with modal dialog and PreTranslateMessage
My application is MDI. I need to react on function keys F1-F12 but not in every window separately - but globally in application. I think that WM_KEYUP/DOWN/PRESSED working only on controls that have focus on. I tried - but it didn't work.