All pastes #978606 Raw Edit

Miscellany

public text v1 · immutable
·published 2008-04-09 18:09 UTC
rendered paste body
/*
 * GLOBAL DATA FOR KEY CAPTURE
 */
float CAPTURE_TIMEOUT = 200;            // number of milliseconds that constitute asynchronous key presses

float startCap;                                         // timestamp that marks start of async key press detection
int [] keysPressed;                                     // keys that have been pressed since startCap w/in CAPTURE_TIMEOUT
int curKeyPressed;                                      // @deprecated; the number of elements in keysPressed

int [] upDownChord = {UP, DOWN};

/*
 * KEY CAPTURE FUNCTIONS
 */
/**
 * reset global variables used to manage detection of multiple key
 * presses w/in a time window dubbed as 'asynchronous'
 */
void cleanKeyPressedEnv() {
  if(DEBUG)  println("clentPressedEnv");
  startCap = 0.0;
  keysPressed = new int[0];
  curKeyPressed = 0;
}

/**
 * add a keyCode to the array which holds the list of keys that have * been pressed w/in the duration of time considered as 'asynchronous'
 *
 * @param int theKeyCode the key code to add to the list
 */
void appendKeyPressed(int theKeysCode) {
  if(DEBUG) println("appendKeyPressed; theKeysCode: " + theKeysCode);
    keysPressed = append(keysPressed, theKeysCode);
    curKeyPressed++;
}

/**
 * handle a match for a chord combo
 */
void handleChordComboMatch() {
        //// TODO: FILL THIS IN W/ ANYTHING YOU WANT TO DO WHEN THE USER
        //// HIT THE CORRECT KEY COMBO
}

/**
 * handle a failed chord key combo
 */
void handleChordComboMiss() {
        //// TODO: FILL THIS IN W/ ANYTHING YOU WANT TO DO WHEN THE USER
        //// BIFFS A KEY COMBO
}

/**
 * userspace function to to standard handling of a key press
 *
 * @param boolean isCoded is the key coded; see docs on CODED
 * @param char theKey the key that was pressed if visible; see docs on key
 * @param int theKeyCode the keyCode of the key that was pressed; see docs on keyCode
 */
void doStdKeyPressed(boolean isCoded, char theKey, int theKeyCode) {
  if(isCoded){
    if (theKeyCode == UP){
      if(DEBUG) println("up");
    } else if(theKeyCode == DOWN){
      if(DEBUG) println("down");
    } else if(theKeyCode == LEFT) {
      if(DEBUG) println("left");
    } else if(theKeyCode == RIGHT) {
      if(DEBUG) println("right");
    } else {
      if(DEBUG) println(theKeyCode);
    }
  }
}

/**
 * do standard keyReleased stuff
 */
void doStdKeyReleased(int theKeyCode) {
        /// TODO COMPLETE THIS METHOD W/ CRAP YOU WOULD NORMALLY
        /// DO IN keyReleased SUBSTITUTING keyCode BY theKeyCode
}

/**
 * any time a key is pressed this method is dispatched.
 * it will push the keycode into a global array that stores
 * all keys pressed within the time length considered to be
 * asynchronous.
 * it will reset this array once that threshold has been
 * breached.
 *
 * @param int theKeyCode the key code of the key thats just been pressed
 */
void handleCap(int theKeysCode) {
  float now = millis();

  if(DEBUG) println("now" + now);

  if(startCap == 0.0) {
    if(DEBUG) println("start cap");
    startCap = now;
  } else if((now - startCap) >= CAPTURE_TIMEOUT) {
    cleanKeyPressedEnv();
    startCap = now;
  }
  appendKeyPressed(theKeysCode);
}

/**
 * determine if a set of keys defined by keysToCheckFor
 * were pressed 'simultaneously'
 * @param int[] keysToCheck a set of key codes that have potentially been pressed 'simultaneously'
 */
boolean wereTogether(int [] keysToCheckFor) {
  if(DEBUG) println("wereTogether");
  boolean wereTogether = true;
  boolean matchFound = false;

  /// number of keys pressed must match number to check for
  if(keysToCheckFor.length != curKeyPressed) {
     wereTogether = false;
  } else {  /// look for correct match
    for(int i=0; i<keysToCheckFor.length; i++) {
      matchFound = false;
      for(int j=0; j<curKeyPressed; j++) {
        if(keysToCheckFor[i] == keysPressed[j]) {
            matchFound = true;
            break;
        }
      }
      if(!matchFound) {
        wereTogether = false;
        break;
      }
    }
  }

  cleanKeyPressedEnv();
  return wereTogether;
}




/*
 * ENVIRONMENTAL FUNCTIONS
 */
/**
 * this is the evn function to implement handling of a key presses
 */
void keyPressed() {
        handleCap(keyCode);
        doStdKeyPressed(key == CODED, key, keyCode);
}

/**
 * this is the env function to implement handling of key release
 */
void keyReleased() {
        doStdKeyReleased(keyCode);
   if(keyPressed == false) {
       if(wereTogether(upDownChord)) {                  /// TODO < CHANGE ME TO SOMEHOW BE THE CURRENT CHORDS KEY COMBO
                        if(DEBUG) println("chord match");
                   handleChordComboMatch();
       } else {
                   if(DEBUG) println("no match");
                        handleChordComboMiss();
       }
   }
}