[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

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

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: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

650 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