ffInput.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.RawTouchscreenImpl = exports.RawMouseImpl = exports.RawKeyboardImpl = void 0;
  6. /**
  7. * Copyright 2017 Google Inc. All rights reserved.
  8. * Modifications copyright (c) Microsoft Corporation.
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the 'License');
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an 'AS IS' BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. */
  22. function toModifiersMask(modifiers) {
  23. let mask = 0;
  24. if (modifiers.has('Alt')) mask |= 1;
  25. if (modifiers.has('Control')) mask |= 2;
  26. if (modifiers.has('Shift')) mask |= 4;
  27. if (modifiers.has('Meta')) mask |= 8;
  28. return mask;
  29. }
  30. function toButtonNumber(button) {
  31. if (button === 'left') return 0;
  32. if (button === 'middle') return 1;
  33. if (button === 'right') return 2;
  34. return 0;
  35. }
  36. function toButtonsMask(buttons) {
  37. let mask = 0;
  38. if (buttons.has('left')) mask |= 1;
  39. if (buttons.has('right')) mask |= 2;
  40. if (buttons.has('middle')) mask |= 4;
  41. return mask;
  42. }
  43. class RawKeyboardImpl {
  44. constructor(client) {
  45. this._client = void 0;
  46. this._client = client;
  47. }
  48. async keydown(modifiers, code, keyCode, keyCodeWithoutLocation, key, location, autoRepeat, text) {
  49. // Firefox will figure out Enter by itself
  50. if (text === '\r') text = '';
  51. await this._client.send('Page.dispatchKeyEvent', {
  52. type: 'keydown',
  53. keyCode: keyCodeWithoutLocation,
  54. code,
  55. key,
  56. repeat: autoRepeat,
  57. location,
  58. text
  59. });
  60. }
  61. async keyup(modifiers, code, keyCode, keyCodeWithoutLocation, key, location) {
  62. await this._client.send('Page.dispatchKeyEvent', {
  63. type: 'keyup',
  64. key,
  65. keyCode: keyCodeWithoutLocation,
  66. code,
  67. location,
  68. repeat: false
  69. });
  70. }
  71. async sendText(text) {
  72. await this._client.send('Page.insertText', {
  73. text
  74. });
  75. }
  76. }
  77. exports.RawKeyboardImpl = RawKeyboardImpl;
  78. class RawMouseImpl {
  79. constructor(client) {
  80. this._client = void 0;
  81. this._page = void 0;
  82. this._client = client;
  83. }
  84. async move(x, y, button, buttons, modifiers, forClick) {
  85. await this._client.send('Page.dispatchMouseEvent', {
  86. type: 'mousemove',
  87. button: 0,
  88. buttons: toButtonsMask(buttons),
  89. x: Math.floor(x),
  90. y: Math.floor(y),
  91. modifiers: toModifiersMask(modifiers)
  92. });
  93. }
  94. async down(x, y, button, buttons, modifiers, clickCount) {
  95. await this._client.send('Page.dispatchMouseEvent', {
  96. type: 'mousedown',
  97. button: toButtonNumber(button),
  98. buttons: toButtonsMask(buttons),
  99. x: Math.floor(x),
  100. y: Math.floor(y),
  101. modifiers: toModifiersMask(modifiers),
  102. clickCount
  103. });
  104. }
  105. async up(x, y, button, buttons, modifiers, clickCount) {
  106. await this._client.send('Page.dispatchMouseEvent', {
  107. type: 'mouseup',
  108. button: toButtonNumber(button),
  109. buttons: toButtonsMask(buttons),
  110. x: Math.floor(x),
  111. y: Math.floor(y),
  112. modifiers: toModifiersMask(modifiers),
  113. clickCount
  114. });
  115. }
  116. async wheel(x, y, buttons, modifiers, deltaX, deltaY) {
  117. // Wheel events hit the compositor first, so wait one frame for it to be synced.
  118. await this._page.mainFrame().evaluateExpression(`new Promise(requestAnimationFrame)`, {
  119. world: 'utility'
  120. });
  121. await this._client.send('Page.dispatchWheelEvent', {
  122. deltaX,
  123. deltaY,
  124. x: Math.floor(x),
  125. y: Math.floor(y),
  126. deltaZ: 0,
  127. modifiers: toModifiersMask(modifiers)
  128. });
  129. }
  130. setPage(page) {
  131. this._page = page;
  132. }
  133. }
  134. exports.RawMouseImpl = RawMouseImpl;
  135. class RawTouchscreenImpl {
  136. constructor(client) {
  137. this._client = void 0;
  138. this._client = client;
  139. }
  140. async tap(x, y, modifiers) {
  141. await this._client.send('Page.dispatchTapEvent', {
  142. x,
  143. y,
  144. modifiers: toModifiersMask(modifiers)
  145. });
  146. }
  147. }
  148. exports.RawTouchscreenImpl = RawTouchscreenImpl;