Author Topic: [SOLVED] How to "unfocus" a Form?  (Read 1401 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 1188
Re: How to "unfocus" a Form?
« Reply #15 on: October 03, 2026, 07:59:14 pm »
Just an intermediate result: the issue, that sometimes, when my Form is made visible, it gets the Focus and the code from paweld from reply #13 does *not* move the Focus back to the program, who had it before (which means, the Focus stays on my Form):
 - I can now reproduce it with KDE-Plasma Deskop
 - until now I saw this never with Gnome-Desktop.

If someone has an idea, what
 - either I could try to find out the root cause
 - or for an improvement / workaround in the code from reply #13, which might solve this issue,
would be very welcome.

As said, I'm on Linux Ubuntu 24.04 64bit with KDE-Plasma and GTK2 and X11 WindowManager with Lazarus 3.4.0 and 2.0.10 (both act the same).

Hartmut

  • Hero Member
  • *****
  • Posts: 1188
Re: How to "unfocus" a Form?
« Reply #16 on: October 04, 2026, 07:06:17 pm »
Meanwhile I tested more about the issue, that sometimes, when my Form is made visible and gets the Focus and the code from paweld from reply #13 *fails* to move the Focus back to the program, who had it before (which means, the Focus stays permanently on my Form).

I found out these more informations:
a) XGetInputFocus() always returns correctly the Window-ID of the program, which currently has the Focus, before showing my Form. I checked with Linux command 'xwininfo -int'.

b) after calling Form1.Show() to make my Form visible, I have to wait at least 150 ms (calling Application.ProcessMessages() multiple times), before I can call XSetInputFocus(). To be save, in all tests I used 1000 ms.

c) when the issue occurs, 'Form1.Active' returns 'True' and XGetInputFocus() returns the Window-ID of 'Form1'. This is no surprise, because 'Form1' has the Focus (what it shouldn't).

d) in all cases, where the issue does *not* occur, XSetInputFocus() is able to set alternatively the Focus successfully to e.g. the open Lazarus Editor or the KDE-Desktop or other open programs.

e) but in all cases, where the issue *does* occur, XSetInputFocus() is *not* able to set the Focus alternatively to e.g. the open Lazarus Editor or the KDE-Desktop or other open programs.

f) this sounds, that the problem is *not* an issue with 1 specific target window. It sounds, that when the issue condition is met, that then XSetInputFocus() is completely unable to work, regardless which target window I choose.

g) when the issue occurs, which I can detect by 'Form1.Active' or XSetInputFocus(), see c), minimizing and normalizing my Form (as described in my very 1st post) moves the Focus successfully to the program, who had it before. So this is not "blocked" anyway, only XSetInputFocus() cannot do it.

What I tried, but without success:
 - to call XCloseDisplay() at the end of RestoreFocus()
 - to call XOpenDisplay() only once at program start, not always again in SaveFocus().

If someone has an idea, what
 - either I could try to find out the root cause
 - or for an improvement / workaround in the code from reply #13, which might solve this issue,
would be very welcome.

As said, I'm on Linux Ubuntu 24.04 64bit with KDE-Plasma and GTK2 and X11 WindowManager with Lazarus 3.4.0 and 2.0.10 (both act the same).

Hartmut

  • Hero Member
  • *****
  • Posts: 1188
Re: How to "unfocus" a Form?
« Reply #17 on: October 05, 2026, 06:38:06 pm »
Just another intermediate result: when the issue occurs, that the code from paweld from reply #13 *fails* to move the Focus back to the program, who had it before, then I can use 2 Linux commands
 - 'wmctrl -i -a <windows-id>' or
 - 'xdotool windowactivate <windows-id>'
to successfully move the Focus to the previuos program.
There is a disadvantage, that then this previuos program is also moved to the foreground, so it can cover "my Form" partly or completely. Will try to prevent this with 'FormStyle:=fsSystemStayOnTop'.

This proves again, that there is an issue with XSetInputFocus() and not with the target window, as far as I can see.

