Adding an ENTER key event to wxRichTextCtrl

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
zubenel
In need of some credit
In need of some credit
Posts: 3
Joined: Fri Dec 07, 2012 2:40 pm

Adding an ENTER key event to wxRichTextCtrl

Post by zubenel » Fri Dec 07, 2012 3:05 pm

Hi,
I'm developing a simple application with a single panel frame containing, among other things, a wxRichTextCtrl component and a button, using wxDev-C++ (latest version) on Win XP. The button has an "onClick" event associated to it which executes some code and that works fine. Now I would like to be able to execute the same code that runs when clicking the button simply by pressing the ENTER, or return, key of the keyboard while typing text in the wxRichTextCtrl component. I tried by adding an "onReturn" event and assigning to it the button "onClick" event, but I get an "invalid static_cast" error associated with the macro that wxDev-C++ automatically generates when creating the "onReturn" event. The compiler is gcc. How could I solve this problem?
Thank you for your help.

User avatar
doublemax
Moderator
Moderator
Posts: 15289
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Adding an ENTER key event to wxRichTextCtrl

Post by doublemax » Fri Dec 07, 2012 6:04 pm

If i understand you correctly, the problem is that you want to connect the "return pressed" event to the "button clicked" event handler that you already have?

That's not possible, because they're taking different event types.

You need to move the code of your event handler to another "normal" method. Then, from both event handlers, you just call that method.
Use the source, Luke!

zubenel
In need of some credit
In need of some credit
Posts: 3
Joined: Fri Dec 07, 2012 2:40 pm

Re: Adding an ENTER key event to wxRichTextCtrl

Post by zubenel » Sun Dec 09, 2012 10:07 am

Thank you for your reply. I tried doing that but it still doesn't work. I have tried adding a wxKeyEvent handler as well as a EVT_TEXT_ENTER macro with a corresponding function to be launched when the enter key is pressed, but that didn't work either, although I set the style wxTE_PROCESS_ENTER for the text control. My problem is that I just want to run some code when the user presses the enter (or return) key in the rich text control, but since I'm new to wxWidgets (and C++ too) I can't do it, even though I tried using the wxWidgets class reference and tutorial to no avail.

Radek
Super wx Problem Solver
Super wx Problem Solver
Posts: 286
Joined: Sun Sep 11, 2011 7:17 am

Re: Adding an ENTER key event to wxRichTextCtrl

Post by Radek » Sun Dec 09, 2012 1:20 pm

This should work, zubenel. I guess that either the button handler or the wxEVT_COMMAND_TEXT_ENTER handler is not called at all. Build a debug target, put breaks in both handlers (you can put them on the "normal method" call) and see whether you reach the breaks on clicking the button and on pressing the Enter key. Also check whether the rich text control has the wxTE_PROCESS_ENTER style.

P.S.: Both wxEVT_COMMAND_BUTTON_CLICKED and wxEVT_COMMAND_TEXT_ENTER are command events so that it should be possible to use only one handler for both of them but, IMO, don't do it. They are different events. Doublemax's solution is, IMO, cleaner.

zubenel
In need of some credit
In need of some credit
Posts: 3
Joined: Fri Dec 07, 2012 2:40 pm

Re: Adding an ENTER key event to wxRichTextCtrl

Post by zubenel » Tue Dec 11, 2012 9:39 am

Finally I've solved it by using the EVT_CHAR_HOOK event associated with an onKey handler that launches a method only if the key code is equal to WXK_RETURN.
Thank you for your help.

Post Reply