# Strange behavior of code

**URL:** https://discourse.processing.org/t/strange-behavior-of-code/15682
**Category:** Coding Questions
**Created:** [November 21, 2019, 9:56am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682 "2019-11-21T09:56:06Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.processing.org/u/qwerty)
#### Post date: [November 21, 2019, 9:56am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/1 "2019-11-21T09:56:06Z")

</div>

Hello. Today i’ve searching the Internet for the new Processing tutorials and found this one:

```auto

void setup() {
    size(400, 400);
    background(#3355CC);
}

void draw() {
    fill(#3355CC, 20);
    rect(0, 0, width, height);
}

void keyPressed() {
    fill(#FFE200);
    textSize(random(20, 200));
    text(key, random(300), random(100, 400));
}

```

… but this code is not working correctly in my Processing 3.5.3 environment. I mean sometimes it’s works and sometimes it’s not (no any outputs on the screen). Just run it about five times, press some keys and you will see that by yourself. But why is so strange behavior?

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [November 21, 2019, 10:40am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/2 "2019-11-21T10:40:13Z")

</div>

I’ve tried it with mousePressed, because I am working on a tablet. For me it’s working alright. Just a guess.These are two different events. Try using noLoop() end loop() in keyPressed()

---

<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: [November 21, 2019, 10:59am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/3 "2019-11-21T10:59:27Z")

</div>

sorry, but if you start from any tutorial / example /…  
better give a link here…

* * *

can i for your code and @noel comment add,

that you should be aware of that what i call

* * *

- draw mode:

```auto
void setup(){
  size(200,200);
}

void draw() {
  background(200,200,0);
  fill(0);
  text("text",10,10);
}

```

if you would try to draw from key/mousePressed anything it will not work

* * *

OR like from your tutorial

- paint mode

```auto
void setup(){
  size(200,200);
  background(200,200,0);
}

void draw() {
}

void keyPressed() {
  fill(0);
  text("text",10,10);
}

```

* * *

so what is the difference?  
it is WHERE the  
` background()`  
command is, erasing the screen/canvas

* * *

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [November 21, 2019, 11:20am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/4 "2019-11-21T11:20:55Z")

</div>

> [@kll](#):
>
> if you would try to draw from key/mousePressed anything it will not work

So why does it draw perfectly on my tablet?

---

<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: [November 21, 2019, 11:22am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/5 "2019-11-21T11:22:34Z")

</div>

sorry, that comment is related to above draw mode info,  
where the draw has a background command…

NOT to your code…

so i failed to describe the 2 mode thinking??

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [November 21, 2019, 11:29am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/6 "2019-11-21T11:29:46Z")

</div>

> [@kll](#):
>
> so i failed to describe the 2 mode thinking??

Do you mean Processing mode? using logic to solve  
_A = C and B = C, is A = C?_

---

<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: [November 21, 2019, 11:31am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/7 "2019-11-21T11:31:26Z")

</div>

how YOU call it?  
if the background command is

- in setup or
- in draw

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [November 21, 2019, 11:37am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/8 "2019-11-21T11:37:29Z")

</div>

I am confused. Isn’t  
rect(0, 0, width, height);  
the same as  
background()?

---

<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: [November 21, 2019, 11:41am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/9 "2019-11-21T11:41:16Z")

</div>

still with using alpha in fill i would call it PAINT MODE ?  
anyhow i love the fading idea.

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [November 21, 2019, 11:44am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/10 "2019-11-21T11:44:44Z")

</div>

Yes you are right, but I would like to know the correct topic answer, because I think it’s a valid question.

---

<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: [November 21, 2019, 11:50am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/11 "2019-11-21T11:50:43Z")

</div>

ahm,  
for me ( win 10 / 64b // processing 3.5.3 )  
it is very normal, that keyboard not works,  
so i write a intro in setup like:

```auto
println("click on canvas and use: key [s] for save picture. ");

```

so my answer would be: get used to that you have to click with mouse first,  
i never considered that as a BUG??? but yes.

it might also be related with the `focused ` problems.  
[https://processing.org/reference/focused.html](https://processing.org/reference/focused.html)

also not know if it is OS related??

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [November 21, 2019, 11:59am UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/12 "2019-11-21T11:59:39Z")

</div>

Now I got it, on PC it really can go out of focus.  
But if he tried several times, and stops after a few, how can it go out of focus?  
Does it work on your PC?

---

<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: [November 21, 2019, 12:03pm UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/13 "2019-11-21T12:03:35Z")

</div>

possibly not go out of focus, i say from here

> 4 of 10 start a key press works

---

<div class="post-metadata">

### Author: ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)
#### Post date: [November 21, 2019, 12:06pm UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/14 "2019-11-21T12:06:46Z")

</div>

> [@qwerty](#):
>
> I mean sometimes it’s works and sometimes it’s not (no any outputs on the screen).

Did you make sure that the window that pop ups right after running the sketch is selected? The sketch doesn’t respond to my keyboard _unless_ I click in the window with my mouse.

---

<div class="post-metadata">

### Author: ![Tiemen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tiemen/32/5477_2.png) [@Tiemen](https://discourse.processing.org/u/Tiemen)
#### Post date: [November 21, 2019, 12:10pm UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/15 "2019-11-21T12:10:01Z")

</div>

> [@kll](#):
>
> how YOU call it?  
> if the background command is
> 
> - in setup or
> - in draw

I think the terms _draw_ and _paint_ mode are good names. You redraw everything or you paint on top of the existing canvas.

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [November 21, 2019, 12:15pm UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/16 "2019-11-21T12:15:59Z")

</div>

What does happen after that?  
Could you try to put the noLoop loop in keyPressed?  
For instance; when I draw in the event onBluetoothDataEvent(String who, byte[] data)  
and use fill there, it will mess up with the draw() event, if I don’t use noLoop.

---

<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: [November 21, 2019, 12:20pm UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/17 "2019-11-21T12:20:11Z")

</div>

> [@noel](#):
>
> Could you try to put the noLoop loop in keyPressed?

sorry i not play this,  
but i know that keyPressed works as long there is a

```auto
draw(){}

```

independent on loop() or noLoop()  
so a keyboard operation for loop() or redraw() ( from noLoop() ) is possible.

( besides above focused condition what also not works good in windows… )

\_\_  
now if we click on canvas we can use keyboard until click on other window ( or ALT TAB )  
mouse position does not matter meanwhile(?)

* * *

something to play with

```auto
String usertxt="";

void setup () {
  size(500, 500);
  println("use: any key ");
  fill(0);
}
void draw () {
  surface.setAlwaysOnTop(true); // actually not helps for this problem
  background(200,200,0);
  if ( mousePresent && focused ) text("focus + see mouse",10,10);
  if ( mousePresent ) text("see mouse",10,30);
  text(usertxt,20,height/2);
}

void keyPressed() {
 usertxt+=key;
}

boolean mousePresent=false;
//_ 2 nice build ins undocumented, not too helpful
// https://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseListener.html#mouseEntered(java.awt.event.MouseEvent)

public void mouseExited() {
  mousePresent = false;
}

public void mouseEntered() {
  mousePresent = true;
}

```

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [November 21, 2019, 12:36pm UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/18 "2019-11-21T12:36:50Z")

</div>

So i went to my neighbours house to bother him, and tried it on his windows 7 64b computer… Unfortunately I could not reproduce the error. It worked perfectly.

---

<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: [November 21, 2019, 1:03pm UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/19 "2019-11-21T13:03:40Z")

</div>

like i show already  
win 10 64b

> 4 of 10 start a key press works

> [@noel](#):
>
> windows 7 64b computer

i also tested on the old PC,

> 7 of 10 start a key press works

booted win7 32b ( & processing 3.5.3 32b )

> 8 of 10 start a key press works

* * *

so win10 is actually worst!

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [November 21, 2019, 3:04pm UTC](https://discourse.processing.org/t/strange-behavior-of-code/15682/20 "2019-11-21T15:04:35Z")

</div>

What happens with a lower frameRate?
