37 Votes

Delphi: Function keys like F1, F2 and F3 do not work in FormKeyDown event

Question by Guest | Last update on 2021-07-01 | Created on 2012-08-10

In the FormKeyDown event, I want to react to the function keys (F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11 and F12) and assign an appropriate action (HotKey). For this I use the following code:

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
  if Key = VK_F1 then Function1();
  if Key = VK_F2 then Function2();
  if Key = VK_F3 then Function3();

Problem: For some reason, it does not work. Apparently, when pressing F1, not even the FormKeyDown event of the form is called! What am I doing wrong? I have checked the code for many times and with all other Virtual Keys such as VK_SPACE, it works perfectly! Only with the function keys there is a problem!

6Best Answer8 Votes

Your code can remain exactly as it is, just try to start the program outside of the IDE.

When you are running the program in Delphi, the function keys are assigned by Delphi to control the program and the debugging and therefore you can not access their key event via the OnKeyDown.

But if you start the program outside of Delphi, it should work.
Last update on 2021-07-01 | Created on 2012-08-12

ReplyPositive Negative
33 Votes

I had the same problem! And that is the solution! Thank you very very much!

Without your tipp, I could never get it!
2012-09-24 at 18:09

Positive Negative
00 Votes

With Delphi Sydney this is not an option: F1 is also ignored "outside".
2021-06-28 at 23:24

ReplyPositive Negative
22 Votes

Then it's probably something else.

For example, have you forgotten to set KeyPreview to true?
2021-06-29 at 11:21

Positive Negative

Related Topics

Delphi: System-Wide HotKey

Tutorial | 1 Comment

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.


Ask your own question or write your own article on askingbox.com. That’s how it’s done.