?
Solved

Tab with OnKeyDown

Posted on 2001-07-08
6
Medium Priority
?
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: 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 400 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

752 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