Page 1 of 1

How to Run Code when the Tab Key is Pressed?

Posted: Wed Mar 01, 2017 6:25 pm
by ColleenKobe
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.

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

Posted: Wed Mar 01, 2017 6:36 pm
by doublemax
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

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

Posted: Wed Mar 01, 2017 7:21 pm
by ColleenKobe
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