Key Handler

// Register
util.addKeyHandler(13, 'down', demo.keyHandlerEnter);
util.addKeyHandler(27, 'down', demo.keyHandlerEsc);
util.addKeyHandler(37, 'down', demo.keyHandlerCursor);
util.addKeyHandler(38, 'down', demo.keyHandlerCursor);
util.addKeyHandler(39, 'down', demo.keyHandlerCursor);
util.addKeyHandler(40, 'down', demo.keyHandlerCursor);
util.addKeyHandler(65, 'down', demo.keyHandlerA);
util.addKeyHandler('B', 'down', demo.keyHandlerB);
util.addKeyHandler('S', 'down', demo.keyHandlerS, {ctrl: true});
util.addKeyHandler(32, 'up', demo.keyHandlerSP);


// Handlers

demo.keyHandlerEnter = function(e) {
  log('Key down: Enter');
};

demo.keyHandlerEsc = function(e) {
  log('Key down: Esc');
};

demo.keyHandlerCursor = function(e) {
  var s;
  switch (e.keyCode) {
    case 37:
      s = 'Left';
      break;
    case 38:
      s = 'Up';
      break;
    case 39:
      s = 'Right';
      break;
    case 40:
      s = 'Down';
      break;
    default:
      s = '?';
      break;
  }
  log('Key down: ' + s);
};

demo.keyHandlerA = function(e) {
  log('Key down: A');
};

demo.keyHandlerB = function(e) {
  log('Key down: B');
};

demo.keyHandlerS = function(e) {
  log('Key down: S');
  e.preventDefault();
};

demo.keyHandlerSP = function(e) {
  log('Key up: Space');
};