Board index » delphi » Making buttons respond to the up/down/left/right arrow keys

Making buttons respond to the up/down/left/right arrow keys

Folks,

I'm missing something here.  I have a program with a small cluster of
BitBtn components used to control a graphic overlay.  You can see what I
mean at:

http://www.david-taylor.pwp.blueyonder.co.uk/software/satsignal_at_wo...
m

What a user has asked for is that the cursor keys be available as an
alternative to the BitBtns to control the overlay (shown in yellow on the
screenshot).  OK, so I coded an OnKeyUp event handler for the form, and
while some components have the focus, it works as expected.  But if the
cluster of cursor keys has the focus, I get no response in the form's
event handler, nor do I get a response from an event handler for the
KeyDown or KeyUp events for the BitBtn components themselves.  Instead,
pressing an arrow key changes which BitBtn is selected (just like tabbing
between controls).

My question is: am I missing a simple property setting or something
somwhere (in which case, can you help?), or have I come across a
fundamental Windows way of working that is going to be difficult to
program round?

Delphi 5, testing on Win XP Pro.

Many thanks,
David
--
Software written to your requirements
Web:  http://www.satsignal.net
Email:  davidtay...@writeme.com

 

Re:Making buttons respond to the up/down/left/right arrow keys


Quote
In article <3d1ed95f_2@dnews>, David J Taylor wrote:
> I'm missing something here.  I have a program with a small cluster of
> BitBtn components used to control a graphic overlay.  You can see what I
> mean at:
> What a user has asked for is that the cursor keys be available as an
> alternative to the BitBtns to control the overlay (shown in yellow on the
> screenshot).  OK, so I coded an OnKeyUp event handler for the form, and
> while some components have the focus, it works as expected.  But if the
> cluster of cursor keys has the focus, I get no response in the form's
> event handler, nor do I get a response from an event handler for the
> KeyDown or KeyUp events for the BitBtn components themselves.  Instead,
> pressing an arrow key changes which BitBtn is selected (just like tabbing
> between controls).

> My question is: am I missing a simple property setting or something
> somwhere (in which case, can you help?), or have I come across a
> fundamental Windows way of working that is going to be difficult to
> program round?

The rule is this: the forms key preview handlers only see the keys the
control with focus wants to see. It gets asked before a key is delivered via
a WM_GETDLGCODE message. If it does not want to see arrow keys, tab and some
others these get send to the form in a CM_DIALOGKEY message. The form uses
them to implement navigation between controls, emulating what the API
function IsDialogMessage does for standard windows dialogs.

So the way to trap these messages is to add a handler for the CM_DIALOGKEY
message to the form.

  private
    // form declaration, private section
    Procedure CMDialogKey(Var Msg: TWMKey); message CM_DIALOGKEY;

Procedure TForm1.CMDialogKey(Var Msg: TWMKEY);
Begin
  Case Msg.Charcode Of
    VK_LEFT: blah;
    VK_RIGHT: ....;
  Else
    inherited;
  End;  
End;

To declare the key as handled set msg.result := 1. If you want the default
handling call the inherited method.

If you want to learn more about the convoluted way of key messages through
the VCL search the newsgroup archives for CM_WANTSPECIALKEY, CM_CHILDKEY,
CN_KEYDOWN in combination, that should turn up a lengthy post on the
subject, probably in several versions.
--
Peter Below (TeamB)  
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be

Re:Making buttons respond to the up/down/left/right arrow keys


"Peter Below (TeamB)" <100113.1...@compuXXserve.com> wrote in message
news:VA.00008dff.0187eb4c@antispam.compuserve.com...

Quote
> The rule is this: the forms key preview handlers only see the keys the
> control with focus wants to see. It gets asked before a key is delivered
via
> a WM_GETDLGCODE message. If it does not want to see arrow keys, tab and
some
> others these get send to the form in a CM_DIALOGKEY message. The form
uses
> them to implement navigation between controls, emulating what the API
> function IsDialogMessage does for standard windows dialogs.

Peter,

Many thanks for that - I will study it and apply it!

Cheers,
David

Other Threads