Link to home
Start Free TrialLog in
Avatar of AdrianSRU
AdrianSRU

asked on

Disable Default Tab Behavior

I am using C++ Builder 5.  What I need to be able to do is to disable the default tab behavior for some controls on a form and replace it with my own handler.

The KeyDown and KeyPress events do not catch the Tab key, which is pretty annoying.  The KeyUp event catches the Tab key but only after the default action has already occured.  I need to be able to capture the Tab key before it is processed and replace the default behavior with some code of my own.

I am writing a huge application and will need to implement this on a lot of forms (~150) so a simple answer would be preferred.


Thanks!

-Adrian
ASKER CERTIFIED SOLUTION
Avatar of mcanti
mcanti

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcanti
mcanti

I think you can check for the tab key in this event by matching Msg.CharCode against VK_TAB constant:

if (Msg.CharCode == VK_TAB)
{
 // Do Action;
  Handled = true;
}
else
{
  Handled = false;
}
return;
Another way is to use the GetKeyState(VK_TAB) or GetAsyncKeyState(VK_TAB) at an idle function at your Form.
Example:
void __fastcall TForm1::IdleLoop(TObject*, bool &done)
{
        TShiftState Shift;
        done = false;
        if(GetAsyncKeyState(VK_TAB) != 0 ){
         ShowMessage("TAB Press");
         Form1->KeyDown(0,Shift);//Pass control at form's keydown with a dummy key.
         return;}
}

gtokas.
Avatar of AdrianSRU

ASKER

mcanti, BRILLIANT!!!!!

I never thought it would be this easy.  I already have all of my forms inherit from a base-class in order to add additional functionality so I won't even have to modify every form.  When OnShortCut captures the tab key, I give the OnKeyDown function of the active control a chance to handle it.  If the control does not handle the tab key, then the default behavior is used.

WORD Key = Msg.CharCode;
if( Key != VK_TAB || ActiveControl == NULL )
    return;

AnsiString className = ActiveControl->ClassName( );
if( className == AnsiString( "TEdit" ) )
{
    if( ( ( TEdit * ) ActiveControl )->OnKeyDown != NULL )
        ( ( TEdit * ) ActiveControl )->OnKeyDown( ActiveControl, Key, TShiftState( ) );
}
//repeat for other control types.

if( Key == 0 )
    Handled = true;


gtokas, thanks for the response.  Your solution sounds like it would work but mcanti's solution is the one for me.


-Adrian
I'm glad I could help you.
I'm glad we can help each other.