Board index » delphi » Tab key in KeyDown event?

Tab key in KeyDown event?

I wonder if anyone knows the answer to this.  If you try the following
code, you'll see that all keys call KeyDown and KeyUp, except the Tab key,
which only calls KeyUp.  Why is this?  I really need to trap the Tab key in
the KeyDown event.  Any ideas?

procedure TForm1.Memo1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  Memo1.SelText := 'KeyDown: ' + IntToStr(Key) + #13#10;
end;

procedure TForm1.Memo1KeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  Memo1.SelText := '  KeyUp: ' + IntToStr(Key) + #13#10;
end;

 

Re:Tab key in KeyDown event?


Quote
k...@nesbitt.com (Kenn Nesbitt) wrote:
>I wonder if anyone knows the answer to this.  If you try the following
>code, you'll see that all keys call KeyDown and KeyUp, except the Tab key,
>which only calls KeyUp.  Why is this?  I really need to trap the Tab key in
>the KeyDown event.  Any ideas?

I ran into the same thing with a TOutline.  I ended up hacking the
source code.  I think it is a windows related thing.

------------------------------------------------
R. Jeffery Bogatay  (ru...@ic.net)  (74654,3321)

Re:Tab key in KeyDown event?


In article <40p2j1$...@condor.ic.net>, ru...@ic.net says...

Quote

>k...@nesbitt.com (Kenn Nesbitt) wrote:

>>I wonder if anyone knows the answer to this.  If you try the following
>>code, you'll see that all keys call KeyDown and KeyUp, except the Tab key,
>>which only calls KeyUp.  Why is this?  I really need to trap the Tab key in
>>the KeyDown event.  Any ideas?

>I ran into the same thing with a TOutline.  I ended up hacking the
>source code.  I think it is a windows related thing.

>------------------------------------------------
>R. Jeffery Bogatay  (ru...@ic.net)  (74654,3321)

The only way I have been able to trap this, was to have only one control        
on the active form.  Then the TAB key shows up, otherwise Windows grabs it.

But, this is Delphi!  Run the message tracker, and Identify what windows
message is being generated for the TAB key.  Then install your own message
handler function for that form for that message.  It should work.

Re:Tab key in KeyDown event?


Quote
k...@nesbitt.com (Kenn Nesbitt) wrote:
>I wonder if anyone knows the answer to this.  If you try the following
>code, you'll see that all keys call KeyDown and KeyUp, except the Tab key,
>which only calls KeyUp.  Why is this?  I really need to trap the Tab key in
>the KeyDown event.  Any ideas?

You may need to trap the key at a higher level such as
Application.onMessage. For example, write your own OnMessage handler
as follows:

procedure TForm1.myFon(var Msg: TMsg; var Handled: Boolean);
begin
    if (Msg.Hwnd=Memo1.handle) and (Msg.Message=WM_KEYDOWN) then
   case Msg.wParam of
    VK_TAB{,other keys, if any, such as VK_UP}:
        begin
          {your code here}
          {to bypass the tab key, add "Handle:=true;" here}
        end
    end;
end;

procedure TForm1.Create(Sender: Tobject);
begin
    Application.OnMessage:=myFon;
    {...}
end;..

N.B. The above code is modified from one of my Delphi programs and has
not been tested.

Regards.

Re:Tab key in KeyDown event?


You can trap any key for any form or any specific component, like this:

Attach a popup menu to the form or component.
Disable the "AutoPopUp" property, wherever it is.
Put a command and description in the menu, something like
        Caption: TAB Key
        Name: ItemTab
In your form's FormCreate event handler, put something like this:
        ItemTab.ShortCut := ShortCut(vk_Tab,[]);
Now, whenever the component or form containing this invisible menu
has the focus, the Tab key will be trapped _first_ by the menu,
and the ItemTab event will be triggered.

Is this in a FAQ somewhere?  (Timo, where are you?)
It's cute, but I had to work it out all by myself.  (>SOB<)

: Eric :

--
------------------------------------------------------------------------------
I can face anything, except the future,  |  Eric the Fruitbat, aka Paul Sleigh
and certain parts of the past            |  In the SCA: Eric of Tobar Mhuire
and present.      -- Ashleigh Brilliant  |  fruit...@canberra.DIALix.oz.au

Other Threads