1
0

key-event.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. (function() {
  2. var KeyEvent = function(data, type) {
  3. this.keyCode = 'keyCode' in data ? data.keyCode : 0;
  4. this.charCode = 'charCode' in data ? data.charCode : 0;
  5. var modifiers = 'modifiers' in data ? data.modifiers : [];
  6. this.ctrlKey = false;
  7. this.metaKey = false;
  8. this.altKey = false;
  9. this.shiftKey = false;
  10. for (var i = 0; i < modifiers.length; i++) {
  11. this[modifiers[i] + 'Key'] = true;
  12. }
  13. this.type = type || 'keypress';
  14. };
  15. KeyEvent.prototype.toNative = function() {
  16. var event = document.createEventObject ? document.createEventObject() : document.createEvent('Events');
  17. if (event.initEvent) {
  18. event.initEvent(this.type, true, true);
  19. }
  20. event.keyCode = this.keyCode;
  21. event.which = this.charCode || this.keyCode;
  22. event.shiftKey = this.shiftKey;
  23. event.metaKey = this.metaKey;
  24. event.altKey = this.altKey;
  25. event.ctrlKey = this.ctrlKey;
  26. return event;
  27. };
  28. KeyEvent.prototype.fire = function(element) {
  29. var event = this.toNative();
  30. if (element.dispatchEvent) {
  31. element.dispatchEvent(event);
  32. return;
  33. }
  34. element.fireEvent('on' + this.type, event);
  35. };
  36. // simulates complete key event as if the user pressed the key in the browser
  37. // triggers a keydown, then a keypress, then a keyup
  38. KeyEvent.simulate = function(charCode, keyCode, modifiers, element, repeat) {
  39. if (modifiers === undefined) {
  40. modifiers = [];
  41. }
  42. if (element === undefined) {
  43. element = document;
  44. }
  45. if (repeat === undefined) {
  46. repeat = 1;
  47. }
  48. var modifierToKeyCode = {
  49. 'shift': 16,
  50. 'ctrl': 17,
  51. 'alt': 18,
  52. 'meta': 91
  53. };
  54. // if the key is a modifier then take it out of the regular
  55. // keypress/keydown
  56. if (keyCode == 16 || keyCode == 17 || keyCode == 18 || keyCode == 91) {
  57. repeat = 0;
  58. }
  59. var modifiersToInclude = [];
  60. var keyEvents = [];
  61. // modifiers would go down first
  62. for (var i = 0; i < modifiers.length; i++) {
  63. modifiersToInclude.push(modifiers[i]);
  64. keyEvents.push(new KeyEvent({
  65. charCode: 0,
  66. keyCode: modifierToKeyCode[modifiers[i]],
  67. modifiers: modifiersToInclude
  68. }, 'keydown'));
  69. }
  70. // @todo factor in duration for these
  71. while (repeat > 0) {
  72. keyEvents.push(new KeyEvent({
  73. charCode: 0,
  74. keyCode: keyCode,
  75. modifiers: modifiersToInclude
  76. }, 'keydown'));
  77. keyEvents.push(new KeyEvent({
  78. charCode: charCode,
  79. keyCode: charCode,
  80. modifiers: modifiersToInclude
  81. }, 'keypress'));
  82. repeat--;
  83. }
  84. keyEvents.push(new KeyEvent({
  85. charCode: 0,
  86. keyCode: keyCode,
  87. modifiers: modifiersToInclude
  88. }, 'keyup'));
  89. // now lift up the modifier keys
  90. for (i = 0; i < modifiersToInclude.length; i++) {
  91. var modifierKeyCode = modifierToKeyCode[modifiersToInclude[i]];
  92. modifiersToInclude.splice(i, 1);
  93. keyEvents.push(new KeyEvent({
  94. charCode: 0,
  95. keyCode: modifierKeyCode,
  96. modifiers: modifiersToInclude
  97. }, 'keyup'));
  98. }
  99. for (i = 0; i < keyEvents.length; i++) {
  100. // console.log('firing', keyEvents[i].type, keyEvents[i].keyCode, keyEvents[i].charCode);
  101. keyEvents[i].fire(element);
  102. }
  103. };
  104. window.KeyEvent = KeyEvent;
  105. }) ();