Recent

Author Topic: Linux intercept key when application is minimise (SOLVED)  (Read 398 times)

TheLastCayen

  • Jr. Member
  • **
  • Posts: 50
Linux intercept key when application is minimise (SOLVED)
« on: November 06, 2019, 10:38:54 pm »
Hi,

I am using :
 - Linux Mint Xfce 64 bit
 - Lazarus 2.0.6 64 bit
 - FPC 3.0.4

I am developing an application that should run in the background. I want to be able to start or stop a timer on that application when I push F5. When my application loses focus, The F5 key is not working anymore.

This is my code:

Code: Pascal  [Select]
  1. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  2.   );
  3. begin
  4.   begin
  5.     IF Key = VK_F5 then
  6.       begin
  7.         if Timer1.Enabled then
  8.           begin
  9.             Timer1.Enabled:=False;
  10.             ShowMessage('Timer is now Disable');
  11.           end
  12.         else
  13.           begin
  14.             Timer1.Enabled:=True;
  15.             ShowMessage('Timer is now Enable');
  16.           end
  17.       end;
  18.   end;
  19. end;
  20.  

Anyone know a way to intercept the F5 key when my application is minimized?



« Last Edit: November 07, 2019, 02:25:57 am by TheLastCayen »

winni

  • Hero Member
  • *****
  • Posts: 722
Re: Linux intercept key when application is minimise
« Reply #1 on: November 06, 2019, 11:40:54 pm »
Hi!

Not so easy. You have to look for "keyboard hook".

Here the search results from the forum:

https://forum.lazarus.freepascal.org/index.php?action=search2

Good luck!

Winni

TheLastCayen

  • Jr. Member
  • **
  • Posts: 50
Re: Linux intercept key when application is minimise
« Reply #2 on: November 07, 2019, 01:03:23 am »
Thank you for the quick reply.

I did a search and I found that post:
https://forum.lazarus.freepascal.org/index.php/topic,41307.msg286642.html#msg286642


Apparently you can do it using  Codebot.Input.hotkeys but I can't find it under my Lazarus component or any documentation on how to add it in my project.

I think this is the GitHub page for the project.
https://github.com/sysrpl/Cross.Codebot

I can't find more information about Codebot:(
I never try to add something like that to one of my projects. Can someone point me to documentation on how to add Codebot.Input.hotkeys to my project?


TheLastCayen

  • Jr. Member
  • **
  • Posts: 50
Re: Linux intercept key when application is minimise
« Reply #3 on: November 07, 2019, 02:25:14 am »
I found how to deal with Cross.Codebot.

for future reference:

add to your project ../Cross.Codebot-master/source/codebotctrls.lpk

This is my code:
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls,
  9.   LCLType, ExtCtrls, Codebot.Input.Hotkeys;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Label1: TLabel;
  17.     LabeledEdit1: TLabeledEdit;
  18.     Timer1: TTimer;
  19.     procedure FormCreate(Sender: TObject);
  20.     procedure FormDestroy(Sender: TObject);
  21.     procedure Timer1StartTimer(Sender: TObject);
  22.     procedure Timer1Timer(Sender: TObject);
  23.     procedure KeyPressed(Sender: TObject; Key: Word; Shift: TShiftState);
  24.   private
  25.  
  26.   public
  27.  
  28.   end;
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34. {$R *.lfm}
  35.  
  36. { TForm1 }
  37. Const
  38.   verbose = True;
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. begin
  42.   HotkeyCapture.RegisterNotify(VK_F5,[],@KeyPressed);
  43. end;
  44.  
  45. procedure TForm1.FormDestroy(Sender: TObject);
  46. begin
  47.   HotkeyCapture.UnRegisterNotify(VK_F5,[]);
  48. end;
  49.  
  50.  
  51. procedure TForm1.Timer1StartTimer(Sender: TObject);
  52. begin
  53.    Timer1.Interval:=strtoint(LabeledEdit1.Text);
  54. end;
  55.  
  56. procedure TForm1.Timer1Timer(Sender: TObject);
  57. begin
  58.     if verbose then ShowMessage('1 Timer cycle')
  59. end;
  60.  
  61. procedure TForm1.KeyPressed(Sender: TObject; Key: Word; Shift: TShiftState);
  62. begin
  63.   if Timer1.Enabled then
  64.     begin
  65.       Timer1.Enabled:=False;
  66.       if verbose then ShowMessage('Timer is now Disable');
  67.     end
  68.   else
  69.     begin
  70.       if verbose then ShowMessage('Timer is now Enable');
  71.       Timer1.Enabled:=True;
  72.     end
  73. end;
  74.  
  75. end.
  76.                                                                                  
  77.