Solved

Tab with OnKeyDown

Posted on 2001-07-08
6
459 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
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!

 

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

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

Suggested Solutions

Title # Comments Views Activity
TCP/IP Socket - connection close results in data lost 14 153
What is sub-make ? 2 88
HTTPSendRequest with WinINet delays on first call 11 84
why doesn't assert terminate here? 2 11
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 how to clear a vector as well as how to detect empty vectors in C++.

696 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