Use the form's OnActivate
and OnDeactivate
events. OnActivate
is called when the form gains focus, and OnDeactivate
is called when it loses it.
Note that these events are only triggered when focus is transferred within your own application. If you need to know when your application itself loses or gains focus, use TApplication.OnActivate
and TApplication.OnDeactivate
instead.