Recent

Author Topic: [SOLVED]How to make a shortcut key for Action work only on Memo1  (Read 3522 times)

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Hi, everybody, I have a question about Action, there are Memo1 and Memo2 on the form. I just want the shortcut key of Action to be valid in Memo1 and invalid in other places. How can I achieve it?

Test Code:
Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     Memo1: TMemo;
  3.     Memo2: TMemo;
  4.     procedure FormCreate(Sender: TObject);
  5.   private
  6.     Action1: TAction;
  7.     ActionList1: TActionList;
  8.     procedure Action1Execute(Sender: TObject);
  9.   end;
  10.  
  11. uses
  12.   LCLProc;
  13.  
  14. procedure TForm1.FormCreate(Sender: TObject);
  15. begin
  16.   ActionList1 := TActionList.Create(self);
  17.   Action1 := TAction.Create(self);
  18.   with Action1 do
  19.   begin
  20.     ActionList := ActionList1;
  21.     ShortCut := TextToShortCut('Ctrl+A');
  22.     OnExecute := @Action1Execute;
  23.   end;
  24. end;
  25.  
  26. procedure TForm1.Action1Execute(Sender: TObject);
  27. begin
  28.   ShowMessage('Action For Memo1');
  29. end;
« Last Edit: October 28, 2017, 04:55:33 pm by tomitomy »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to make a shortcut key for Action work only on Memo1
« Reply #1 on: October 27, 2017, 10:07:25 am »
You can design your action's OnExecute to be as specific as you want.
e.g.
Code: Pascal  [Select][+][-]
  1.   TForm1 = class(TForm)
  2.     Memo1: TMemo;
  3.     Memo2: TMemo;
  4.     procedure FormCreate(Sender: TObject);
  5.   private
  6.     ActnList: TActionList;
  7.     Memo1Action: TAction;
  8.     procedure Memo1ActionExecute(Sender: TObject);
  9.   end;
  10.  
  11. var
  12.   Form1: TForm1;
  13.  
  14. implementation
  15.  
  16. procedure TForm1.FormCreate(Sender: TObject);
  17. begin
  18.   ActnList:=TActionList.Create(Self);
  19.   Memo1Action:=TAction.Create(ActnList);
  20.   with Memo1Action do begin
  21.     ActionList:=ActnList;
  22.     ShortCut:=TextToShortCut('Ctrl+A');
  23.     OnExecute:=@Memo1ActionExecute;
  24.   end;
  25. end;
  26.  
  27. procedure TForm1.Memo1ActionExecute(Sender: TObject);
  28. begin
  29.   Memo1.Color:=RGBToColor(Random(256), Random(256), Random(256));
  30. end;

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: How to make a shortcut key for Action work only on Memo1
« Reply #2 on: October 27, 2017, 10:52:58 am »
You can design your action's OnExecute to be as specific as you want.

Thank you howardpc, but in this case, Memo2's shortcut Ctrl+A will not work properly, unable to perform "select all" operation. I hope Action's shortcut only affect Memo1.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to make a shortcut key for Action work only on Memo1
« Reply #3 on: October 27, 2017, 11:25:04 am »
Thank you howardpc, but in this case, Memo2's shortcut Ctrl+A will not work properly, unable to perform "select all" operation. I hope Action's shortcut only affect Memo1.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. begin
  3.   ActnList:=TActionList.Create(Self);
  4.   Memo1Action:=TAction.Create(ActnList);
  5.   with Memo1Action do begin
  6.     ActionList:=ActnList;
  7.     ShortCut:=TextToShortCut('Ctrl+A');
  8.     OnExecute:=@Memo1ActionExecute;
  9.     OnUpdate := @Action1Update;
  10.   end;
  11. end;
  12.  
  13. procedure form1.Action1Update(Sender: TObject);
  14. begin
  15.   Action1.enabled := activecontrol = TMemo1;
  16. end;
  17.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to make a shortcut key for Action work only on Memo1
« Reply #4 on: October 27, 2017, 11:29:31 am »
OnUpdate gives you limited customisation. But AFAIK it does not let you redfine the action for specific controls, so that (say) Ctrl-A has different effects in Memo1 and Memo2. Disabling Ctrl-A for Memo2 is rather different from giving Ctrl-A a new effect in Memo2 compared to Memo1.

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: How to make a shortcut key for Action work only on Memo1
« Reply #5 on: October 27, 2017, 12:09:47 pm »
Thank you taazz. I tested your code. It's exactly what I need. Thank you very much! :)


OnUpdate gives you limited customisation. But AFAIK it does not let you redfine the action for specific controls, so that (say) Ctrl-A has different effects in Memo1 and Memo2. Disabling Ctrl-A for Memo2 is rather different from giving Ctrl-A a new effect in Memo2 compared to Memo1.

I'm sorry howardpc, my English is poor, I can not understand the content of your post, Google translation results I can not understand also. But thank you for your help. :)

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: [SOLVED]How to make a shortcut key for Action work only on Memo1
« Reply #6 on: October 29, 2017, 02:59:21 am »
what he means is this, having CTRL-A perform different duties between Memo1 and Memo2 is difficult.

But disabling them is easier.
 
I believe that was his meaning.
The only true wisdom is knowing you know nothing

tomitomy

  • Sr. Member
  • ****
  • Posts: 251
Re: [SOLVED]How to make a shortcut key for Action work only on Memo1
« Reply #7 on: October 29, 2017, 03:10:28 am »
what he means is this, having CTRL-A perform different duties between Memo1 and Memo2 is difficult.

But disabling them is easier.
 
I believe that was his meaning.

Thank you, jamie, I understand.

 

TinyPortal © 2005-2018