Skip to content

Instantly share code, notes, and snippets.

@DanielBoerlage
Last active August 29, 2015 14:16
Show Gist options
  • Save DanielBoerlage/f44793e50b7a1a2eca6b to your computer and use it in GitHub Desktop.
Save DanielBoerlage/f44793e50b7a1a2eca6b to your computer and use it in GitHub Desktop.
Processing keyboard input
import java.awt.event.KeyListener;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import static java.awt.event.KeyEvent.*;
/*
* Keyboard input handler for processing
* Coded with care by Daniel Boerlage 2015
*
* example usage:
* if(keyboard.upArrowPressed()) {
* moveUp();
* }
* if(keyboard.onCharDown(' ')) {
* fireBullet();
* }
*/
class Keyboard implements KeyListener, FocusListener {
/*
* ArrayList of keyCodes to keep track of the current keys pressed
* keyCodes are just integers that java uses to keep track of keys on your keyboard
* We use Integer instead of int because you cant have an ArrayList of the primitive type int
* warning: most modern usb keyboards have a small key rollover
* For example keyboards with three-key rollover can reliably detect only any three keys used simultaneously
* So isDown may return false even when the key is actually being pressed if many other keys are also being pressed
*/
ArrayList<Integer> keysDown;
// Constructor: tells the sketch to send the keyboard class all key events like keyPressed() and focus events like focusGained()
Keyboard(PApplet parent) {
parent.addKeyListener(this);
parent.addFocusListener(this);
keysDown = new ArrayList<Integer>();
}
// General method to detect if a particular keyCode is down
boolean isKeyCodeDown(Integer code) {
return keysDown.contains(code);
}
/*
* Similar to isKeyCodeDown, but uses the key and then immediatley disposes it,
* This method is useful for example when shooting a single bullet;
* you dont want to shoot bullets every frame that the trigger key is held down in
* the arrayList remove function returns true if the original list contained the parameter
*/
boolean onKeyCodeDown(Integer code) {
return keysDown.remove(code);
}
/*
* returns true if the key corresponding to the character c is currently pressed down
* character literals must be wrapped in single quotes, for example 'a' or ' ' or even '\t' for tab
*/
boolean isCharDown(char c) {
return isKeyCodeDown(charToKeyCode(c));
}
// one-off version of isCharDown
boolean onCharDown(char c) {
return onKeyCodeDown(charToKeyCode(c));
}
/**
* create as many single key methods as necessary for keys that cant be represented by characters
* a few examples are below
* reference http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html for KeyCode constants
*/
boolean upArrowPressed() {
return isKeyCodeDown(VK_UP);
}
boolean downArrowPressed() {
return isKeyCodeDown(VK_DOWN);
}
boolean leftArrowPressed() {
return isKeyCodeDown(VK_LEFT);
}
boolean rightArrowPressed() {
return isKeyCodeDown(VK_RIGHT);
}
boolean shiftPressed() {
return isKeyCodeDown(VK_SHIFT);
}
boolean controlPressed() {
return isKeyCodeDown(VK_CONTROL);
}
// ...
/*
* KeyListener method that adds a keyCode to keysDown
* When a key is held down, the os sends multiple key pressed events like holding backspace to delete multiple words in a text editor
* Our program recieves these repeated events and shouldnt add them to the keysDown list because they would clutter the list with duplicate keyCodes
*/
void keyPressed(KeyEvent ev) {
if (!isKeyCodeDown(ev.getKeyCode()))
keysDown.add(ev.getKeyCode());
}
/*
* KeyListener method that removes a keyCode from keysDown
* we cast to int with the code (Integer) because if we dont the arraylist remove thinks we are using the integer as an index instead of an element
*/
void keyReleased(KeyEvent ev) {
keysDown.remove((Integer)ev.getKeyCode());
}
// KeyListener method - no use
void keyTyped(KeyEvent ev) { }
// FocusListener method - no use
void focusGained(FocusEvent ev) { }
// FocusListener method - without this defocusing and refocusing the sketch window breaks everything
void focusLost(FocusEvent ev) {
keysDown.clear();
}
// utility method to convert characters to their integer keyCode equivalent
Integer charToKeyCode(char c) {
return new Integer(Character.toUpperCase(c));
}
}
// the main instance of keyboard - consider renaming to something shorter like 'kbd'
Keyboard keyboard = new Keyboard(this);
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import static java.awt.event.KeyEvent.*;
/*
* Keyboard input handler for processing (core version)
* Coded with care by Daniel Boerlage 2015
*
* example usage:
* if(keyboard.upArrowPressed()) {
* moveUp();
* }
*/
class Keyboard extends KeyAdapter {
/*
* ArrayList of keyCodes to keep track of the current keys pressed
* keyCodes are just integers that java uses to keep track of keys on your keyboard
* We use Integer instead of int because you cant have an ArrayList of the primitive type int
* warning: most modern usb keyboards have a small key rollover
* For example keyboards with three-key rollover can reliably detect only any three keys used simultaneously
* So isDown may return false even when the key is actually being pressed if many other keys are also being pressed
*/
ArrayList<Integer> keysDown;
Keyboard(PApplet parent) {
parent.addKeyListener(this);
keysDown = new ArrayList<Integer>();
}
/*
* create as many single key methods as necessary
* a few examples are below
* reference http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html for KeyCode constants
*/
boolean aPressed() {
return keysDown.contains(VK_A);
}
boolean upArrowPressed() {
return keysDown.contains(VK_UP);
}
boolean shiftPressed() {
return keysDown.contains(VK_SHIFT);
}
void keyPressed(KeyEvent ev) {
if (!isKeyCodeDown(ev.getKeyCode()))
keysDown.add(ev.getKeyCode());
}
void keyReleased(KeyEvent ev) {
keysDown.remove((Integer)ev.getKeyCode());
}
}
// the main instance of keyboard - consider renaming to something shorter like 'kbd'
Keyboard keyboard = new Keyboard(this);
import java.awt.event.KeyListener;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.FocusEvent;
import static java.awt.event.KeyEvent.*;
/*
* Keyboard input handler for processing
* Coded with care by Daniel Boerlage 2015
*/
class Keyboard implements KeyListener, FocusListener {
ArrayList<Integer> keysDown;
Keyboard(PApplet parent) {
parent.addKeyListener(this);
parent.addFocusListener(this);
keysDown = new ArrayList<Integer>();
}
boolean isKeyCodeDown(Integer code) {
return keysDown.contains(code);
}
boolean onKeyCodeDown(Integer code) {
return keysDown.remove(code);
}
boolean isCharDown(char c) {
return isKeyCodeDown(charToKeyCode(c));
}
boolean onCharDown(char c) {
return onKeyCodeDown(charToKeyCode(c));
}
boolean upArrowPressed() {
return isKeyCodeDown(VK_UP);
}
boolean downArrowPressed() {
return isKeyCodeDown(VK_DOWN);
}
boolean leftArrowPressed() {
return isKeyCodeDown(VK_LEFT);
}
boolean rightArrowPressed() {
return isKeyCodeDown(VK_RIGHT);
}
boolean shiftPressed() {
return isKeyCodeDown(VK_SHIFT);
}
boolean controlPressed() {
return isKeyCodeDown(VK_CONTROL);
}
void keyPressed(KeyEvent ev) {
if (!isKeyCodeDown(ev.getKeyCode()))
keysDown.add(ev.getKeyCode());
}
void keyReleased(KeyEvent ev) {
keysDown.remove((Integer)ev.getKeyCode());
}
void keyTyped(KeyEvent ev) { }
void focusGained(FocusEvent ev) { }
void focusLost(FocusEvent ev) {
keysDown.clear();
}
Integer charToKeyCode(char c) {
return new Integer(Character.toUpperCase(c));
}
}
Keyboard keyboard = new Keyboard(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment