Handle Key Down Events on Active CView

Step 1

Add OnKeyDown method declaration in .h file of subclass of CView like as:           
  1. afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags); 
nChar

         Virtual key code of the given key. see Winuser.h

nRepCnt

         Repeat count.

nFlags

         Specifies the scan code, key-transition code, previous key state, and context code.

Step 2

Add ON_WM_KEYDOWN() in BEGIN_MESSAGE_MAP and END_MESSAGE_MAP in .m file of subclass of CView like as:        
  1. BEGIN_MESSAGE_MAP(CDrawViewView, CView)  
  2.         ON_WM_KEYDOWN()  
  3.         END_MESSAGE_MAP()  
Here , CDrawViewView is a subclass of CView.
 
Step 3

Add OnKeyDown event definition in .m file like as:        
  1. /* Key Down Events */  
  2. void CDrawViewView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)  
  3. {  
  4.       switch (nChar)  
  5.       {  
  6.              case VK_DELETE:  
  7.                    AfxMessageBox(L"Delete Key Pressed");  
  8.                    break;  
  9.              default:  
  10.                    CView::OnKeyDown(nChar, nRepCnt, nFlags);  
  11.       }  
  12. }  
In above method , get delete key event and showing message box with text "Delete Key Pressed". And VK_DELETE define in winuser.h.
Ebook Download
View all
Learn
View all