If someone has an idea, what
 - either I could try, to find out the root cause, why XSetInputFocus() fails
 - or for an improvement in the code from reply #13, which might solve this issue,
this would be very welcome.

paweld

  • Hero Member
  • *****
  • Posts: 1796
Re: How to "unfocus" a Form?
« Reply #18 on: October 07, 2026, 05:00:46 am »
I only use Linux occasionally, so I can't help any further - during testing, every attempt was successful. Check to see if, after shifting focus back to the previous application, there are any calls in your code that might cause the focus to shift back to your program.
Best regards / Pozdrawiam
paweld

Hartmut

  • Hero Member
  • *****
  • Posts: 1188
Re: How to "unfocus" a Form?
« Reply #19 on: October 07, 2026, 08:48:09 pm »
Check to see if, after shifting focus back to the previous application, there are any calls in your code that might cause the focus to shift back to your program.
Thank you paweld for your post. I checked multiple times, there is no code like that. And I tested after RestoreFocus() to wait some seconds, calling Application.ProcessMessages() repeatedly, and it's sure, that RestoreFocus() does nothing.

But I found a way to reproduce the issue, that XSetInputFocus() fails in a way, which has only a very small difference, whether XSetInputFocus() fails or not. Maybe this helps to come closer:

a) XOpenDisplay() is called only once at program start
b) when conditions are met to show my Form (which most time is not visible), the following is done:
c) XGetInputFocus() is called. It always returns correctly the Window-ID of the program, which currently has the Focus, before I show my Form.
d) Form.Show() is called to make my Form visible
e) wait at least 150 ms and call Application.ProcessMessages() some times, until my Form is really visible => my Form gets the Focus (in all tests I waited 1000 ms to be safe)
f) call XSetInputFocus() + XSync() + XFlush() including XErrorHandler() as in reply #13 from paweld
g) now it works correctly (it sets the Focus back to the window got by XGetInputFocus())
h) close my Form via Button, which only calls Form.Hide
i) now important:
    - if you want, that next XSetInputFocus() fails: do not klick anywhere! Not in any open window of any other program and not on the desktop (but you may move the mouse)
    - if you want, that next XSetInputFocus() works correctly: klick at least once in an arbitrary window of an arbitrary other program or on the desktop
k) wait until conditions are met to show my Form a 2nd Time
l) above steps c) to f) are executed as before, but now XSetInputFocus() fails, if you did not click anywhere after pressing the close-button - if you clicked anywhere, XSetInputFocus() works correctly.

All the following made no difference:
 - I called XSetInputFocus() alternatively with *other* Window-ID's, taken from an open Lazarus Editor or the KDE-Desktop or other open programs. In the situation, when XSetInputFocus() fails, the target does not matter, in this situation XSetInputFocus() fails completely with all targets. But I can successfully set the Focus e.g. via Linux commands 'wmctrl' or 'xdotool' (see reply #17) or by minimizing and normalizing my Form, so this is not "blocked" - only XSetInputFocus() fails.
 - To check, if XSetInputFocus() only has a problem, to set the same Window-ID 2 times after each other: alternatively I used 2 different Window-ID's from other programs, which always in the 1st call worked correctly, but his did not help. Again the 2nd call of XSetInputFocus() failed.
 - I replaced my Form, which was the Main-Form, by a separate, dynamically created Form (and called also Form.Hide after the Close-button)
 - Then after Close-button I called Form.Close and FreeAndNil() instead of only Form.Hide and created the dynamic Form again for each Form.Show
 - I called XCloseDisplay() each time after XSetInputFocus() and called XOpenDisplay() each time again before calling XGetInputFocus().

If someone has an idea, what
 - either I could try, to find out the root cause, why XSetInputFocus() fails
 - or for an improvement in the code from reply #13, which might solve this issue,
this would be very welcome.

440bx

  • Hero Member
  • *****
  • Posts: 6628
