Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Always react on FormKeyDown...

Status
Not open for further replies.

VuurSnikkel

Programmer
Sep 8, 2003
16
NL
Hi there ppl...


I'm having trouble getting my application to always react on a FormKeyDown-event. Let's say I've got some form and it contains an OK- and a Cancel-button. I want the form to perform the OKClick-function when I press Enter and the CancelClick-function when I press Escape. The logical way to do it (as far as I know) would be the following:

Procedure TSomeForm.FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
Begin
Case Key Of
VK_ESCAPE: CancelButtonClick(Self);
VK_RETURN: OKButtonClick(Self);
End;
End;

The problem is that when the form contains some random elements (buttons, text boxes etc.) the form will not respond to the FormKeyDown but the keyhandler of the component that currently has focus will kick in.

I COULD put a KeyDown-event on each of the components with the above code to catch Enter- and Escape-presses, but that's probably not the way to go.

So is there some way in which I can automatically bypass the keyhandler for the components and give primacy to the form's keyhandler? Help would be greatly appreciated.


GRTZ and thanx in advance,

VuurSnikkel.
 
The usual way to do what you want is to:

set the Cancel property of your CancelButton to true

set the Default property of your OKButton to true

then there is no need to write a KeyDown handler at all.


Andrew
 
Thanks a lot. I'm going to try it right now.


GRTZ,

VuurSnikkel.
 
another way to do this, is doing a TAction instance and setting the shortcuts key to Esc and the other to Enter key

--- LastCyborg ---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top