Strange behavior of code

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


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?

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()

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:
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
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


1 Like

So why does it draw perfectly on my tablet?

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??

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

how YOU call it?
if the background command is

  • in setup or
  • in draw

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

1 Like

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

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

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:

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

also not know if it is OS related??

1 Like

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?

possibly not go out of focus, i say from here

4 of 10 start a key press works

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.

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

1 Like

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.

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

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

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;
}

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.

like i show already
win 10 64b

4 of 10 start a key press works

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!

What happens with a lower frameRate?