Sketch does not always have focus on start up

I have a sketch which frequently does not gain focus at start-up. I was able to solve this:

boolean _focusConfirmed= false;

void draw(){
// ensure focus comes to this sketch on start-up
if (millis() < 20000 && !_focusConfirmed ){
if( !focused ) {
println(“not focused”, millis(),“mSec”);
((java.awt.Canvas) surface.getNative()).requestFocus();
}
else {
println(“screen has focus”, millis(),“mSec”);
_focusConfirmed= true;
}
}
//… continue with code

The test for 20000 miilis() allows up to 20 seconds for setup to complete as I am loading some files there. The global _focusConfirmed allows multiple attempts to gain focus until successful.

2 Likes

In my code above I am using
((java.awt.Canvas) surface.getNative()).requestFocus();

But java.awt is not recommended. Does anyone have a way to request focus in Processing 3?

Please review this post and see if it helps and if still is valid:

Kf

That post also uses java.awt which we are advised to not use in the future. My code, using java.awt with Processing 3.0.1 seems to work perfectly. I will load the current 3.5, perhaps the underlying problem of sketch starting without focus will go away.

thank you for sharing,
your code works fine also with processing 3.5.3
and can improve ( the start for ) many keyboard heavy games.
i also try different renderers P2D, P3D ( under win 10 )

please could you link to that info?


give me some time to check about P 4.0 beta
ok same improvement
( but some not related warnings at P3D )
used code mod:

boolean _focusConfirmed = false; //__ if false force set focus

void setup() {
  size(200,200); //,P3D);
}

void draw() {
  check_focus();
}

void keyPressed() {
  println("key "+key);
}

void check_focus() {
  if ( !_focusConfirmed ) { //&& millis() < 20000 ) {
    if ( !focused ) ((java.awt.Canvas) surface.getNative()).requestFocus();
    else            _focusConfirmed= true;
    println("focus_check: time ", millis(), "ms");
  }
}


// https://discourse.processing.org/t/sketch-does-not-always-have-focus-on-start-up/16834
// @rhole ( Richard L Hole )

//__ processing sketch need to have FOCUS to detect a key press
//__ so usually we click with mouse on canvas after start, to allow keyboard
//__ as like on WIN10 30% at start there is NO focus
//__ but this code seems to work very good


This link discusses changes in Processing 3:

Library authors, now is the time to reduce your reliance on AWT! In 3.0, we’ve moved away from AWT in a big way (here’s why). Any library features that require AWT should be treated with suspicion. Modes and Tools can still use AWT, but the OpenGL renderers ( P2D and P3D ) and the upcoming FX2D renderer don’t use AWT at all.

Quoting the last paragraph:

  • This is one reason we built the processing.event classes in 2.x, and have been removing spurious AWT usage from the core API, documentation, and examples.) It’s now been a couple years since we made those changes.
  • The other reason is that we can’t rely on AWT features when targeting JavaScript or Android, so it was encouraging bad habits.

By the way, I am new to this forum. How did you paste your code in a code-block?

1 Like

thanks,
not worry that too much,
esp. as you still 2 rev behind…

now as i see that i would recommend doing test for the different renderers,
well, i did it little bit ( and only win 10 )


and to your original question?
i also not know any better way to do it.


please format your code posting by pasting it into the

</> code button

of the editor header menu ( context name: Preformatted text )
it looks like
```
type or paste code here
```

also can use the ``` manually above and below your code.

thank you.