We help IT Professionals succeed at work.

Tab with OnKeyDown and OnKeyUp

TropicalFish
TropicalFish asked
on
I have an empty form without any components on it. I use the OnKeyDown event to respond to keyboard events. It works for all keys I need it for but the Tab key.

I recently upgraded from BCB4 to BCB5. In BCB4 I used the following code, but it gives an error in BCB5. Anyways I couldn't catch the tab KeyUp with it, so how can I catch both those events in BCB5?

Thanks

//Code used in BCB4:

....
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)
{
     if (Key == VK_TAB)
       // do something

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

Author

Commented:
I've read that, but as the note on the bottom of that page sais, it doesn't work that way on empty forms without controls...
Sorry. Try the following comment : need to capture CM_DIALOGKEY message.

////////////////////////////////////////
// form1.h
////////////////////////////////////////


//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:     // IDE-managed Components
    void __fastcall FormKeyDown(TObject *Sender, WORD &Key,
          TShiftState Shift);
private:     // User declarations
public:          // User declarations
    __fastcall TForm1(TComponent* Owner);
protected:

   void __fastcall CMDialogKey( Messages::TWMKey &Message );

   BEGIN_MESSAGE_MAP
        MESSAGE_HANDLER( CM_DIALOGKEY, Messages::TWMKey, CMDialogKey )
   END_MESSAGE_MAP( TForm )
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

////////////////////////////////////////
// form1.cpp
////////////////////////////////////////

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key,
      TShiftState Shift)
{
    if (Key == VK_TAB)
        Caption = "Tab Key Down ....";
    else
        Caption = "Form1";

}
//---------------------------------------------------------------------------


void __fastcall TForm1::CMDialogKey( Messages::TWMKey &Message )
{
  if( Message.CharCode != VK_TAB )
    TForm::Dispatch( (void*)&Message );

}



Try to press "Tab" ==> caption wil change to "Tab Key Down ...."
press other key   ==> caption will come back to "Form1"

Author

Commented:
Great!! And it works for releasing the Tab key too!
Thanks a lot!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.