Solved

Disable Default Tab Behavior

Posted on 2004-09-28
5
615 Views
Last Modified: 2013-11-17
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
0
Comment
Question by:AdrianSRU
  • 3
5 Comments
 
LVL 2

Accepted Solution

by:
mcanti earned 500 total points
ID: 12169732
Hi,

I'm using this method for catching all keys before they get processed by the default action handler:

I made myself a base-form class, which inherits directly from TForm. In the OnShortcut event of this base class, I check the Msg.CharCode parameter to see if it is a key I want to special-process. If it is, then I do the action for it and set Handled = true, else Handled = false; return;

That's about it. For any other questions, please reply...
0
 
LVL 2

Expert Comment

by:mcanti
ID: 12169762
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;
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 12169850
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.
0
 
LVL 12

Author Comment

by:AdrianSRU
ID: 12170100
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
0
 
LVL 2

Expert Comment

by:mcanti
ID: 12170523
I'm glad I could help you.
I'm glad we can help each other.
0

Featured Post

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!

Question has a verified solution.

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

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…

697 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