Recent

Author Topic: How to execute a procedure from the user pressing an F-Key, i.e. F4  (Read 884 times)

1HuntnMan

  • Sr. Member
  • ****
  • Posts: 445
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
I've been attempting to find an example of activating F4 like the user clicking a speedbutton.  I'm sure you use the ASCII table for this????

cdbc

  • Hero Member
  • *****
  • Posts: 2728
    • http://www.cdbc.dk
Re: How to execute a procedure from the user pressing an F-Key, i.e. F4
« Reply #1 on: March 06, 2026, 08:54:55 pm »
Hi
· TForm1.KeyPreview:= true; /// OI
· TForm1.FormKeyDown(); /// OI
· uses ...LCLType;
· in TForm1.FormKeyDown();
  begin
    case Key of
      VK_F4: {call some method here};
    end;
    { if need be -- Key:= 0;}
  end;

HTH
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

TBMan

  • Sr. Member
  • ****
  • Posts: 353
Re: How to execute a procedure from the user pressing an F-Key, i.e. F4
« Reply #2 on: March 06, 2026, 10:40:21 pm »
With the extended keys there are two codes if you are using "readkey" The first code is #0, the second the extended keycode. Below is an old style way of getting the extended keycode. This is in Windows, btw.

Code: Pascal  [Select][+][-]
  1. program testkey;
  2. uses crt;
  3.  
  4.  
  5. var
  6.   c:char;
  7.  
  8. begin
  9.      clrscr;
  10.      repeat
  11.            if keypressed then
  12.            begin
  13.                 c := readkey;
  14.                 if c = #0 then c := readkey; // extended code
  15.                 writeln(ord(c));
  16.            end;
  17.      until  c in ['q','Q'];
  18. end.                                    
  19.  
  20.  
  21.  
Barry

Newest game (clone),
Missile Commander:
https://www.youtube.com/watch?v=tgKz0cxog-k

mas steindorff

  • Hero Member
  • *****
  • Posts: 574
Re: How to execute a procedure from the user pressing an F-Key, i.e. F4
« Reply #3 on: March 07, 2026, 03:25:22 am »
are you, perchance, thinking of a "shortcut" field like what Tmenuitems support.  if you have the procedure you want to do in a menu (visible  or not) then you can setup a key to do the same thing as if you clicked it.
windows 10 &11, Ubuntu 21+ IDE 3.4 general releases

Aruna

  • Hero Member
  • *****
  • Posts: 795
Re: How to execute a procedure from the user pressing an F-Key, i.e. F4
« Reply #4 on: March 07, 2026, 03:34:41 am »
I've been attempting to find an example of activating F4 like the user clicking a speedbutton.  I'm sure you use the ASCII table for this????
I have attached a fully working zip tested on Linux. Screenshot shows what to expect.

n7800

  • Hero Member
  • *****
  • Posts: 683
  • Lazarus IDE contributor
    • GitLab profile
Re: How to execute a procedure from the user pressing an F-Key, i.e. F4
« Reply #5 on: March 07, 2026, 10:16:53 pm »
Shift


It's also important to check the "Shift" variable. A simple "Key" check will result in any keystroke being triggered: [F4], [Ctrl+F4], [Alt+Shift+F4]... Correctly consider valid flags from the Shift set:

Code: Pascal  [Select][+][-]
  1.   // [F4]
  2.   if (Key = VK_F4) and (Shift = []) then
  3.   begin
  4.     ...
  5.   end
  6.   // [Alt+Shift+F4]
  7.   else if (Key = VK_F4) and (Shift = [ssAlt, ssShift]) then
  8.   begin
  9.     ...
  10.   end;

Note that sometimes something like "(ssCtrl in Shift)" is used, but this is incorrect, as it doesn't check the Alt and Ctrl keys. Therefore, instead of the expression "(ssCtrl in Shift) and not (ssShift in Shift) and not (ssCtrl in Shift)", simply use "(Shift = [ssCtrl])", which means "only this flag". Usually, checking these modifiers is sufficient.*

* There's also a platform-dependent "ssMeta" (e.g. meaning the [Win] key on Windows).

In fact, there are nuances regarding which TShiftStateEnum modifiers can be used in events. For example, the above-mentioned modifiers are found in keyboard events, but they can never be "ssLeft, ssRight, ssMiddle, ssDouble, ...", as these flags are only checked in mouse events. Even if you use "(Shift = [ssLeft, ssCtrl])" in them, and a double-click occurs (i.e., the "ssDouble" flag is also present), the condition will not be triggered. I don't want to turn this comment into an article...
« Last Edit: March 07, 2026, 10:21:16 pm by n7800 »

n7800

  • Hero Member
  • *****
  • Posts: 683
  • Lazarus IDE contributor
    • GitLab profile
Re: How to execute a procedure from the user pressing an F-Key, i.e. F4
« Reply #6 on: March 07, 2026, 10:17:32 pm »
Key := 0


Also note that "Key" is an argument with the "var" modifier, allowing modification. This is almost always necessary to reset it ("Key:=0;") after processing, so that subsequent keypress handlers (either those of other components or the default handler) won't be called:

Code: Pascal  [Select][+][-]
  1.   if (Key = VK_F4) and (Shift = [ssAlt]) then
  2.   begin
  3.     ...
  4.     Key := 0; // key handled
  5.   end;

This is a very good example for this key, which demonstrates:
1. You can override the behavior of some standard "system" keys, such as [Alt+F4] for closing a window.
2. If you don't specify the key as processed, its processing will continue and reach the default window handler, causing the window to close ))

 

TinyPortal © 2005-2018