Re: How to "unfocus" a Form?
« Reply #20 on: October 07, 2026, 09:01:43 pm »
here is an idea... completely untested, I don't know if it will work or not but, at least it's different.

the idea is, create an invisible popup window, the important thing is that a popup window can get the focus.  set the focus to that invisible popup window, in theory, that should switch the focus from the window that currently has it to the popup.  It might be possible to recover the focus by sending the popup window a message telling it to restore/set the focus to a particular window.

As stated previously, I have no idea if that will work but, I think it's worth a shot.  There is nothing to lose other than the little bit of time it takes to try it out.

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Hobbit00

  • Jr. Member
  • **
  • Posts: 70
Re: How to "unfocus" a Form?
« Reply #21 on: October 07, 2026, 10:00:45 pm »
@440bx

Just recently, while testing my applications following updates to Delphi and—more importantly—Windows (26H2), I encountered an issue specifically involving hidden windows.
In the past, when I didn't want to assign the default focus to a specific control, I would set the focus to a component that was read-only, disabled, or hidden.
Now, however, if I set the focus to a disabled or hidden component, the program crashes with the error: "The control cannot receive focus".

I don't know if this behavior is something new linked to these two events (or one of them), or if it existed in the past and I simply never happened to come across it.

Hartmut

  • Hero Member
  • *****
  • Posts: 1188
Re: How to "unfocus" a Form?
« Reply #22 on: October 08, 2026, 06:19:00 pm »
the idea is, create an invisible popup window, the important thing is that a popup window can get the focus.  set the focus to that invisible popup window, in theory, that should switch the focus from the window that currently has it to the popup.  It might be possible to recover the focus by sending the popup window a message telling it to restore/set the focus to a particular window.

Thanks 440bx for that idea. Do you suggest this for Windows or Linux? For Windows I have a good solution...

I'm not so familiar with popup windows. Do you mean a "standalone" popup window, not connected to anything?
I have no idea how to set the focus to an invisible popup window.
Are you aware, that my goal is, to move the Focus not only away from my Form, but to move it back to the program, who had the Focus, before my Form was made visible?
So that the Focus is not stolen from there?



Meanwhile I found out, that my issue on Linux with function XLib.XSetInputFocus() is a *general issue* with this function on KDE-Desktop. Because this does not fit into this subforum, I created a new Topic in Linux subforum in https://forum.lazarus.freepascal.org/index.php/topic,75079.0.html

If you have any ideas about function XLib.XSetInputFocus(), please write them only there. Thanks.

440bx

  • Hero Member
  • *****
  • Posts: 6628
Re: How to "unfocus" a Form?
« Reply #23 on: October 08, 2026, 10:57:20 pm »
My knowledge of Linux is, for practical programming purposes, none.  I suggested that because I thought it may also apply to Linux but, I have no idea if it does.

Just FYI, in Windows, a popup window behaves very similarly to a top level window.  The important thing is that regardless of which window has the focus in the program, the program remains the active program.

At this point I have to admit that I am somewhat unclear as to what it is exactly that you're trying to accomplish.  Focus and window activation are intimately related.  The important thing is that, at least in Windows, the O/S has made it very difficult for the focus to jump from one app to another, that is to prevent a free-for-all where any app could get the focus at anytime which would be quite confusing for the user, not to mention potentially very inconvenient.


FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Hartmut

  • Hero Member
  • *****
  • Posts: 1188
Re: How to "unfocus" a Form?
« Reply #24 on: October 09, 2026, 10:12:52 am »
At this point I have to admit that I am somewhat unclear as to what it is exactly that you're trying to accomplish.
My goal ist: my program runs mostly in background, where it's Form is not visible (because of Form1.Hide). When certain circumstances are met, the Form is made visible (done by Form.Show), which sometimes gives it the Focus (sometimes not, don't know why), which "steals" the Focus from the previous program. In this case, I want to move the Focus back to the previous program.

