I'm trying to make my own piano, and I've got the problem of where if you hold down a key, processing will repeatedly call the keyPressed function even though I only want it to be called once at a time. How do you fix this?
Your solution is good. I believe you are checking mousePressed in draw(). It could work better if you use mousePressed() and mouseReleased() functions. There is a current effort to make it consistent across multiple platforms. Check this discussion for more details: https://github.com/processing/processing/issues/5441
Possibly relevant -- a general-purpose approach to tracking booleans in the way you describe for any key that you press. If you wanted more than one sound sample / video / whatever, you could manage as many as you want through one mechanism.
Answers
Never mind, I figured out how to fix it by adding a .pressed boolean variable and then only allowed it to play if the variable was false.
Your solution is good. I believe you are checking
mousePressed
indraw()
. It could work better if you usemousePressed()
andmouseReleased()
functions. There is a current effort to make it consistent across multiple platforms. Check this discussion for more details: https://github.com/processing/processing/issues/5441Kf
Possibly relevant -- a general-purpose approach to tracking booleans in the way you describe for any key that you press. If you wanted more than one sound sample / video / whatever, you could manage as many as you want through one mechanism.