We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I wrote a simple movie player that can play and pause, but I want to be able to for the sketch to wait for a key to be pressed that would play the said movie.
So nothing happens until you press '1' and then 'movie 1' is played.
(I'm pretty new to Processing but i think i've taught myself the basics, and I suspect redraw could be used for this?)
Answers
You could just use the
keyPressed()
function for this. Something like:This is just example code, but the idea is the same: use a variable to keep track of the state of the sketch, change that state in the
keyPressed()
function, then use that state to determine what to do in thedraw()
function.Check out the Input > Keyboard section of the Processing Reference. In particular, if you are planning to also unpause with the same flag, you may want to use keyReleased(), as this won't fire multiple times per press.
If you are planning on playing / pausing multiple movies with keys 1 2 3 4 etc... then you can duplicate this approach (10 variables, 10 tests, etc.). However, you may instead want to create a single array of flags and check them and update them with loops, e.g.
Finally, you can use HashMap to keep a list of your keys and a true/false play button for each one. This is much more advanced to set up, but then your key checking code is extremely simple, as you only need to look up the last key pressed in the HashMap. No matter how many entries you add in setup(), the checking code just works without changing.
Back in the day I followed this old sketch up on this by creating an updated one that was more a general purpose Toggle keyboard demo -- showing and tracking any keypress, and not defining them up-front. Sharing it here because it has come up again.