?
Solved

Capturing KeyBoard Events in MFC

Posted on 1998-12-02
8
Medium Priority
?
4,801 Views
Last Modified: 2008-02-20
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  
0
Comment
Question by:KevinT120298
  • 5
  • 3
8 Comments
 
LVL 4

Expert Comment

by:pagladasu
ID: 1179138
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

0
 

Author Comment

by:KevinT120298
ID: 1179139
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.


0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1179140
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


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:KevinT120298
ID: 1179141
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









0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1179142
OK Kevin, I am giving it a try and send you a feedback.
0
 
LVL 4

Expert Comment

by:pagladasu
ID: 1179143
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
0
 

Author Comment

by:KevinT120298
ID: 1179144
The Base class of my project is CFormView, and I have controls on the form.  I need to
capture the events on the form
0
 
LVL 4

Accepted Solution

by:
pagladasu earned 800 total points
ID: 1179145
I think the solution would be to trap the PreTranslateMessage function. It takes a pointer to the MSG structure. Use the message attribute to check whether the message is WM_KEYUP or not( it could also be WM_KEYDOWN). Use the wParam(if needed) attribute to test the key value and do whatever you want. Here is a sample code:

BOOL CMyView::PreTranslateMessage(MSG* pMsg)
{
      // TODO: Add your specialized code here and/or call the base class
      if(pMsg->message==WM_KEYUP){
                  MessageBox("Keyed");
      }
      return CFormView::PreTranslateMessage(pMsg);
}

Thanks
pagladasu
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question