We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I was looking for some codes but many results are for processing one, or using deprecated keyEvent code, or relying on keyPressed() function, or even depending on extra libraries...
I believe keyPressed() function doesn't work for me now so I am using keyPressed boolean.
This solution works partially. My code will stop firing after shift is pressed/released. I just wanted the Shift key to change "speed" of a movement while user is holding an arrow key permanently.
KeyEvent e; //do I really need this workaround?
void setup() {
}
void draw() {
if (keyPressed) {
//int location = keyEvent.getKeyLocation();
//println( location );
//KeyEvent.KEY_LOCATION_LEFT:
if (key == CODED) {
if (keyCode == LEFT) {
if (e.isShiftDown()) { //can't I get KeyEvent "e" from other than keyPressed( KeyEvent )?
println(millis(), "FASTER");
} else {
println(millis(), "SLOWER");
}
}
}
}
}
void keyPressed(KeyEvent ev) {
e = ev;
}
Answers
I don't understand why you can't use keypressed().
Thanks for your reply!
keyPressed() works, but it will produce a lot of extra lines for the implementation I need to do... I was just wondering if this is the best practice to trigger the action as I wanted. Using your snippet I was able to make the code below:
Yes, it's a bit more code, but not that much more? Besides, you separate the key-stuff/interaction from the other stuff in draw(). Which means your code will probably be easier to read and work with.
GoToLoops's code is even longer. But also even cooler :)