I found the solution for a problem that has been annoying me for quite some time. I am using FVWM for a window manager. A few programs written in Java, most notably the Arduino IDE, will not accept keyboard focus. As soon as the window is given focus using normal window-manager commands, it immediately loses the focus again, making it impossible to eg. edit code or use keyboard shortcuts. I could usually get it to focus by randomly clicking a few menus or some such, but it was hardly usable.
Turns out that FVWM has a Style option to handle this: Lenience. From the FVWM manual:
The ICCCM states that windows possessing the property WM_HINTS(WM_HINTS): Client accepts input or input focus: False should not be given the keyboard input focus by the window manager... A number of applications set this property, and yet expect the window manager to give them the keyboard focus anyway, so fvwm provides a window style, Lenience, which allows fvwm to overlook this ICCCM rule.
And indeed, the problem seems solved after I put the following in my FVWM configuration:
Style "*Arduino*" Lenience
The Arduino IDE window now keeps focus when switching between virtual desktops, using window selection hotkeys, etc. It still seems to require explicitly clicking the window with the mouse to actually accept keyboard events, which is annoying, but at least it is no longer impossible to type into the window. Not sure if this is a fundamental "feature" of Arduino, or if there is a way to fix this also. I rarely use the Arduino IDE anyway.
From the FVWM documentation it would appear that this is a problem with the Arduino code (or the underlying Java window toolkit, I remember seeing similar problems in other Java applications). Not sure why it has never been fixed though - or if it is actually FVWM that is mis-interpreting or being overly zealous with its adherence to ICCCM compliance.