Solved

handle enter key in richedit control

Posted on 2002-05-14
6
639 Views
Last Modified: 2013-11-20
I've derived a richedit control class and using it as some sort of language editor
with such a behavior

suppose what i've key in in the editor is 3 line like this (with cursor at first '100'

mov    100     101    0
org    100     999    0
exit   0

when i pressed ENTER, cursor should go to '101'
if i press arrow key, it should go to next line, with cursor at 'org'
if i press BACKSPACE, cursor should go to mov

only predefined 'command' (i.e. org, exit, mov is allowed to be entered into editor.

problem i face is, i dunno how to control cursor position

pls give somecode to help me doing this. I think i'll handle it in CMyRichEditCtrl::Onkeydown()


thanks
0
Comment
Question by:orange_juice
  • 3
  • 2
6 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 7010366
For cursor selection

CRichEditCtrl::SetSel
void SetSel( long nStartChar, long nEndChar );


set nStartChar, nEndChar with same value for setting cursor

GOOD LUCK
0
 
LVL 1

Author Comment

by:orange_juice
ID: 7010406
roshmon,

   i thought of that and tried that too. For my need, that mean i have to 'on' overwrite key, and replace whatever in that position. I need to control that the cursor won't overshoot to 'spaces' space. i think that is tedious, and post this to seek a better idea.

thank you anyway.

0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7010551
If the selection you choose starts and stops at the same character, then you don't need to replace anything -- the cursor is simply a blinking 'insertion caret' at that location.

In order to take special actions on certain keystrokes, you can add an EN_MSGFILTER handler.  Then check the incoming codes.  You can 'eat' a keystroke by returning 1, or let it pass through by returning 0.

So, let's say you get an ENTER key press.  You have defined that to mean "advance the cursor to the next word" (or something like that).  At that point, you will determine the desired cursor position (let's say that you want the cursor to go in front of charater #53) and then use SetSel(53,53). And then return 1.  That way, the RE control will not take the normal action for the Enter key.

-- Dan

0
What Is Threat Intelligence?

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

 
LVL 1

Author Comment

by:orange_juice
ID: 7015820
Dan,

    i got the idea, but don't know how to do it by code. Can you give me step by step how to get the message?
anyway, this is what i attempt

I use a CRichEditCtrl derived m_richedit control in my dialog class CMyDlg

1) in OnInitDialog(), i call
             m_richedit.SetEventMask (m_richedit.GetEventMask() | ENM_KEYEVENTS);
     --->so that this EN_MSGFILTER work as in given in MSDN

2) i handle the EN_MSGFILTER in CMyDlg
 void CMyDlg::OnMsgfilterRichedit1(NMHDR* pNMHDR, LRESULT* pResult)
{
     //this is for catching, eating, and vomiting key press

     MSGFILTER *pMsgFilter = reinterpret_cast<MSGFILTER *>(pNMHDR);
     // TODO: The control will not send this notification unless you override the
     // CDialog::OnInitDialog() function to send the EM_SETEVENTMASK message
     // to the control with either the ENM_KEYEVENTS or ENM_MOUSEEVENTS flag
     // ORed into the lParam mask.
     
// don't know what to write here..... and cannot eat code since the function is void, cannot return    
     //if(pNMHDR->code ==    
     //*pResult = 0;
}

i believe i do it wrongly, .... pls help. i think i should handle it in the CMyRichEdit that i derive??
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 7016717
The LRESULT* pResult is what eventually gets returned.  So..

    *pResult= 1;  // eat the keystroke    
    *pResult= 0;  // allow it through

Your code to look for keystrokes will look like this:

    if (pMsgFilter->msg == WM_CHAR) {
          if ( pMsgFilter->wParam == VK_ENTER ) {
               // do somehting
               *pResult= 1;
               return;
          }
    }

(I did not have time to test this code)

-- Dan
0
 
LVL 1

Author Comment

by:orange_juice
ID: 7017677
it work!!! thank you ^_^
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

758 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

21 Experts available now in Live!

Get 1:1 Help Now