Solved

Capturing KeyBoard Events in MFC

Posted on 1998-12-02
8
4,500 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now