Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Function keys onFormKeyDown (https://www.delphipraxis.net/176507-function-keys-onformkeydown.html)

question 11. Sep 2013 11:48

Function keys onFormKeyDown
 
Hi,
I would like to use the function key (F1) in my application. I was trying several times to make it work in the following way
Code:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
 if Key = VK_F1 then
    //do something here
it was not working then i fount that, if i make the "KeyPreview := True" then then it works
though i have read about it from several sites, neverthless i am not totally clear and therefore i need a suggestion, is that okay if i make "KeyPreview := True" or it can be affected
other event handlers? and should i need to make the Key := 0 at the end? like the follwoing way
Code:
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
 if Key = VK_F1 then
    //do something here
Key := 0 // i mean here should i make it Key is zero

Der schöne Günther 11. Sep 2013 11:58

AW: Function keys onFormKeyDown
 
I've never tried it myself, but this fine gentleman on SO reports that

Zitat:

The KeyPreview property of the current active form is checked for the KeyUp-, KeyDown- and KeyPress- event handlers of the current active control. I.e.: a key press in any control results in the check of the form's KeyPreview property.

If that property is True, the event handler in question invokes the event handler of the form prior to that of itself. If the form's event handler does not change the key value to 0 (or #0, depending on KeyPress or KeyDown/KeyUp), then the active control's event handler takes back over, otherwise the event is considered handled.
So it's pretty much like gestures work in Delphi: The Event in your form fires first, no matter which component is currently selected. If you assign
Delphi-Quellcode:
Key := 0
in your
Delphi-Quellcode:
TForm1.FormKeyDown
, then the component that is currently selected won't even notice a key way pressed. If you want it do to some additional stuff, then don't zero it out :-)

So KeyPreview itself does not affect other handlers, while setting Key to zero does.


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:20 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz