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
Solved

handle enter key in richedit control

Posted on 2002-05-14
6
672 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand to include initial dialog with two choices. 9 80
method notes when mouse over in eclipse 5 87
wait notify demo infinite loop 3 121
Excel file not created as expected 7 79
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

839 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