Recent

Author Topic: how to detect what has fired a popupmenu when shortcut key used?  (Read 8076 times)

Josh

  • Hero Member
  • *****
  • Posts: 1274
Hi

I have been using the same popup menu on various grids,

I can use the Fired_From_Component := PopupMenu1.PopupComponent.Name; in the onpopup event, but this is not fired on a shortcut key.
Using the popupcomponent.name within the menuitem event; when using shortcut keys contains the value of the last popup menu activated by mouse.

is there a similar event available for when shortcut keys are used to invoke a menuitem.

The best way to get accurate information on the forum is to post something wrong and wait for corrections.

sky_khan

  • Guest
Re: how to detect what has fired a popupmenu when shortcut key used?
« Reply #1 on: June 05, 2017, 11:41:24 am »
I'm not sure that I understand the problem but I guess you can probably use form's ActiveControl property. It gives you which control has focus at the moment. This way it doesnt matter if popup activated by keyboard or mouse.

Thaddy

  • Hero Member
  • *****
  • Posts: 14392
  • Sensorship about opinions does not belong here.
Re: how to detect what has fired a popupmenu when shortcut key used?
« Reply #2 on: June 05, 2017, 11:56:36 am »
Hi

I have been using the same popup menu on various grids,

I can use the Fired_From_Component := PopupMenu1.PopupComponent.Name; in the onpopup event, but this is not fired on a shortcut key.
Using the popupcomponent.name within the menuitem event; when using shortcut keys contains the value of the last popup menu activated by mouse.

is there a similar event available for when shortcut keys are used to invoke a menuitem.
OnKeyDown, OnKeyUp for the parent form give you both key and shiftstate, etc. Other option: OnMessage hook for TApplication. TShortcut itself is just a word, not a class, alas.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  2.   );
  3. begin
  4.   Caption :=Format('key %d, ShiftState %d',[Key,integer(Shift)]);
  5.   inherited;
  6. end;
You will see that the item on the menu still works and receives focus.
« Last Edit: June 05, 2017, 12:16:31 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Josh

  • Hero Member
  • *****
  • Posts: 1274
Re: how to detect what has fired a popupmenu when shortcut key used?
« Reply #3 on: June 05, 2017, 01:16:23 pm »
Hi

I should have added, so I have also added in a simple example project to show the issue ( created with latest SVN).

i use the onpopuop event to enable disable hide controls that are non appropriate depending on what has been set in other parts of the program, as the onpopup is not called when a shortcut is used, it is possible to activate shortcuts that would not be available if you were to use the mouse to activate the menu.

In the example project, if you right click in memo1, the option Explode is available, and triggers from menu or shortcut, if you move into memo 2 and press ctrl-e the it is still triggered, if you bring up menu in memo 2, then the ctrl-e is not enabled and also will not trigger if pressed within memo2.

I suppose a quick fix would be to calll a derivative of the onpopup event; when ever I enter any component that has a popupmenu so as to set available options and also fire a similar routine when ever any text is manipulated or highlighted so as to display/set the relevant options on the menu; which then makes the shortcuts fire correctly.

Josh
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

sky_khan

  • Guest
Re: how to detect what has fired a popupmenu when shortcut key used?
« Reply #4 on: June 05, 2017, 06:25:10 pm »
I believe I already pointed you to right direction but you did not listen.
Anyways, check this if it is what you want.

Josh

  • Hero Member
  • *****
  • Posts: 1274
Re: how to detect what has fired a popupmenu when shortcut key used?
« Reply #5 on: June 05, 2017, 08:12:21 pm »
Hi SkyKhan,

Thanks for your reply.

I opened your zip and it contains lfm and pas file, no project info?

How can I open this to test?

Thanks
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

sky_khan

  • Guest
Re: how to detect what has fired a popupmenu when shortcut key used?
« Reply #6 on: June 05, 2017, 08:20:49 pm »
I just modified your example's main form and put them in a zip. Replace yours with mine (extract my zip into your example project folder) . Backup your example project folder first if you wish.
« Last Edit: June 05, 2017, 08:26:09 pm by SkyKhan »

Thaddy

  • Hero Member
  • *****
  • Posts: 14392
  • Sensorship about opinions does not belong here.
Re: how to detect what has fired a popupmenu when shortcut key used?
« Reply #7 on: June 05, 2017, 08:38:05 pm »
I just modified your example's main form and put them in a zip. Replace yours with mine (extract my zip into your example project folder) . Backup your example project folder first if you wish.
But it is a..<not!>. generic solution.  Half of it requires.... Oh well.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: how to detect what has fired a popupmenu when shortcut key used?
« Reply #8 on: June 06, 2017, 09:11:49 am »
Hi

I have been using the same popup menu on various grids,

I can use the Fired_From_Component := PopupMenu1.PopupComponent.Name; in the onpopup event, but this is not fired on a shortcut key.
Using the popupcomponent.name within the menuitem event; when using shortcut keys contains the value of the last popup menu activated by mouse.

is there a similar event available for when shortcut keys are used to invoke a menuitem.
Use Tactionlist.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Josh

  • Hero Member
  • *****
  • Posts: 1274
Re: how to detect what has fired a popupmenu when shortcut key used?
« Reply #9 on: June 06, 2017, 12:18:08 pm »
Hi SkyKhan,

Your solution does indeed work. :)

As my onpopup event in my application is rather bloated, ie popup and shortcuts are created dynamically each time a onpopup is called up, a menu could be 4 items or over 20.

The issue I had only occured when I started adding in shortcut keys, and became aware that the popupmenu shortcut keys can stay active when in a control that should not be active.

So was after a way force a onpopup when shortcuts are used.

I have played around with grabbing the ctrl key in form events and then firing the onpopup event, this also seems to be working, without to much change of applicatioin and no additional components or controls.

ie, this is rough and needs fine tuning etc.

Code: Pascal  [Select][+][-]
  1.  
  2. var last_fired_from:string=''; // this is used to stop auto-repeat of ctrl firing the onpopup routine continuously
  3.  
  4. procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
  5.   );
  6. begin
  7.   if Shift = [ssCtrl] then
  8.   begin
  9.     // now check if active control has popupmenu
  10.     try
  11.       if last_fired_from<>activecontrol.name then
  12.       begin
  13.         last_fired_from:=activecontrol.name;
  14.         if assigned(activecontrol.PopupMenu) then
  15.         begin
  16.           if (findcomponent(activecontrol.PopupMenu.Name) as tpopupmenu).OnPopup<>nil then
  17.           begin
  18.             (findcomponent(activecontrol.PopupMenu.Name) as tpopupmenu).PopupComponent:=activecontrol;  // sets popupcpomponent to activecontrol.
  19.             (findcomponent(activecontrol.PopupMenu.Name) as tpopupmenu).OnPopup(activecontrol); // fire the onpoup event
  20.           end;
  21.         end;
  22.       end;
  23.     finally
  24.     end;
  25.   end
  26.   else
  27.   begin
  28.    // ctrl not pressed so reset
  29.     last_fired_from:='';
  30.   end;
  31. end;
  32.  
  33.  
  34. procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
  35. begin
  36.   last_fired_from:='';  / reset this so ctrl key can still fire the onpopup if ctrl released and pressed again in same control
  37. end;                      
  38.  
  39.  
« Last Edit: June 06, 2017, 12:25:50 pm by josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

sky_khan

  • Guest
Re: how to detect what has fired a popupmenu when shortcut key used?
« Reply #10 on: June 06, 2017, 12:41:07 pm »
Nice to know you now have a solution . I knew you need to use activecontrol after all :)

 

TinyPortal © 2005-2018