Skip to content

Instantly share code, notes, and snippets.

@falkirks
Last active August 29, 2015 13:59
Show Gist options
  • Save falkirks/10937029 to your computer and use it in GitHub Desktop.
Save falkirks/10937029 to your computer and use it in GitHub Desktop.
Check for multiple key presses in Java Processing.
class bindKey {
IntList keylist;
public bindKey() {
keylist = new IntList();
}
public void pressKey(int key) {
if (!keylist.hasValue(key)) keylist.append(key);
}
public void releaseKey(int key) {
if (keylist.hasValue(key)) {
for (int i = 0; i < keylist.size(); i++) {
if (keylist.get(i) == key) {
keylist.remove(i);
return;
}
}
}
}
public Boolean isPressed(int check){
return keylist.hasValue(check);
}
}
bindKey keys;
void setup(){
size(800,600);
keys = new bindKey();
}
void draw(){
if(keys.isPressed(UP) && keys.isPressed(DOWN)) background(0);
else background(255);
}
void keyPressed(KeyEvent evt){
keys.pressKey(evt.getKeyCode());
}
void keyReleased(KeyEvent evt){
keys.releaseKey(evt.getKeyCode());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment