keyboard.js 5.4 KB

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