not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.8 KiB

  1. define(function () {
  2. 'use strict';
  3. var shortcut = {};
  4. shortcut._allShortcuts = [];
  5. shortcut.add = function (modifiersString, key, callback) {
  6. // Parse the modifiers. For example "Ctrl+Alt" will become
  7. // {'ctrlKey': true, 'altKey': true, 'shiftKey': false}
  8. var modifiersList = modifiersString.toLowerCase().split("+");
  9. var modifiers = {
  10. 'ctrlKey': modifiersList.indexOf('ctrl') >= 0,
  11. 'altKey': modifiersList.indexOf('alt') >= 0,
  12. 'shiftKey': modifiersList.indexOf('shift') >= 0
  13. };
  14. this._allShortcuts.push({
  15. 'modifiers': modifiers,
  16. 'key': key.toLowerCase(),
  17. 'callback': callback
  18. });
  19. };
  20. document.onkeypress = function (e) {
  21. e = e || window.event;
  22. var modifiers = {
  23. 'ctrlKey': e.ctrlKey,
  24. 'altKey': e.altKey,
  25. 'shiftKey': e.shiftKey
  26. };
  27. // Obtain the key
  28. var charCode;
  29. if (typeof e.which == "number") {
  30. charCode = e.which;
  31. } else {
  32. charCode = e.keyCode;
  33. }
  34. var key = String.fromCharCode(charCode).toLowerCase();
  35. // Search for a matching shortcut
  36. for (var i = 0; i < shortcut._allShortcuts.length; i += 1) {
  37. var currentShortcut = shortcut._allShortcuts[i];
  38. var match = currentShortcut.key == key &&
  39. currentShortcut.modifiers.ctrlKey == modifiers.ctrlKey &&
  40. currentShortcut.modifiers.altKey == modifiers.altKey &&
  41. currentShortcut.modifiers.shiftKey == modifiers.shiftKey;
  42. if (match) {
  43. currentShortcut.callback();
  44. return;
  45. }
  46. }
  47. };
  48. return shortcut;
  49. });