Solved

Tab with OnKeyDown

Posted on 2001-07-08
6
448 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

785 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