How to send WM_KEYDOWN message?
-
Hi, I have a dialog which contains different controls,and I want to send a WM_KEYDOWN message to the parent dialog when a control receives a WM_KEYDOWN message. In the dialog,I overrided the OnKeyDown() method as follow:
void Frm_WKFailureMatrix::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}and in the control,I also overrided the OnKeyDown() method , and inside that handler I call the ::SendMessage(...) method,as follow:
void WkMfcButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
int x = 0;
::SendMessage(GetOwner()->GetSafeHwnd() , WM_KEYDOWN , 0 , 0);
}My problem is that ::SendMessage() accept the third and fourth parameters as WPARAM and LPARAM(UINT_PTR and LONG_PTR). How can I convert nChar,nRepCnt and nFlags to WPARAM and LPARAM so the parent dialog will receive the correct parameters??? With best regards, Eli
-
Hi, I have a dialog which contains different controls,and I want to send a WM_KEYDOWN message to the parent dialog when a control receives a WM_KEYDOWN message. In the dialog,I overrided the OnKeyDown() method as follow:
void Frm_WKFailureMatrix::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}and in the control,I also overrided the OnKeyDown() method , and inside that handler I call the ::SendMessage(...) method,as follow:
void WkMfcButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
int x = 0;
::SendMessage(GetOwner()->GetSafeHwnd() , WM_KEYDOWN , 0 , 0);
}My problem is that ::SendMessage() accept the third and fourth parameters as WPARAM and LPARAM(UINT_PTR and LONG_PTR). How can I convert nChar,nRepCnt and nFlags to WPARAM and LPARAM so the parent dialog will receive the correct parameters??? With best regards, Eli
-
Hi, I have a dialog which contains different controls,and I want to send a WM_KEYDOWN message to the parent dialog when a control receives a WM_KEYDOWN message. In the dialog,I overrided the OnKeyDown() method as follow:
void Frm_WKFailureMatrix::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}and in the control,I also overrided the OnKeyDown() method , and inside that handler I call the ::SendMessage(...) method,as follow:
void WkMfcButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
int x = 0;
::SendMessage(GetOwner()->GetSafeHwnd() , WM_KEYDOWN , 0 , 0);
}My problem is that ::SendMessage() accept the third and fourth parameters as WPARAM and LPARAM(UINT_PTR and LONG_PTR). How can I convert nChar,nRepCnt and nFlags to WPARAM and LPARAM so the parent dialog will receive the correct parameters??? With best regards, Eli
Why are you doing such a messy thing? This way, you will be writing keydown handlers for each and every control. Do you want your code to look like Sphagetti? What if you add two new controls later? Did I not ask you to override the
PreTranslateMessage()
? Did you ever try that? -
Why are you doing such a messy thing? This way, you will be writing keydown handlers for each and every control. Do you want your code to look like Sphagetti? What if you add two new controls later? Did I not ask you to override the
PreTranslateMessage()
? Did you ever try that?Hi brahmma, I know the way of overriding PreTranslateMessage. The problem is that I've put a breakpoint inside that function but I never stop there. My code is :
virtual BOOL PreTranslateMessage(MSG\* pMsg);
and the implementation is :
BOOL Frm_WKFailureMatrix::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
int x = 0;
}
return TRUE;
}However,if i move the focus into a button(by mouse click),the button receives the WM_KEYDOWN message. 1. Is it because of that automatically when I select a window , the focus is moving into the first control inside that window? 2. As far as I understand , the parrent windw shall receive the WM_KEYDOWN message and dispatch it to all of its children , isn't it??? Thanks again, Eli
-
toxcct wrote:
static_cast<>()...
Actually, that won't work correctly. The OP actually needs to pack the three parameters back into the wParam and lParam. The MAKELPARAM and MAKEWPARAM macros will do this. Refer to the WM_KEYDOWN doc to determine where each item goes. Judy
-
toxcct wrote:
static_cast<>()...
Actually, that won't work correctly. The OP actually needs to pack the three parameters back into the wParam and lParam. The MAKELPARAM and MAKEWPARAM macros will do this. Refer to the WM_KEYDOWN doc to determine where each item goes. Judy
-
Hi brahmma, I know the way of overriding PreTranslateMessage. The problem is that I've put a breakpoint inside that function but I never stop there. My code is :
virtual BOOL PreTranslateMessage(MSG\* pMsg);
and the implementation is :
BOOL Frm_WKFailureMatrix::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
int x = 0;
}
return TRUE;
}However,if i move the focus into a button(by mouse click),the button receives the WM_KEYDOWN message. 1. Is it because of that automatically when I select a window , the focus is moving into the first control inside that window? 2. As far as I understand , the parrent windw shall receive the WM_KEYDOWN message and dispatch it to all of its children , isn't it??? Thanks again, Eli
if(pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam == VK_ESCAPE)
AfxMessageBox(_T("You pressed the Escape Key"));
}This is just an example. Use the virtual key codes to verify which key was pressed. Check out MSDN for virtual key codes.