Solved

How to use the OnKeyDown() of a CEdit control?

Posted on 2003-12-01
2
2,121 Views
Last Modified: 2013-11-20
Hi,
I want to trap the Arrow Keys and the Enter key in a CEdit control.
I used the sample from MSDN:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q104/6/37.asp&NoWebContent=1

So I have a custom CMyEdit class with an overloaded OnKeyDown() function.
I added a CMyEdit control called mMyEdit in my dialog called CMyDlg.

Now I want to add code in CMyDlg to handle the event when I trap an Arrow Key. The function itself should look something like this:

void CMyDlg::OnMyEditKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
   // my code here
}

So how do I add such an event?
Thanks!
0
Comment
Question by:benhere
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 500 total points
ID: 9855481
1. In your derived EDIT class map the WM_KEYDOWN.
2. And in that routine OnKeyDown post a message to the parent like thos

#define WM_MY_EDITKEY_DOWM WM_USER+1200


void CMyEdit::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
      CWnd *pWnd = GetParent();
      
      if ( pWnd )
            pWnd->PostMessage(WM_MY_EDITKEY_DOWM, nChar, MAKELPARAM(nRepCnt,nFlags));

      CEdit::OnMyEditKeyDown(nChar, nRepCnt, nFlags);
}

3. And in your CMyDlg, map the message WM_MY_EDITKEY_DOWM

BEGIN_MESSAGE_MAP(CUpdateXMLDlg, CDialog)
      //{{AFX_MSG_MAP(CUpdateXMLDlg)
      // Class wizard message mapping
      //}}AFX_MSG_MAP
      ON_MESSAGE(WM_MY_EDITKEY_DOWM, OnMyEditKeyDown)
END_MESSAGE_MAP()

and in your header
agx_msg void OnMyEditKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);

4. Place the definition of the function in CPP

Good Luck
0
 
LVL 2

Author Comment

by:benhere
ID: 9862135
Thanks, it works! :-)

I just had to change a few details:


Change this:
CEdit::OnMyEditKeyDown(nChar, nRepCnt, nFlags);

To this:
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);


Change this:
agx_msg void OnMyEditKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);

To this:
afx_msg long OnMyEditKeyDown(WPARAM wParam, LPARAM lParam);


And my functions looks something like this:

long CMyDlg::OnMyEditKeyDown(WPARAM wParam, LPARAM lParam)
{
   UINT lChar = (UINT)wParam;

   //Check if the key pressed was a DOWN ARROW key
   if( lChar == VK_DOWN )
   {
      AfxMessageBox("It is a down arrow key!");
   }

   return 1;
}

0

Featured Post

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!

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

724 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