We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Problem: Erratic keyPressed() behavior.
I have made a small program to learn how to use if-statements. I use the draw-, keyPressed- and mousePressed functions.
The purpose of the program is to change the color on the left and right side of the window with mouse cursor placement and with key presses.
The strange thing is that the keyPressed() function sometimes works as it should, and sometimes doesn´t work unless I first press the mouse once.
Can someone explain this erratic behavior of the keyPressed() function and how I can remedy it?
Windows. Processing 3.01
// PROCESSING 3.01
// ARTEFAKT 2016
//////////////////////////////////////////////////////////////////////////////////////////
// EXPERIMENT WITH IF STATEMENTS
// 1. Draw window with two colors; one to the left and another to the right.
// 2. Cursor placements (mouseX) change which color will show on each side
// 3. Pressed mouse key (mousePressed()) reverse color placement
// 4. Pressed keyboard key (keyPressed()) randomize color; 'l' for left side, 'r' for right side
//////////////////////////////////////////////////////////////////////////////////////////
// ERROR /////////////////////////////////////////////////////////////////////////////////
// NUMBER 4 NOT WORKING PROPERLY! Sometimes it only works after having pressed the
// mouse once. Not consistent. Why is that?
//////////////////////////////////////////////////////////////////////////////////////////
// --- START -----------------------------------------------------------------------------
// --- INITIALIZE ------------------------------------------------------------------------
// --- VARIABLES FOR COLORS
// Color One
int redOne;
int greenOne;
int blueOne;
// Intermediary "Color buckets"
int colorBucketRed;
int colorBucketGreen;
int colorBucketBlue;
// Color Two
int redTwo;
int greenTwo;
int blueTwo;
// --- SETUP -----------------------------------------------------------------------------
void setup () {
size (600, 400);
background (155);
smooth();
// RANDOMIZE COLORS
redOne = (int) random(255);
greenOne = (int) random(255);
blueOne = (int) random(255);
redTwo = (int) random(255);
greenTwo = (int) random(255);
blueTwo = (int) random(255);
}
// --- DRAW FUNCTION ---------------------------------------------------------------------
// --- BEHAVIOR: Draw colors in window. Reverse colors depending on cursor placement -----
void draw () {
if (mouseX < width/2) { // When cursor to the left;
fill (redOne, greenOne, blueOne); // Draw left rectangle
rect (0, 0, 299, 400);
fill (redTwo, greenTwo, blueTwo); // Draw right rectangle
rect (300, 0, 600, 400);
} else { // When cursor to the right;
fill (redTwo, greenTwo, blueTwo); // Draw left rectangle
rect (0, 0, 299, 400);
fill (redOne, greenOne, blueOne); // Draw right rectangle
rect (300, 0, 600, 400);
}
}
// ---- MOUSEPRESSED FUNCTION ------------------------------------------------------------
// ---- BEHAVIOR: Mouse press reverse colors ---------------------------------------------
void mousePressed() {
// Store left colors in "color buckets"
colorBucketRed = redOne;
colorBucketGreen = greenOne;
colorBucketBlue = blueOne;
// Change left colors to the same as the right colors
redOne = redTwo;
greenOne = greenTwo;
blueOne = blueTwo;
// Change right colors from the stored left colors
redTwo = colorBucketRed;
greenTwo = colorBucketGreen;
blueTwo = colorBucketBlue;
}
// --- KEYPRESSED FUNCTION ---------------------------------------------------------------
// --- BEHAVIOR: Keyboard key press randomize colors, 'l' on left side, 'r' on right side
void keyPressed() {
if (mouseX < width/2) { // When cursor on left side:
// If 'l' is pressed, radomize left color
if (key == 'l' || key == 'L') {
redOne = (int) random(255);
greenOne = (int) random(255);
blueOne = (int) random(255);
}
// If 'r' is pressed, randomize right color
if (key == 'r' || key == 'R') {
redTwo = (int) random(255);
greenTwo = (int) random(255);
blueTwo = (int) random(255);
}
} else { // When cursor on right side - reverse the above
// If 'l' is pressed, radomize left color
if (key == 'l' || key == 'L') {
redTwo = (int) random(255);
greenTwo = (int) random(255);
blueTwo = (int) random(255);
}
// If 'r' is pressed, randomize right color
if (key == 'r' || key == 'R') {
redOne = (int) random(255);
greenOne = (int) random(255);
blueOne = (int) random(255);
}
}
}
// ---- END ------------------------------------------------------------------------------
Answers
keyPressed() is only triggered when sketch's canvas is the active window.
If it isn't, we need to click at anywhere in its window in order to gain focus. :-B
Sorry, but that doesn´t seem to be the case. The window is active. The "mouse over" behavior changes colors, but keyboard key presses don´t. I can grab the border of the window, move it, and then try to press the key with no effect (unless I press the mouse key inside the canvas). Sometimes it works, and sometimes it doesn´t even if I use exactly the same steps.
If I press the "play" button and the window appears, and then press the keyboard keys, sometimes it works and sometimes it doesn´t...
"System" variables mouseX & mouseY _ don't need that the canvas' window to have focus!
But as I've mention, keyPressed() demands window focus. Take a look at _focused reference:
https://processing.org/reference/focused.html
Ok, I will look it up. Thanks.
GoToLoop, you were right. I tested to see if the window was focused with the following code inside the draw() function:
The question then is why the window sometimes is active and sometimes isn´t, though I do exactly the same. What process makes the window active or non-active at the start?
Dunno, sorry! But it seems like that CTRL+R got more chance to keep focus when starting. 8-|
I tested the two starting methods, each 50 times. This is the result:
...........................................
PLAY BUTTON IN PDE
Non-focused: 36 times
Focused: 14 times
...........................................
CTRL-R
Non-focused: 37 times
Focused: 13 times
...........................................
So, no difference between them. The canvas will start "non-focused" in about 70-75 percent of the cases. I wonder why.