Recent

Author Topic: [SOLVED] Don't get how to set hotkeys  (Read 1317 times)

hcvoiron

  • New member
  • *
  • Posts: 8
[SOLVED] Don't get how to set hotkeys
« on: March 30, 2020, 07:25:48 pm »
Hi everyone, I'm "new" to Lazarus... i used to program pascal at a time it wasn't OO... And played with Delphi quite a long time ago, so i Consider myself as a new Noob ;)

I'm on windows 10, and so far, don't think my app will be used elsewhere. It's quite simple, i create text files in wich are stored Scores et Team fouls... Nothing new, but it fits my needs. With the buttons, i can go up and down the scores, so no trouble. I'd like to use Hotkeys (and if possible personnalized hotkeys).

I've checked at the TactionList and it dosen't fit because all the hotkeys are Shift+xxx or ctrl+xxx (but anyway, the real problem is not there, the real thing is that i don't know how to "click" the button with the actionlist)

I've also tried the shortcut property of my form. With this (it's quite dirty...) i've written someting like that :
     if (msg.CharCode=65) then begin
          Add 1 to score, update label.caption and write the file
     end;

But the problem is that the score is not score +1 but score +2... as if the button was clicked twice....
And I don't know how to use the button.click property to make it work as if it was clicked by the mouse...

I've searched the forum, but didn't see something clear/simple enough

My buttons are called B_Score_Local_Plus for example... and don't know if i can, or how i can use the B_Score_Local.Plus.click

Any help is welcome, and sorry if i missed a topic in this forum ;)

Regards
S.
« Last Edit: March 31, 2020, 05:26:47 pm by hcvoiron »

jamie

  • Hero Member
  • *****
  • Posts: 6128
Re: Don't get how to set hotkeys
« Reply #1 on: March 30, 2020, 10:38:32 pm »
You need to show a few lines of the suspected problem area..
The only true wisdom is knowing you know nothing

hcvoiron

  • New member
  • *
  • Posts: 8
Re: Don't get how to set hotkeys
« Reply #2 on: March 31, 2020, 01:05:26 am »
Thanks for your answer,

here are the lines...

The button that increases the score is made like this

procedure TOBS_HOCKEY_Control.B_Score_Local_PlusClick(Sender: TObject);
begin
  Score_Local := Score_Local +1; // I could have used "Inc" instead
  Affiche_Score_Local.Caption:= intToStr(Score_Local);
  Ecrire_Fichier('d:\Score_Local.txt',Affiche_Score_Local.Caption);
end;                                                       

The form shortcut used is this one :

procedure TOBS_HOCKEY_Control.FormShortCut(var Msg: TLMKey; var Handled: Boolean
  );
begin
     if (msg.CharCode=65) then begin
         B_Score_Local_Plus.Click ();
     end;
end;         

The problem is that when i press the 'a' key, the score is increased by 2 and not by one... 0 -> 2 -> 4 -> 6 etc.



lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Don't get how to set hotkeys
« Reply #3 on: March 31, 2020, 11:54:22 am »
Try this simple modification:

Code: Pascal  [Select][+][-]
  1. procedure TOBS_HOCKEY_Control.FormShortCut(var Msg: TLMKey; var Handled: Boolean
  2.   );
  3. begin
  4.      if (msg.CharCode=65) then begin
  5.          B_Score_Local_Plus.Click ();
  6.          Handled := True.
  7.      end;
  8. end;

BTW, you can do exactly the same with an action, which IMHO would be cleaner, or adding a handler for the form's OnKeyPress event and setting the form's KeyPreview to True:

Code: Pascal  [Select][+][-]
  1. { Using the OnKeyPress event }
  2. procedure TOBS_HOCKEY_Control.FormKeyPress(Sender: TObject; var Key: char);
  3. begin
  4.   if Key = 'a' then begin
  5.     B_Score_Local_Plus.Click ();
  6.     Key := #0;
  7.   end;
  8. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

hcvoiron

  • New member
  • *
  • Posts: 8
SOLVED - Don't get how to set hotkeys
« Reply #4 on: March 31, 2020, 04:31:02 pm »
Hi, Thanks very much for your answer... the first option works fine... So i'll try the second option that should be cleaner...
As there are several Hotkeys, maybe a "case..of" instead of multiple "if then" or a "if/then/else" would be better too ?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: SOLVED - Don't get how to set hotkeys
« Reply #5 on: March 31, 2020, 05:18:05 pm »
As there are several Hotkeys, maybe a "case..of" instead of multiple "if then" or a "if/then/else" would be better too ?

Yes, for several keys it's better (and cleaner) to use a case statement; indeed, that's one of the main reasons why the case statement exists, to replace long series of "if x then y else if a then b else ...".

It's also generally easier to optimize for the compiler, which might or might not matter much but is always a bonus :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018