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.