Created
April 15, 2018 17:48
-
-
Save shinyaoguri/8057e67e1188e887c0506685b691b9c1 to your computer and use it in GitHub Desktop.
Processingで複数のキーが押されたときの挙動
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class KeyState{ | |
| HashMap<Integer, Boolean> Key; | |
| KeyState(){ | |
| this.Initialize(); | |
| } | |
| void Initialize(){ | |
| this.Key = new HashMap<Integer, Boolean>(); | |
| this.Key.put(RIGHT, false); | |
| this.Key.put(LEFT, false); | |
| this.Key.put(UP, false); | |
| this.Key.put(DOWN, false); | |
| this.Key.put(ALT, false); | |
| this.Key.put(SHIFT, false); | |
| this.Key.put(CONTROL, false); | |
| } | |
| boolean getState(int code){ | |
| return this.Key.get(code); | |
| } | |
| void setState(int code, boolean state){ | |
| this.Key.put(code, state); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| KeyState keyState; | |
| PImage img; | |
| PFont f; | |
| void setup() { | |
| keyState = new KeyState(); | |
| keyState.Initialize(); | |
| size(1600,900, P3D); | |
| f = createFont("Avenir-Light-48.vlw", 48); | |
| textFont(f); | |
| textAlign(CENTER, CENTER); | |
| } | |
| void draw(){ | |
| background(0); | |
| if(keyState.getState(RIGHT) == true) { | |
| text('R', width/2, height/2); | |
| } | |
| if(keyState.getState(LEFT) == true) { | |
| text('L', width/2, height/2); | |
| } | |
| if(keyState.getState(DOWN) == true) { | |
| text('D', width/2, height/2); | |
| } | |
| if(keyState.getState(UP) == true) { | |
| text('U', width/2, height/2); | |
| } | |
| if(keyState.getState(SHIFT) == true) { | |
| text('S', width/2, height/2); | |
| } | |
| image(canvas, 0, 0); | |
| server.sendImage(canvas); | |
| } | |
| void keyPressed(){ | |
| keyState.setState(keyCode, true); | |
| } | |
| void keyReleased(){ | |
| keyState.setState(keyCode, false); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment