Re: assign function keys in Delphi

Posted by webmaster Guido on June 29, 2007

In Reply to function keys in delphi? posted by Willy P15898 on June 29, 2007

: I want to assign function keys to my Delphi program. For example, if I press F2, I want to change the name of a text file, if I press F4 I want to view the contents of a text file, and so on.
---------------------

You can use the OnKeyDown event of the form:

1. Select the form, and in the Object Inspector set its property KeyPreview to True. Now, all the key-presses firstly go to the form, instead of to any other component that happens to have the "focus".

2. In the Object Inspector's events list, double-click on the box next to OnKeyDown. Delphi will generate an empty "event handler" code block. Complete the code as follows:

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_F2 then
    ChangeName
  else if Key = VK_F4 then
    ShowContents 
  else if Key = ... then
    // and so on, for other function keys
 end;

3. Of course, you also got to write the code for the procedures routines ChangeName and ShowContents ;)

Related articles

       

Follow Ups