'lo all,
I have a view with a few OnMouseWhatever() handlers set up, all of these work fine. But keyboard handlers (OnChar(), OnKeyDown()...) don't seem to work ! It seems like the handler function is never called. Any idea why ?
Thanks in advance.
Printable View
'lo all,
I have a view with a few OnMouseWhatever() handlers set up, all of these work fine. But keyboard handlers (OnChar(), OnKeyDown()...) don't seem to work ! It seems like the handler function is never called. Any idea why ?
Thanks in advance.
Where and how did you implement the"OnChar(), OnKeyDown()..." handlers that "don't seem to work"?
Sorry, I guess I was a bit vague.Quote:
Originally Posted by VictorN
My view (let's call it CMyView) is derived from CFormView. The handlers are defined as usual, i.e. "afx_msg void OnChar(params);" in the header, "ON_WM_CHAR()" in the message map, and the implementation "void CMyView::OnChar(params) { TRACE("test\n"); __super::OnChar(params); }" in the source file.
I've put a breakpoint in the function, so I'm pretty sure the function is never actually called. I've also tried OnKeyDown() and OnKeyUp() instead of OnChar(), just to be sure. None work.
The mouse handlers (OnMouseMove(), OnMouseWheel(), etc.) are defined in the same fashion, and they work like a charm.
Well, all these messages are redirected to the control currently having focus. Therefore, you cannot handle them in the CFormView derived class.
As a workaround (if you don't want handle them in the child controls' classes) you could add the virtual method PreTranslateMessage and handle WM_CHAR, WM_KEXDOWN, ... in it:Code:BOOL CMyView::PreTranslateMessage(MSG* pMsg)
{
UINT msg = pMsg->message;
if (msg == WM_KEYDOWN)
{
//handle it
...
}
else if( msg == WM_KEYUP)
{
//handle it
...
}
else if( msg == WM_CHAR)
{
//handle it
...
}
// return TRUE if you don't want this message to be processed somewere else
// or call the base class:
return CFormView::PreTranslateMessage(pMsg);
}
I'll try that. Thanks a lot!
Alright, problem solved! Thanks a lot VictorN!
Code:BOOL CMyView::PreTranslateMessage(MSG* pMsg)
{
UINT msg = pMsg->message;
if (msg == WM_CHAR)
{
UINT nChar = pMsg->wParam;
UINT nRepCnt = LOWORD(pMsg->lParam);
UINT nFlags = HIWORD(pMsg->lParam);
OnCharLikeFunction(nChar, nRepCnt, nFlags);
}
return __super::PreTranslateMsg(pMsg);
}