We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Whenever I launch a sketch, either from the P3 pde or from Eclipse, the sketch always becomes the top app in my system (Windows, so the title bar is highlighted, and the task bar and task manager both show the sketch app on top). But more and more, I have to click inside the sketch window to get it to recognize keyboard input (i.e., the keypressed() handler is not getting called). And, the problem is intermittent. While working on a sketch, say I run it 20 times, about half the time it recognizes the keyboard directly, and half the time I have to click inside the window first. Clicking on the title bar has no effect, the app just stays on top, but no keystroke response. Also, not changing any significant stuff (settings(), setup(), draw(), mouse/key handlers) just random code changes and keypressed() working/not working.
I've spent a few hours wading through focus management tutorials and posts and at one point I thought I had nailed it with:
frame.requestFocus(); // had no effect on it's own, but thenn seemed to work with toFront() and repaint() frame.toFront(); frame.repaint();
But after that started failing intermittently I came across the switch from 'frame' to 'surface' in 3.0 and switched to:
surface.setAlwaysOnTop(true); // no requestFocus(), toFront(), or repaint() for surface
Again, seemed to help at first but now back to never knowing if the keyboard will work or if I need to click first with the mouse.
I put the following code inside draw() to try and see who/what was getting the focus:
if (KeyboardFocusManager.getCurrentKeyboardFocusManager() != null) println(millis() + ": " + KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner());
It seems like the keystrokes fail when the focus owner shows JFrame or null, and the keystrokes work when the focus starts on the JFrame but then switches to processing.awt.PSurfaceAWT:$SmoothCanvas. Sometimes it switches on it's own (highly desirable, the whole point of this post) or as the result of a mouse click.
Any suggestions for helping manage the startup focus? I don't recall having this problem over the last 5-10 years, just over the past year or so (P3 issue?), but no definitive start date due to the intermittency.