?
Solved

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

Posted on 2003-12-01
2
Medium Priority
?
2,150 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 2000 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

770 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