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.
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.
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.
Window focus() The focus() method sets focus to a window. The blur() method removes focus from a window.
D2007 has this useful property
if Application.Active then
//
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 ...
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With