Link to home
Start Free TrialLog in
Avatar of paul_tsekov
paul_tsekov

asked on

How to handle WM_KEYDOWN in a Dialog???

Hello,
I have a problem.

I'm writing a standard application
on Visual C++, using Win32 and
the ATL classes included in
the headers ATLBase.h and ATLWin.h.
I use my own classes derived from:

CMyWindow : public CWindowImpl<CMyWindow>

CMyDialog : public CDialogImpl<CMyDialog>

I make my main window,
and then open a dialog at some time.

My question is:
How to handle for example the WM_KEYDOWN message in this
dialog.
I want when I press for example "Alt+A" and the focus
is in any of the controls in the dialog, this WM_KEYDOWN message to be handled from a single function
for example:
CMyDialog::OnKeyDown(.........),
not from this control's WM_KEYDOWN handler.

If this would help, I want to make the same as the Form KeyPreview property in Visual Basic does.
The WM_KEYDOWN should be handled somehow firstly by the dialog, and
then by the concreate control.

How could I do this???

Thank you in advance.

Pavel Tsekov, Varna, Bulgaria
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mblat
mblat

There is an article Q117563 in Microsoft Knowleage Base.
HOWTO: How to Trap WM_KEYDOWN Messages in a CDialog.

It shows the way for MFC based app.  I see that you are using WTL, but it may point you to the right direction.

Hope it helps.
Hi,

I am not sure about the architecture of ur app. If u r using MFC, then using classwizard, map the PreTranslateMessage(...) function to ur dialong class and modify it as

BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
     if(pMsg->message == WM_KEYDOWN)
     {
          //Here U R when a key is hit, but, Alt is handled differently.
          //So u can get here by hitting any other key combinations. Like
          //if(pMsg->wParam == 'B')
          // Key B is hit
          //and
          //if(GetKeyState(VK_CONTROL) & 0x8000) && (pMsg->wParam == 'A'))
          // Key Ctrl+B is hit
     }

     //When Alt is hit check like this
     if((GetKeyState(VK_MENU) & 0x8000) && (pMsg->wParam == 'A'))
     {
          // U R Here as User hit Alt+A
          //Call Ur Function!!!!
          DoKeyProcess();
     }
     
     return CDialog::PreTranslateMessage(pMsg);
}

void CTestDlg::DoKeyProcess()
{
     AfxMessageBox("Alt+A Hit");
}

Here DoKeyProcess()is declared as void DoKeyProcess()in public section of dialog's .h file

Try it out.
VinExpert

Please finalize the following year 2001 question:
https://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20246850
Moondancer - EE Moderator
i think vinexpert is right:)
bsr
Avatar of paul_tsekov

ASKER

No, the EXPERT is not right.
I'm using the CDialogImpl<> class,
not CDialog.
I'm not talking about MFC here.
I talk about ATL!!!!!!!!!!!!!!!!
No, the EXPERT is not right.
I'm using the CDialogImpl<> class,
not CDialog.
I'm not talking about MFC here.
I talk about ATL!!!!!!!!!!!!!!!!