# Sketch does not always have focus on start up

**URL:** <https://discourse.processing.org/t/sketch-does-not-always-have-focus-on-start-up/16834>\
**Category:** Coding Questions\
**Created:** [January 3, 2020, 3:28pm UTC](https://discourse.processing.org/t/sketch-does-not-always-have-focus-on-start-up/16834 "2020-01-03T15:28:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rhole](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rhole](https://discourse.processing.org/u/rhole)\
**Post date:** [January 3, 2020, 3:28pm UTC](https://discourse.processing.org/t/sketch-does-not-always-have-focus-on-start-up/16834/1 "2020-01-03T15:28:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rhole](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rhole](https://discourse.processing.org/u/rhole)\
**Post date:** [January 3, 2020, 4:58pm UTC](https://discourse.processing.org/t/sketch-does-not-always-have-focus-on-start-up/16834/2 "2020-01-03T16:58:55Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [January 4, 2020, 3:32am UTC](https://discourse.processing.org/t/sketch-does-not-always-have-focus-on-start-up/16834/3 "2020-01-04T03:32:46Z")

</div>

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

> **[Processing 2.x and 3.x Forum](https://forum.processing.org/two/discussion/comment/111873/#Comment_111873)**
>
> Processing is an electronic sketchbook, a language and a worldwide community. This is its forum.

Kf

---

<div class="post-metadata">

**Author:** ![rhole](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rhole](https://discourse.processing.org/u/rhole)\
**Post date:** [January 4, 2020, 2:41pm UTC](https://discourse.processing.org/t/sketch-does-not-always-have-focus-on-start-up/16834/4 "2020-01-04T14:41:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 4, 2020, 3:14pm UTC](https://discourse.processing.org/t/sketch-does-not-always-have-focus-on-start-up/16834/5 "2020-01-04T15:14:52Z")

</div>

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 )

> [@rhole](#):
>
> java.awt which we are advised to not use

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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![rhole](https://avatars.discourse-cdn.com/v4/letter/r/e19adc/32.png) [@rhole](https://discourse.processing.org/u/rhole)\
**Post date:** [January 4, 2020, 3:50pm UTC](https://discourse.processing.org/t/sketch-does-not-always-have-focus-on-start-up/16834/6 "2020-01-04T15:50:02Z")

</div>

This link discusses changes in Processing 3:

> **[processing/processing](https://github.com/processing/processing/wiki/Changes-in-3.0)**
>
> Source code for the Processing Core and Development Environment (PDE) - processing/processing

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](https://github.com/processing/processing/tree/master/core)). 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?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 4, 2020, 3:57pm UTC](https://discourse.processing.org/t/sketch-does-not-always-have-focus-on-start-up/16834/7 "2020-01-04T15:57:18Z")

</div>

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

```auto
</> 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.

* * *
