How to Run Code when the Tab Key is Pressed? Topic is solved

This forum can be used to talk about general design strategies, new ideas and questions in general related to wxWidgets. If you feel your questions doesn't fit anywhere, put it here.
Post Reply
User avatar
ColleenKobe
Experienced Solver
Experienced Solver
Posts: 88
Joined: Mon Aug 31, 2015 3:47 pm

How to Run Code when the Tab Key is Pressed?

Post by ColleenKobe » Wed Mar 01, 2017 6:25 pm

I have three wxTextCtrl boxes. I want the user to be able to enter text into a box, and then press the Enter or Tab key to move to the next box. On leaving the first box, I want to do the following:
  • Execute a function to verify what the user typed is a legal value.
    If the value is legal:
    • save the new value in a table;
    else
    • display an error message;
      revert the text box contents to the original value.
    In both cases,
    • change the first box's text field background color to the default color (white);
      move to the second box; and
      change the second box's text field background color to "highlighted" (yellow).
    done.
So far, I have created a procedure that runs on the wxEVT_COMMAND_TEXT_ENTER event. It works just the way I want it to.

My problem is that I don't know how to set up code to handle when the Tab key is pressed. When I press the Tab key, the cursor moves to the next field but of course it does not call my error-checking routine or save the field contents if they are correct. If I set wxTE_PROCESS_TAB, tab spaces get inserted into the wxTextCtrl box, which isn't what I want.

The only wxTextCtrl flag I have set is wxTE_PROCESS_ENTER.

What is the best way to set up code to run when the Tab key is pressed? By capturing the most recent key pressed? How do you do that?

I am using wxCrafter version 2.6 in CodeLite version 10.0.0.

Thanks.

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

Re: How to Run Code when the Tab Key is Pressed?

Post by doublemax » Wed Mar 01, 2017 6:36 pm

I would catch the wxEVT_SET_FOCUS/wxEVT_KILL_FOCUS event on all text controls. One of the advantages would be, that these even get triggered when the user changes focus by clicking into another text field.

http://docs.wxwidgets.org/trunk/classwx ... event.html
Use the source, Luke!

User avatar
ColleenKobe
Experienced Solver
Experienced Solver
Posts: 88
Joined: Mon Aug 31, 2015 3:47 pm

Re: How to Run Code when the Tab Key is Pressed?

Post by ColleenKobe » Wed Mar 01, 2017 7:21 pm

BEAUTIFUL! The wxEVT_SET_FOCUS and wxEVT_KILL_FOCUS worked perfectly!
And they worked the first time, too! How often does THAT happen?

Thank you, doublemax!

Colleen

Post Reply