Solved

Capturing KeyBoard Events in MFC

Posted on 1998-12-02
8
4,571 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

856 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