Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if my application has focus?

What I want to do is check if my application has focus because if it is not then I will popup an Alert Window just over the Notification Area to display some message to the end user.

like image 791
fossilz Avatar asked Sep 14 '10 20:09

fossilz


People also ask

How do I know if my window is in focus?

Use the document. hasFocus() method to check if a window has focus, e.g. if (document. hasFocus()) {} . The method returns a boolean value indicating whether the document or any element in the document has focus.

Is focused HTML?

HTML | DOM hasFocus() Method. In HTML document, the document. hasfocus() the method is used for indicating whether an element or document has the focus or not. The function returns a true value if the element is focused otherwise false is returned.

What is window focus in JavaScript?

Window focus() The focus() method sets focus to a window. The blur() method removes focus from a window.


3 Answers

D2007 has this useful property

if Application.Active then
//
like image 96
Anton Duzenko Avatar answered Oct 28 '22 22:10

Anton Duzenko


Call Windows.GetForegroundWindow() and then pass the HWND to the Controls.FindControl() function. It will return a non-nil TWinControl pointer if the HWND belongs to your process. For example:

if FindControl(GetForegroundWindow()) <> nil then
  // has focus ...
else
  // does not have focus ...
like image 28
Remy Lebeau Avatar answered Oct 28 '22 21:10

Remy Lebeau


If your application consists of a single form, then

GetForegroundWindow = Handle

will suffice. The expression above is true if and only if the your form is the foreground window, that is, if keyboard focus belongs to a control on this form (or to the form itself).

If your application consists of a number of forms, simply loop through them and check if any of them matches GetForegroundWindow.

like image 38
Andreas Rejbrand Avatar answered Oct 28 '22 21:10

Andreas Rejbrand