Multiple key presses

From Processing

Jump to: navigation, search
Versions: 1.0 (not compatible with 2.0, needs update)
Contributors: seltar, analogAI, jeffg
Started: 2006-02-28


These are small handlers for checking the status of multiple keys.

Source Code

Option 1

/**
multiplekeys taken from http://wiki.processing.org/index.php?title=Keep_track_of_multiple_key_presses
@author Yonas Sandbæk http://seltar.wliia.org
*/
import  java.awt.event.KeyEvent;
 
// usage: 
// if (checkKey("ctrl") && checkKey("s")) println("CTRL+S");  
 
// Highest key on most keyboards is VK_CONTEXT_MENU (code 0x20D), the context menu key on Windows keyboards
boolean[] keys = new boolean[0x20E];
void draw() {}
 
boolean checkKey(String k)
{
  for (int i = 0; i < keys.length; i++)
    if (KeyEvent.getKeyText(i).toLowerCase().equals(k.toLowerCase())) return keys[i];  
  return false;
}
 
void keyPressed()
{ 
  keys[keyCode] = true;
//  println(KeyEvent.getKeyText(keyCode));
}
 
void keyReleased()
{ 
  keys[keyCode] = false; 
}

And a simpler way of doing this just checking a few keys (without an array)

boolean keyup = false;
boolean keyright = false;
boolean keyleft = false;
boolean keydown = false;
 
float x, y;
 
void setup() {
  size(640, 360);  
  x = width / 2;
  y = height / 2;
}
 
void draw() {
  background(51);
  fill(255);
  ellipse(x, y, 16, 16);
 
  if (keyup) y--;
  if (keydown) y++;
  if (keyleft) x--;
  if (keyright) x++;
}
 
void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) keyup = true; 
    if (keyCode == DOWN) keydown = true; 
    if (keyCode == LEFT) keyleft = true; 
    if (keyCode == RIGHT) keyright = true; 
  }
}
 
void keyReleased() {
  if (key == CODED) {
    if (keyCode == UP) keyup = false; 
    if (keyCode == DOWN) keydown = false; 
    if (keyCode == LEFT) keyleft = false; 
    if (keyCode == RIGHT) keyright = false; 
  }
}

Option 2

/**
Modified version of Option 1 multiplekeys (should provide improved performance and accuracy)
@author Yonas Sandbæk http://seltar.wliia.org (modified by jeffg)
*/
 
// usage: 
// if (checkKey(KeyEvent.VK_CONTROL) && checkKey(KeyEvent.VK_S)) println("CTRL+S");  
 
boolean[] keys = new boolean[0x20E]; // See above for this value
 
void draw() {}
 
boolean checkKey(int k)
{
  if (keys.length >= k) {
    return keys[k];  
  }
  return false;
}
 
void keyPressed()
{ 
  keys[keyCode] = true;
}
 
void keyReleased()
{ 
  keys[keyCode] = false; 
}

Option 3

  /**
   * Snappier multiple key detection using Primitive Collections Classes for Java http://pcj.sourceforge.net/
   * (standard Java Collection Objects can be used instead)
   * @author analogAI http://recursivepath.com/analogAI/
   */
 
   // usage:
   // if (checkKeysHeld(KeyEvent.VK_CONTROL) && checkKeysHeld(KeyEvent.VK_S)) println("CTRL+S");  
 
import bak.pcj.set.IntSet;
import bak.pcj.set.IntOpenHashSet;
import java.awt.event.KeyEvent;
 
public class HelloWorld extends PApplet {
 
  public IntSet keysheld = new IntOpenHashSet();
  /**
   * @param keycode key integer code, the value are constants defined in KeyEvent Class
   *                http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/KeyEvent.html
   *                e.g. KeyEvent.VK_A for letter A
   *                     KeyEvent.VK_0 for number 0
   *                     KeyEvent.VK_SHIFT for shift button
   * @return true if the key is currently held down, false otherwise
   */
 
  public boolean checkKeysHeld(int keycode) {
    return keysheld.contains(keycode);
  }
 
  public void keyPressed(){
    // add key to the list of keys held down
    // with Processing, the KeyEvent object is always available as "keyEvent",
    // the getKeyChar() is already in the variable 'key', and getKeyCode() is in the variable 'keyCode'.
    keysheld.add(keyEvent.getKeyCode());
    println("key pressed: " + KeyEvent.getKeyText(keyEvent.getKeyCode()));
    println("keys in current held list: " + keysheld.toString());
  }
 
  public void keyReleased() {
    // remove key from the list of keys held down
    keysheld.remove(keyEvent.getKeyCode());
  }
}

Downloads

Related Links

Personal tools