t e m p o r a l 
 d o o r w a y 

Getting Unusual Keys In Your Application

 

Introduction

When you need your component or form to capture and react to unusual keys like the arrow keys, you need what this article shows.

The reasons for all of this are not apparent, but it does work. The techniques were discussed in the newsgroups, and my thanks to the originators.

Note that this assumes a component based on TForm... If your component (or form) is based on another class, substitute that class name where you find TForm.

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;
}

Getting Tab and Shift Tab

You can use this same technique to get the tab key. Simply return Message.Result = 1 from CMWantSpecialKey for the VK_TAB and react to VK_TAB in the KeyDown function.

Copyright © 2004 by Mark Cashman (unless otherwise indicated), All Rights Reserved