Unfortunately you did not answer my questions:
 - Do you mean a "standalone" popup window, not connected to anything?
 - How can I set the focus to an "invisible popup window"?
 - Do you (still) assume, that such a popup window may be helpful for my goal?

If yes, do you mean with "invisible popup window" perhaps to give it only no MenuItems and that's all?

440bx

  • Hero Member
  • *****
  • Posts: 6628
Re: How to "unfocus" a Form?
« Reply #25 on: October 09, 2026, 12:20:00 pm »
The answers I can provide only apply to Windows because my knowledge of Linux is next to nil.

Now, with that out of the way...


When certain circumstances are met, the Form is made visible (done by Form.Show), which sometimes gives it the Focus (sometimes not, don't know why), which "steals" the Focus from the previous program.
My _guess_ is that the somewhat erratic stealing of the focus is timing related, specifically related to the value of ForegroundLockTimeout in the registry.  Depending on time elapsed, the behavior will vary depending on the value of that key.  Again, this is a _guess_, I haven't tested it but, that's where I'd start looking for the cause of the problem.

In this case, I want to move the Focus back to the previous program.
That by itself is already a problem.  In theory a program should not be able to steal the focus from another one. Newer versions of Windows have made it increasingly difficult for a program to steal focus or to give the focus to a specific program.  IIRC, in Windows, your program should get a message when the focus is switched that includes the process id of the program that is losing it. 

Unfortunately you did not answer my questions:
You're right.  I didn't because I cannot provide answer that work for both Windows and Linux.  That said, I will answer your questions but, the answers are only valid for Windows.

- Do you mean a "standalone" popup window, not connected to anything?
Honestly, I'm not sure what you mean by that.  In Windows, a popup window acts as a separate, independent, window that is owned by the program that created it.  Since it is independent, it could be considered standalone but, its lifetime is still obviously tied to the program that created it.

- How can I set the focus to an "invisible popup window"?
In Windows, use the SetFocus API.  You may need to call AttachThreadInput.

- Do you (still) assume, that such a popup window may be helpful for my goal?
Now that I have a better understanding of what you're trying to accomplish, I doubt my suggestion can help. 

One thing that comes to mind is to create the window as "unfocusable", i.e, using WS_EX_NOACTIVATE when creating it.  That way you'll never have to worry about the window gaining the focus.  Manipulating the WS_EX_NOACTIVATE you might be able to control when and if the window can gain the focus at specific times.  I have no idea if there is an equivalent for Linux.

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Hartmut

  • Hero Member
  • *****
  • Posts: 1188
Re: How to "unfocus" a Form?
« Reply #26 on: October 09, 2026, 05:38:50 pm »
Thanks 440bx for that infos. Again I learned something new.

Quote
One thing that comes to mind is to create the window as "unfocusable", i.e, using WS_EX_NOACTIVATE when creating it.  That way you'll never have to worry about the window gaining the focus.  Manipulating the WS_EX_NOACTIVATE you might be able to control when and if the window can gain the focus at specific times.  I have no idea if there is an equivalent for Linux.
Did not know this before. But as said my Form has some buttons (and maybe in the future more controls) and I fear possible problems with an "unfocusable" window. And I want the normal icons in the upper right corner (for minimize and close) and the normal taskbar entry. All this would be too much "experiments" at this time. But I added it to my jotter for possible future use.

I set this Topic now to "solved". I have a good solution for Windows from Martin_fr (using GetForegroundWindow() + SetForegroundWindow() + 'FormStyle:=fsSystemStayOnTop', see reply #10) and fortunately today paweld found a good solution for Linux, see reply #1 in https://forum.lazarus.freepascal.org/index.php/topic,75079.0.html

Again thanks a lot to all who helped me.

440bx

  • Hero Member
  • *****
  • Posts: 6628
Re: [SOLVED] How to "unfocus" a Form?
« Reply #27 on: October 09, 2026, 06:18:54 pm »
I'm very pleased you got working solutions for both Windows and Linux.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

 

TinyPortal © 2005-2018