Solved

Capturing KeyBoard Events in MFC

Posted on 1998-12-02
8
4,557 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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 200 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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

777 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