keyboard.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict';
  2. require('../utils/dataTransfer/Clipboard.js');
  3. var getActiveElement = require('../utils/focus/getActiveElement.js');
  4. function _define_property(obj, key, value) {
  5. if (key in obj) {
  6. Object.defineProperty(obj, key, {
  7. value: value,
  8. enumerable: true,
  9. configurable: true,
  10. writable: true
  11. });
  12. } else {
  13. obj[key] = value;
  14. }
  15. return obj;
  16. }
  17. var DOM_KEY_LOCATION = /*#__PURE__*/ function(DOM_KEY_LOCATION) {
  18. DOM_KEY_LOCATION[DOM_KEY_LOCATION["STANDARD"] = 0] = "STANDARD";
  19. DOM_KEY_LOCATION[DOM_KEY_LOCATION["LEFT"] = 1] = "LEFT";
  20. DOM_KEY_LOCATION[DOM_KEY_LOCATION["RIGHT"] = 2] = "RIGHT";
  21. DOM_KEY_LOCATION[DOM_KEY_LOCATION["NUMPAD"] = 3] = "NUMPAD";
  22. return DOM_KEY_LOCATION;
  23. }({});
  24. const modifierKeys = [
  25. 'Alt',
  26. 'AltGraph',
  27. 'Control',
  28. 'Fn',
  29. 'Meta',
  30. 'Shift',
  31. 'Symbol'
  32. ];
  33. function isModifierKey(key) {
  34. return modifierKeys.includes(key);
  35. }
  36. const modifierLocks = [
  37. 'CapsLock',
  38. 'FnLock',
  39. 'NumLock',
  40. 'ScrollLock',
  41. 'SymbolLock'
  42. ];
  43. function isModifierLock(key) {
  44. return modifierLocks.includes(key);
  45. }
  46. class KeyboardHost {
  47. isKeyPressed(keyDef) {
  48. return this.pressed.has(String(keyDef.code));
  49. }
  50. getPressedKeys() {
  51. return this.pressed.values().map((p)=>p.keyDef);
  52. }
  53. /** Press a key */ async keydown(instance, keyDef) {
  54. const key = String(keyDef.key);
  55. const code = String(keyDef.code);
  56. const target = getActiveElement.getActiveElementOrBody(instance.config.document);
  57. this.setKeydownTarget(target);
  58. this.pressed.add(code, keyDef);
  59. if (isModifierKey(key)) {
  60. this.modifiers[key] = true;
  61. }
  62. const unprevented = instance.dispatchUIEvent(target, 'keydown', {
  63. key,
  64. code
  65. });
  66. if (isModifierLock(key) && !this.modifiers[key]) {
  67. this.modifiers[key] = true;
  68. this.modifierLockStart[key] = true;
  69. }
  70. if (unprevented) {
  71. this.pressed.setUnprevented(code);
  72. }
  73. if (unprevented && this.hasKeyPress(key)) {
  74. instance.dispatchUIEvent(getActiveElement.getActiveElementOrBody(instance.config.document), 'keypress', {
  75. key,
  76. code,
  77. charCode: keyDef.key === 'Enter' ? 13 : String(keyDef.key).charCodeAt(0)
  78. });
  79. }
  80. }
  81. /** Release a key */ async keyup(instance, keyDef) {
  82. const key = String(keyDef.key);
  83. const code = String(keyDef.code);
  84. const unprevented = this.pressed.isUnprevented(code);
  85. this.pressed.delete(code);
  86. if (isModifierKey(key) && !this.pressed.values().find((p)=>p.keyDef.key === key)) {
  87. this.modifiers[key] = false;
  88. }
  89. instance.dispatchUIEvent(getActiveElement.getActiveElementOrBody(instance.config.document), 'keyup', {
  90. key,
  91. code
  92. }, !unprevented);
  93. if (isModifierLock(key) && this.modifiers[key]) {
  94. if (this.modifierLockStart[key]) {
  95. this.modifierLockStart[key] = false;
  96. } else {
  97. this.modifiers[key] = false;
  98. }
  99. }
  100. }
  101. setKeydownTarget(target) {
  102. if (target !== this.lastKeydownTarget) {
  103. this.carryChar = '';
  104. }
  105. this.lastKeydownTarget = target;
  106. }
  107. hasKeyPress(key) {
  108. return (key.length === 1 || key === 'Enter') && !this.modifiers.Control && !this.modifiers.Alt;
  109. }
  110. constructor(system){
  111. _define_property(this, "system", undefined);
  112. _define_property(this, "modifiers", {
  113. Alt: false,
  114. AltGraph: false,
  115. CapsLock: false,
  116. Control: false,
  117. Fn: false,
  118. FnLock: false,
  119. Meta: false,
  120. NumLock: false,
  121. ScrollLock: false,
  122. Shift: false,
  123. Symbol: false,
  124. SymbolLock: false
  125. });
  126. _define_property(this, "pressed", new class {
  127. add(code, keyDef) {
  128. var _this_registry, _code;
  129. var _;
  130. (_ = (_this_registry = this.registry)[_code = code]) !== null && _ !== undefined ? _ : _this_registry[_code] = {
  131. keyDef,
  132. unpreventedDefault: false
  133. };
  134. }
  135. has(code) {
  136. return !!this.registry[code];
  137. }
  138. setUnprevented(code) {
  139. const o = this.registry[code];
  140. if (o) {
  141. o.unpreventedDefault = true;
  142. }
  143. }
  144. isUnprevented(code) {
  145. var _this_registry_code;
  146. return !!((_this_registry_code = this.registry[code]) === null || _this_registry_code === undefined ? undefined : _this_registry_code.unpreventedDefault);
  147. }
  148. delete(code) {
  149. // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
  150. delete this.registry[code];
  151. }
  152. values() {
  153. return Object.values(this.registry);
  154. }
  155. constructor(){
  156. _define_property(this, "registry", {});
  157. }
  158. }());
  159. _define_property(this, "carryChar", '');
  160. _define_property(this, "lastKeydownTarget", undefined);
  161. _define_property(this, "modifierLockStart", {});
  162. this.system = system;
  163. }
  164. }
  165. exports.DOM_KEY_LOCATION = DOM_KEY_LOCATION;
  166. exports.KeyboardHost = KeyboardHost;