This is to do with user interaction with processing via the keyboard... I guessed this was the best section to post in.
I was earlier frustrated by the number of variables I was having to write to track which keys were currently being pressed. This is very important if you want to press the up and left key at the same time, for example in a driving game to accelerate and turn at the same time. As I understand the keyPressed variable on its own doesn't contain enough information about which keys have been pressed and which ones have been released.
So, my usual solution is as follows:
Code:boolean keyPressedUp = false;
boolean keyPressedDown = false;
void keyPressed() {
if(keyCode == UP) keyPressed = true;
if(keyCode == DOWN) keyPressed = true;
}
void keyReleased() {
if(keyCode == UP) keyPressed = false;
if(keyCode == DOWN) keyPressed = false;
}
Now I can keep track of which keys are current depressed, well for UP and DOWN. If I want to track LEFT, UP, SHIFT, or any of the others, I have to create a new variable, and add logic to keyPressed() and keyReleased().
My newest solution/idea is:
Code:boolean[] keysPressed = new boolean[128];
void keyPressed() {
keysPressed[keyCode] = true;
}
void keyReleased() {
keysPressed[keyCode] = false;
}
Now I don't have to worry about which keys I want to use, because their states are all being stored for me:
Code:if(keysPressed[LEFT]) {
...turn car left...
}
if(keysPressed[RIGHT]) {
...turn car right...
}
if(keysPressed[UP]) {
...accellerate...
}
if(keysPressed[DOWN]) {
...brake...
}
if(keysPressed[SHIFT]) {
...handbrake...
}
...etc...
This seems to work really well for me. My big limitation however is if you want to track letters such as 'a', 'w', '5', 'z', etc. - I can't see a method to calculate their keycodes. They all have keycodes, and they are being set in the array during the keyPressed() and keyReleased() methods but this is a one way process at the moment.
The missing part to help open up keyboard states would be to have a
int keyCode('a'); method so you could do checks such as:
Code:if(keysPressed[keyCode('q')]) {
...quit...
}
Anyone got any ideas/comments on this system?