Recent

Author Topic: Application Getting Focus  (Read 9012 times)

sfeinst

  • Full Member
  • ***
  • Posts: 244
Application Getting Focus
« on: July 12, 2011, 02:01:49 am »
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

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Application Getting Focus
« Reply #1 on: July 19, 2011, 09:41:08 am »
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/HelpUpdate2/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
« Last Edit: July 19, 2011, 09:53:51 am by felipemdc »

sfeinst

  • Full Member
  • ***
  • Posts: 244
Re: Application Getting Focus [solved]
« Reply #2 on: July 19, 2011, 09:30:40 pm »
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

  • Hero Member
  • *****
  • Posts: 1263
Re: Application Getting Focus
« Reply #3 on: July 24, 2011, 07:59:41 pm »
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.
Lazarus Trunk/FPC Trunk on Windows [7, 10]
  Have you tried searching this forum or the wiki?:   http://wiki.lazarus.freepascal.org/Alternative_Main_Page
  BOOKS! (Free and otherwise): http://wiki.lazarus.freepascal.org/Pascal_and_Lazarus_Books_and_Magazines

avra

  • Hero Member
  • *****
  • Posts: 2547
    • Additional info
Re: Application Getting Focus
« Reply #4 on: August 04, 2011, 10:39:01 am »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Elmug

  • Hero Member
  • *****
  • Posts: 849
Re: Application Getting Focus
« Reply #5 on: August 05, 2011, 12:57:49 pm »
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

  • Hero Member
  • *****
  • Posts: 1021
Re: Application Getting Focus
« Reply #6 on: August 05, 2011, 01:14:33 pm »
Would TForm.ActiveControl.Name give you what you need?
Lazarus Trunk / fpc 2.6.2 / Win32

sfeinst

  • Full Member
  • ***
  • Posts: 244
Re: Application Getting Focus
« Reply #7 on: August 05, 2011, 07:50:29 pm »
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? %)
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

  • Full Member
  • ***
  • Posts: 244
Re: Application Getting Focus
« Reply #8 on: August 05, 2011, 07:55:53 pm »
Would 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

  • Hero Member
  • *****
  • Posts: 1021
Re: Application Getting Focus
« Reply #9 on: August 05, 2011, 08:02:22 pm »
This might be more usefull

case Form1.ActiveControl.TabOrder of
  0: begin ...end;
  1: begin ...end;
  ...
end;
Lazarus Trunk / fpc 2.6.2 / Win32

 

TinyPortal © 2005-2018