Recent

Author Topic: [SOLVED] Hotkey repeat procedure  (Read 3043 times)

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
[SOLVED] Hotkey repeat procedure
« on: August 22, 2017, 12:32:11 pm »
Anyone have a SIMPLE example of how to repeat a procedure if a hotkey is pressed?

Something like Word or Excel's F4.

The procedure would vary, so assigning a normal ShortCut key is not the solution I am after.

I guess the answer is something along the lines that when a procedure is called it is registered in a global variable or similar, and if the hot key is pressed then somehow that variable defines which procedure to call.  :D

Thanks

Bazza
« Last Edit: September 09, 2017, 06:52:19 am by Bazzao »
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Hotkey repeat procedure
« Reply #1 on: August 22, 2017, 01:30:20 pm »
Yes. You can declare your global var and methods like this:
Code: Pascal  [Select][+][-]
  1.   private
  2.     MyProc: procedure of object;
  3.   public
  4.     procedure My1;
  5.     procedure My2;
  6.  
assign it:
Code: Pascal  [Select][+][-]
  1. MyProc:=@My1;
and then call it:
Code: Pascal  [Select][+][-]
  1. begin
  2.   MyProc;
  3. end;

But all methods (My1, ...) must have the same list of parameters.

You can also use Actions and ActionList.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Hotkey repeat procedure
« Reply #2 on: August 22, 2017, 10:42:28 pm »
Thanks Blaazen,

It works ...  :)

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ActnList,
  9.   ExtCtrls, StdCtrls, LCLType;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     My2Button1: TButton;
  17.     My1Button1: TButton;
  18.     Memo1: TMemo;
  19.     Panel1: TPanel;
  20.     Panel2: TPanel;
  21.     procedure FormCreate(Sender: TObject);
  22.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  23.     procedure My1Button1Click(Sender: TObject);
  24.     procedure My2Button1Click(Sender: TObject);
  25.   private
  26.     { private declarations }
  27.     MyProc: procedure of object;
  28.   public
  29.     { public declarations }
  30.     procedure My0;
  31.     procedure My1;
  32.     procedure My2;
  33.   end;
  34.  
  35.  
  36. var
  37.   Form1: TForm1;
  38.  
  39. implementation
  40.  
  41. {$R *.lfm}
  42.  
  43. { TForm1 }
  44.  
  45. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  46.   );
  47. begin
  48.   if Key=VK_F2 then
  49.     ShowMessage('F2.')
  50.   else if Key=VK_F4 then
  51.     if (MyProc<>@My0) then
  52.       MyProc;
  53. end;
  54.  
  55. procedure TForm1.My1Button1Click(Sender: TObject);
  56. begin
  57.   My1;
  58. end;
  59.  
  60. procedure TForm1.My2Button1Click(Sender: TObject);
  61. begin
  62.   My2;
  63. end;
  64.  
  65. procedure TForm1.My0;
  66. begin
  67.   ShowMessage('Should never be called.');
  68. end;
  69.  
  70. procedure TForm1.My1;
  71. begin
  72.   ShowMessage('My1');
  73.   MyProc:=@My1;
  74. end;
  75.  
  76. procedure TForm1.My2;
  77. begin
  78.   ShowMessage('My2');
  79.   MyProc:=@My2;
  80. end;
  81.  
  82. procedure TForm1.FormCreate(Sender: TObject);
  83. begin
  84.   Form1.KeyPreview:=True;
  85.   MyProc:=@My0;
  86. end;
  87.  
  88. end.
  89.  

I don't know how to initialise MyProc with a null value, so I had to create a My0 and test for that. What is a null value for a procedure of object?

The other thing is that I get an annoying warning "Shift is not used". I could put another test in KeyDown or disable that message (I believe there is an option somewhere), the latter I don't want to do. Any other suggestions?

Bazza
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

jamie

  • Hero Member
  • *****
  • Posts: 7701
Re: Hotkey repeat procedure
« Reply #3 on: August 23, 2017, 01:49:38 am »
You could of done that using a Case statement..

Case My_Index_Switch_For_The_Hotkey of
 1:CallProc1(parms......);
 2:CallProc2(parms.....);
 3:CallProc3(and more parms or none, its your choice);
End;

 The Index will be set somewhere that you define so that Hot key will
execute the one you want in the case list...
 Using Case you can selectively use different parameters etc..
The only true wisdom is knowing you know nothing

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Hotkey repeat procedure
« Reply #4 on: August 23, 2017, 02:05:11 am »
I don't know how to initialise MyProc with a null value, so I had to create a My0 and test for that. What is a null value for a procedure of object?

Assign nil to Myproc, e.g:
Code: Pascal  [Select][+][-]
  1. MyProc := nil;

That way you could use something like if assigned MyProc then MyProc construction;

Bazzao

  • Full Member
  • ***
  • Posts: 178
  • Pies are squared.
Re: Hotkey repeat procedure
« Reply #5 on: August 23, 2017, 09:31:20 am »
Thanks Molly.

B
Bazza

Lazarus 2.0.10; FPC 3.2.0; SVN Revision 63526; x86_64-win64-win32/win64
Windows 10.

 

TinyPortal © 2005-2018