Solved

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

Posted on 2003-12-01
2
2,028 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
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 95
Expand to include initial dialog with two choices. 9 59
dog bark java program 15 82
move a line in eclipse 3 60
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

12 Experts available now in Live!

Get 1:1 Help Now