We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi! First of all, I'm a newbie and this is the first time I'm writing a Processing sketch seriously after a lot of following examples from books. Here's where I'm stuck.
I'm trying to build a small application where patterns are generated with minim and audio input. I've defined all the functions etc and I was trying to toggle them with keyPressed and then I realized that I'll need to keep holding down the key for the pattern to keep looping. How do I solve this where once i press 1 or 2 it switches to that function and loops continuously? Below is what I've written.
//PATTERN SELECTION
if (keyPressed) { if (key == '1') { pattern01(); } else if (key == '2') { pattern02(); } else if (key == '3') { pattern03(); } }
Answers
Rather than using key pressed inside the draw() method you are better using the keyPressed or keyReleased method. Personally I prefer the latter. in this method use a variable to remember the key e.g.
then in draw use a switch statement to decide which method to call e.g.
Oh wow! Thanks, it works (even though I don't exactly know how)!
I kind of get what you're doing but the switch function is something I've not used before, will look it up.
Thanks again!
:D
The
switch
statement allows you to choose from multiple options whereas theif
statement only gives you a choice of 2, although you can chain them together as you did.The switch statement looks for the first
case
statement that matches the value stored inkeychar
and then starts executing the program from that point. When abreak
statement is found the program jumps to the next statement after the switch in this case line 12.I've run into a new problem now though, I had a few other keyPressed options enabled, like using the arrow keys for changing the value of one integer and w and b to change the background colour to white and black - they don't seem to be working properly anymore - I'm guessing it's coz the key is being stored inside keychar instead of functioning how it should.
Can I get keyPressed to ignore a few keys?
Oh wait, I could do that with a if and || functions I'm guessing. If (key=='w') || (key == 'b'){keychar='0'} else keychar = key
let me see if that works
Try changing the
keyPressed()
in my previous post to method tokeyReleased()
BTW
keychar = key;
does not change the value inkey
it simply makes a copy.Nope, not helping.
Here's the full sketch, tell me what's going on? The arrow keys are freezing the output.
And the w & b keys are supposed to just change the background but it's overriding the pattern and filling up the whole screen.
If I just leave pattern01(); or pattern02(); instead of the whole switch function, the rest seems to work perfectly.
Difficult because I am not sutre what you hope to achieve try changing lines 13-15 to
yes! That fixes it!
Eternally grateful!