Requires

Provides

Keyboard.Extras.js

Enhances Keyboard by adding the ability to name and describe keyboard shortcuts, and the ability to grab shortcuts by name and bind the shortcut to different keys.

License:
MIT-style license
Authors:
Perrin Westrich
  1. 24
  2. 25
  3. 26
Keyboard.prototype.options.nonParsedEvents.combine(['rebound', 'onrebound']); Keyboard.implement({

shortcut should be in the format of: { ‘keys’: ‘shift+s’, // the default to add as an event. ‘description’: ‘blah blah blah’, // a brief description of the functionality. ‘handler’: function(){} // the event handler to run when keys are pressed. }

  1. 36
  2. 37
  3. 38
  4. 39
  5. 40
  6. 41
  7. 42
  8. 43
  9. 44
  10. 45
  11. 46
  12. 47
  13. 48
  14. 49
  15. 50
  16. 51
  17. 52
  18. 53
  19. 54
  20. 55
  21. 56
  22. 57
  23. 58
  24. 59
  25. 60
  26. 61
  27. 62
  28. 63
  29. 64
  30. 65
  31. 66
  32. 67
  33. 68
  34. 69
  35. 70
  36. 71
  37. 72
  38. 73
  39. 74
  40. 75
  41. 76
  42. 77
  43. 78
  44. 79
  45. 80
  46. 81
  47. 82
  48. 83
  49. 84
  50. 85
  51. 86
  52. 87
  53. 88
  54. 89
  55. 90
  56. 91
  57. 92
  58. 93
  59. 94
  60. 95
  61. 96
  62. 97
  63. 98
  64. 99
  65. 100
  66. 101
  67. 102
  68. 103
  69. 104
  70. 105
  71. 106
  72. 107
  73. 108
  74. 109
  75. 110
addShortcut: function(name, shortcut){ this.shortcuts = this.shortcuts || []; this.shortcutIndex = this.shortcutIndex || {}; shortcut.getKeyboard = Function.from(this); shortcut.name = name; this.shortcutIndex[name] = shortcut; this.shortcuts.push(shortcut); if (shortcut.keys) this.addEvent(shortcut.keys, shortcut.handler); return this; }, addShortcuts: function(obj){ for (var name in obj) this.addShortcut(name, obj[name]); return this; }, removeShortcut: function(name){ var shortcut = this.getShortcut(name); if (shortcut && shortcut.keys){ this.removeEvent(shortcut.keys, shortcut.handler); delete this.shortcutIndex[name]; this.shortcuts.erase(shortcut); } return this; }, removeShortcuts: function(names){ names.each(this.removeShortcut, this); return this; }, getShortcuts: function(){ return this.shortcuts || []; }, getShortcut: function(name){ return (this.shortcutIndex || {})[name]; } }); Keyboard.rebind = function(newKeys, shortcuts){ Array.from(shortcuts).each(function(shortcut){ shortcut.getKeyboard().removeEvent(shortcut.keys, shortcut.handler); shortcut.getKeyboard().addEvent(newKeys, shortcut.handler); shortcut.keys = newKeys; shortcut.getKeyboard().fireEvent('rebound'); }); }; Keyboard.getActiveShortcuts = function(keyboard){ var activeKBS = [], activeSCS = []; Keyboard.each(keyboard, [].push.bind(activeKBS)); activeKBS.each(function(kb){ activeSCS.extend(kb.getShortcuts()); }); return activeSCS; }; Keyboard.getShortcut = function(name, keyboard, opts){ opts = opts || {}; var shortcuts = opts.many ? [] : null, set = opts.many ? function(kb){ var shortcut = kb.getShortcut(name); if (shortcut) shortcuts.push(shortcut); } : function(kb){ if (!shortcuts) shortcuts = kb.getShortcut(name); }; Keyboard.each(keyboard, set); return shortcuts; }; Keyboard.getShortcuts = function(name, keyboard){ return Keyboard.getShortcut(name, keyboard, { many: true }); };