Solved

handle enter key in richedit control

Posted on 2002-05-14
6
682 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
Independent Software Vendors: 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Tab names are off by one 5 54
ffmpeg - "rtsp://...... Operation not permitted" 4 89
only14 challenge 19 84
Problem to App 4 120
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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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