Link to home
Start Free TrialLog in
Avatar of orange_juice
orange_juice

asked on

handle enter key in richedit control

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
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

For cursor selection

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


set nStartChar, nEndChar with same value for setting cursor

GOOD LUCK
Avatar of orange_juice
orange_juice

ASKER

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.

Avatar of DanRollins
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

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??
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it work!!! thank you ^_^