Application Getting Focus lazarus / 31-LCL ---------------------------------------------------------------------- sfeinst · 2011-07-12 00:01 I need to determine when my application gets the focus from another application.  In general, what I am trying to do, is determine if I need to enable a Paste menu.  Since a user could go into another app (say text editor) and copy text to the clipboard and then return to my app, how do I do this?  I tried OnActivate of the form, but that seems to only get fired at the beginning of the app when loading.  I tried OnEnter of a tmemo, but if the memo had focus when the user clicked to another app, this even does not fire, since it still has focus when returning to the app. Any suggestions? ---------------------------------------------------------------------- felipemdc · 2011-07-19 07:41 TForm.OnActivate is only for focus change between forms of the same program. For focus change between applications the event Application.OnActivate is called and I just tested that it works fine in linux-qt Code: [Select]procedure TForm1.FormActivate(Sender: TObject); // Not connected to TForm.OnActivate begin   Caption := 'OnActive ' + IntToStr(Counter);   Inc(Counter); end; procedure TForm1.FormCreate(Sender: TObject); // Connected to TForm.OnCreate begin   Application.OnActivate:=@FormActivate; end; See also: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpU pdate2/EN/html/delphivclwin32/Forms_TCustomForm_OnActivate.html I documented this also in our docs, but it will probably take a lot of time before the online docs are updated. Link for future use: http://lazarus- ccr.sourceforge.net/docs/lcl/forms/tcustomform.onactivate.html ---------------------------------------------------------------------- sfeinst · 2011-07-19 19:30 Thanks.  That got me most of the way there.  It definitely fires which is what I wanted, but for some reason, the control with focus is not cosidered to have focus when that event fires, so manually keeping track of control with focus (because the focused control affects what is enabled/disabled). Thanks. ---------------------------------------------------------------------- Mike.Cornflake · 2011-07-24 17:59 You could try a different tack - put the menu enabling code in the OnPopup of your menu...   That way it'll work even if the user puts something in the clipboard from elsewhere in your application. ---------------------------------------------------------------------- avra · 2011-08-04 08:39 Take a look at the solution here: http://www.lazarus.freepascal.org/index.php/topic,13958.0.html ---------------------------------------------------------------------- Elmug · 2011-08-05 10:57 Just a simpleton view here: If the user leaves your application, what is there to say that he has copied elsewhere and wishes to come back to your application to paste it? Perhaps the user just went to do whatever, not necessarily copy something? ---------------------------------------------------------------------- Avishai · 2011-08-05 11:14 Would TForm.ActiveControl.Name give you what you need? ---------------------------------------------------------------------- sfeinst · 2011-08-05 17:50 Quote from: Elmug on August 05, 2011, 12:57:49 pmJust a simpleton view here: If the user leaves your application, what is there to say that he has copied elsewhere and wishes to come back to your application to paste it? Perhaps the user just went to do whatever, not necessarily copy something? That is true.  That is why on return to my app I check the clipboard to see if there is anything that can be pasted.  If so, I enable the correct options. ---------------------------------------------------------------------- sfeinst · 2011-08-05 17:55 Quote from: Avishai on August 05, 2011, 01:14:33 pmWould TForm.ActiveControl.Name give you what you need? Thank you.  That is what I was looking for.  I had been using Control.Focused.  Form.ActiveControl makes more sense. ---------------------------------------------------------------------- Avishai · 2011-08-05 18:02 This might be more usefull case Form1.ActiveControl.TabOrder of   0: begin ...end;   1: begin ...end;   ... end;