Key and coded key shift combo (cmd + something)

Below the basics of how I set it up. There are three functions to handle the special keys and a function to handle the normal key. Those functions return a boolean that indicates if the function handled the key or not.

It only shows some examples of possible keys / key combinations. Replace the print statements in the handleXXX functions by whatever needs to be done.

boolean hasAlt = false;
boolean hasCtrl = false;
boolean hasShift = false;

void setup()
{
  // give the window the focus
  surface.setVisible(true);
}

void keyPressed()
{
  //println(millis() + "\t[keyPressed()]keyCode = " + hex(keyCode) + ", key = " + hex(key));
  if (key == CODED)
  {
    switch(keyCode)
    {
    case ALT:
      hasAlt = true;
      break;
    case CONTROL:
      hasCtrl = true;
      break;
    case SHIFT:
      hasShift = true;
      break;
    default:
      break;
    }
  }
}

void keyReleased()
{
  //println(millis() + "\t[keyReleased()]keyCode = " + hex(keyCode) + ", key = " + hex(key));
  if (key == CODED)
  {
    switch(keyCode)
    {
    case ALT:
      hasAlt = false;
      break;
    case CONTROL:
      hasCtrl = false;
      break;
    case SHIFT:
      hasShift = false;
      break;
    }
  }
}


void draw()
{
  boolean result = false;

  if (keyPressed)
  {
    result = handleAlt();
    if (result == false) result = handleCtrl();
    if (result == false) result = handleShift();
    if (result == false) result= handleNormal();

    // for debugging only
    if (result == false)
    {
      println("key was not handled");
    }
  }
}

/*
Handle alt key
 Returns:
 true if the key combination was handled, ekse false
 */
boolean handleAlt()
{
  if (hasAlt == false)
  {
    return false;
  }
  if (key == 'a')
  {
    println("<alt>a");
    //key = 0;
    return true;
  }
  if (key == 'A')
  {
    println("<alt>A");
    //key = 0;
    return true;
  }
  if (keyCode == RIGHT)
  {
    println("<alt><right>");
    //keyCode = 0;
    return true;
  }

  return false;
}

/*
Handle ctrl key
 Returns:
 true if the key combination was handled, ekse false
 */
boolean handleCtrl()
{
  if (hasCtrl == false)
  {
    return false;
  }
  /*
  // you can use this for <ctrl>A
  if (key == 0x01)
   {
   println("<ctrl>a (using key)");
   key = 0;
   return true;
   }
   */
  // or you can use this for <ctrl>A
  if (keyCode == 0x41)
  {
    println("<ctrl>a (using keyCode)");
    //keyCode = 0;
    return true;
  }
  if (keyCode == LEFT)
  {
    println("<ctrl><left>");
    //keyCode = 0;
    return true;
  }
  // there is no defined constant for the home key
  //  so the numeric value has to be used
  if (keyCode == 0x24)
  {
    println("<ctrl><home>");
    //keyCode = 0;
    return true;
  }

  return false;
}

/*
Handle shift key
 Returns:
 true if the key combination was handled, ekse false
 */
boolean handleShift()
{
  if (hasShift == false)
  {
    return false;
  }
  return false;
}

/*
Handle normal key
 Returns:
 true if the key was handled, ekse false
 */
boolean handleNormal()
{
  if (hasAlt == true || hasCtrl == true)
  {
    return false;
  }

  if (key != CODED)
  {
    println("normal key = " + hex(key));
    return true;
  }

  return false;
}
1 Like