Skip to content

Commit ffa504b

Browse files
author
Mike Barkmin
committed
Allow two keys to be pressed simultaneously
1 parent 43e6ad1 commit ffa504b

3 files changed

Lines changed: 13 additions & 22 deletions

File tree

resources/code/doc.sh

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package eu.barkmin.processing.scratch;
2+
3+
public class ScratchAnimatedSprite {
4+
}

src/eu/barkmin/processing/scratch/ScratchStage.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package eu.barkmin.processing.scratch;
22

3+
import com.sun.org.apache.xpath.internal.operations.Bool;
34
import processing.core.PApplet;
45
import processing.core.PConstants;
56
import processing.core.PGraphics;
@@ -34,7 +35,7 @@ public class ScratchStage {
3435
private float mouseX;
3536
private float mouseY;
3637
private boolean mouseDown;
37-
private int keyCodePressed = -1;
38+
private HashMap<Integer, Boolean> keyCodePressed = new HashMap<>();
3839

3940
private ScratchStage(PApplet parent, boolean debug) {
4041
parent.imageMode(PConstants.CENTER);
@@ -448,10 +449,10 @@ public boolean isMouseDown() {
448449
public void keyEvent(KeyEvent e) {
449450
switch (e.getAction()) {
450451
case KeyEvent.PRESS:
451-
keyCodePressed = e.getKeyCode();
452+
keyCodePressed.put(e.getKeyCode(), true);
452453
break;
453454
case KeyEvent.RELEASE:
454-
keyCodePressed = -1;
455+
keyCodePressed.put(e.getKeyCode(), false);
455456
break;
456457
}
457458
}
@@ -463,7 +464,11 @@ public void keyEvent(KeyEvent e) {
463464
* @return key pressed
464465
*/
465466
public boolean isKeyPressed(int keyCode) {
466-
return keyCodePressed == keyCode;
467+
Boolean isPressed = keyCodePressed.get(keyCode);
468+
if (isPressed == null) {
469+
return false;
470+
}
471+
return isPressed;
467472
}
468473

469474
/**

0 commit comments

Comments
 (0)