Solved

handle enter key in richedit control

Posted on 2002-05-14
6
695 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
[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
  • 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
Technology Partners: 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!

 
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

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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 …
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: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 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