Link to home
Start Free TrialLog in
Avatar of KevinT120298
KevinT120298

asked on

Capturing KeyBoard Events in MFC

How do I trap Keyboard events in Microsoft Visual C++ with
an SDI Application with multiple views. The WM_KEYDOWN & 
WM_CHAR messages work in a Dialog based application, but not in my SDI application. Do I have to explicitly set the focus of the view, or is there a different event handler?

Kevin  
Avatar of pagladasu
pagladasu

You use classwizard and select your view class in the class name drop down list box. The WM_KEYDOWN, WM_CHAR as well as WM_KEYUP handlers are all present.
You write your code there and see if it works.
Thanks
pagladasu

Avatar of KevinT120298

ASKER

Make an SDI program with project wizard  and in MFC Wizard put a handler for WM_CHAR or WM_KEYDOWN.  Neither will get called when you run the program and press a key.  However, making a dialog based program will route the key event to the handler.


I have done exactly what you said. Created an SDI application and using ClassWizard mapped the WM_CHAR ( OnChar) and WM_KEYDOW (OnKeyDown) for the VIEW class. This point is important - map the messages to the view class - make sure it is seleceted in the drop down list box. And it works perfectly OK. Here is the code snippet.

void CKtestView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      // TODO: Add your message handler code here and/or call default
      MessageBox("ONCHAR");
      CView::OnChar(nChar, nRepCnt, nFlags);
}

void CKtestView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      // TODO: Add your message handler code here and/or call default
      MessageBox("KD");
      CView::OnKeyDown(nChar, nRepCnt, nFlags);
}

I have also tested it out with a dialog based application and it also works. Here is its code snippet.

void CKtest2Dlg::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      // TODO: Add your message handler code here and/or call default
      MessageBox("ONCHAR");      
      CDialog::OnChar(nChar, nRepCnt, nFlags);
}

void CKtest2Dlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      // TODO: Add your message handler code here and/or call default
      MessageBox("KD");
      CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

Hope this will help. Thanks
pagladasu


Pagladasu,

  Your solution works when the Base Class of the project is derived from CView, but it
doesn't work when the Base class of the project is derived from CFormView, which is
the base class of my project. Give it a try and see what happens?

Kevin









OK Kevin, I am giving it a try and send you a feedback.
Just checked it out. The FormView does respond to these events only when there aren't any controls in the view. If there is a control, then it does not respond.
Thanks
pagladasu
The Base class of my project is CFormView, and I have controls on the form.  I need to
capture the events on the form
ASKER CERTIFIED SOLUTION
Avatar of pagladasu
pagladasu

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