Solved

Tab with OnKeyDown

Posted on 2001-07-08
6
442 Views
Last Modified: 2007-11-27
I have a form without any components on it in BCB4. I use the OnKeyDown event to respond to keyboard events. It works for all keys I need it for but the Tab key. How can I capture the Tab key using OnKeyDown?

Thanks
0
Comment
Question by:TropicalFish
  • 3
  • 2
6 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6263728
Most of the time the tab key is handled by Windows and your program
doesn't see it. There are couple of ways to go about getting at the tab key,
both involve responding to Windows messages. Have a look at the links below:

http://www.temporaldoorway.com/programming/cbuilder/advancedissue/oddkeys.htm 
And
http://www.bcbdev.com/faqs/faq78.htm 

0
 
LVL 2

Expert Comment

by:vbk_bgm
ID: 6263753
void OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags)
{
   if  (nChar == VK_TAB) //for the tab key
      //do your work

    //call the base class
     CWnd::OnKeyDown(nChar,nRepCnt,nFlags);
}
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 6273105
sniipet from http://www.bcbdev.com/faqs/faq78.htm 
...
To receive key events for the TAB key, the focused cotnrol must return DLGC_WANTTAB. To receive key events for the arrow keys, return DLGC_WANTARROWS. If you want to receive events for both the arrow keys and the TAB key, then bitwise OR DLGC_WANTARROWS with DLGC_WANTTAB and return that value. If you want to receive key events for all keys, then return DLGC_WANTALLKEYS.

The VCL contains some good examples of how to use the WM_GETDLGCODE message. TToolBar responds to WM_GETDLGCODE so it can receive events for the arrow keys, and TCustomGrid uses WM_GETDLGCODE to request both the arrow keys and the TAB key. TMediaPlayer and TDBNavigator use WM_GETDLGCODE to request the arrow keys. Here are some code snippets from the VCL that demonstrate how they respond to the WM_GETDLGCODE message. I have converted code from pascal to C++ for readability.

void __fastcall TToolBar::WMGetDlgCode(TMessage & Message)
{
    // only want the arrow keys in some situations
    if(FInMenuLoop)
        Message.Result = DLGC_WANTARROWS;
}

void __fastcall TCustomGrid::WMGetDlgCode(TMessage & Message)
{
    // always want the arrow keys
    Msg.Result := DLGC_WANTARROWS;
    ...

    // sometimes want the tab key
    if (Options.Contains(goTabs))
        Msg.Result |= DLGC_WANTTAB;

    // sometimes want WM_CHAR messages
    if (Options.Contains(goEditing))
        Msg.Result |= DLGC_WANTCHARS;
}

void __fastcall TMediaPlayer::WMGetDlgCode(TMessage &Message)
{
    Message.Result = DLGC_WANTARROWS;
}

Note: Don't create a WM_GETDLGCODE handler for your form. You must write the WM_GETDLGCODE handler at the control level.

Note: In VCL programs, the operating system is not actually the creator of the WM_GETDLGCODE message. The VCL synthesizes the message. Fortunately, this is an implementation detail that you can ignore. Simply write your WM_GETDLGCODE handler as if the OS was the creator of the message.
...
I hope, it helps. Alex

 
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:TropicalFish
ID: 6273560
Yes, I've read that before, but isn't that only for capturing tab in different components and not directly on a form? The note sais not to create a WM_GETDLGCODE handler for your form. This doesn't seem to work:

void __fastcall TForm::WMGetDlgCode(TMessage &Message)
{
   Message.Result = DLGC_WANTTAB;
}
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 100 total points
ID: 6281973
O, your form w/o controls. In such case you can use 1-st
link. Next is snippet:
....
The .h
   void __fastcall KeyDown(Word &Key, Classes::TShiftState Shift);
   void __fastcall CMWantSpecialKey(TCMWantSpecialKey &Message);

   BEGIN_MESSAGE_MAP
      MESSAGE_HANDLER(CM_WANTSPECIALKEY , TCMWantSpecialKey, CMWantSpecialKey)
   END_MESSAGE_MAP(TForm)// Assumes this is in a form derived from TForm


The .cpp
void __fastcall TForm1::KeyDown(Word &Key, Classes::TShiftState Shift)
{
   switch (Key)
   {
      case VK_UP:

         // do something
            break;

      case VK_DOWN:

         // do something
            break;


      case VK_LEFT:

         // do something
            break;

      case VK_RIGHT:

         // do something
            break;
   };

   TForm::KeyDown(Key,Shift);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::CMWantSpecialKey(TCMWantSpecialKey &Message)
{
   if(Message.CharCode == VK_TAB)
      TForm::Dispatch((void*)&Message);
   else Message.Result = 1;
}
....
0
 

Author Comment

by:TropicalFish
ID: 6282164
Great, thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
IdTelnet1DataAvailable handler freezes the Application 4 64
convert char array to number in c 5 83
c++ syntax question 9 44
What is sub-make ? 2 38
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now