Input.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /**
  2. * Copyright 2017 Google Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the 'License');
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an 'AS IS' BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. const {assert} = require('./helper');
  17. const keyDefinitions = require('./USKeyboardLayout');
  18. /**
  19. * @typedef {Object} KeyDescription
  20. * @property {number} keyCode
  21. * @property {string} key
  22. * @property {string} text
  23. * @property {string} code
  24. * @property {number} location
  25. */
  26. class Keyboard {
  27. /**
  28. * @param {!Puppeteer.CDPSession} client
  29. */
  30. constructor(client) {
  31. this._client = client;
  32. this._modifiers = 0;
  33. this._pressedKeys = new Set();
  34. }
  35. /**
  36. * @param {string} key
  37. * @param {{text?: string}=} options
  38. */
  39. async down(key, options = { text: undefined }) {
  40. const description = this._keyDescriptionForString(key);
  41. const autoRepeat = this._pressedKeys.has(description.code);
  42. this._pressedKeys.add(description.code);
  43. this._modifiers |= this._modifierBit(description.key);
  44. const text = options.text === undefined ? description.text : options.text;
  45. await this._client.send('Input.dispatchKeyEvent', {
  46. type: text ? 'keyDown' : 'rawKeyDown',
  47. modifiers: this._modifiers,
  48. windowsVirtualKeyCode: description.keyCode,
  49. code: description.code,
  50. key: description.key,
  51. text: text,
  52. unmodifiedText: text,
  53. autoRepeat,
  54. location: description.location,
  55. isKeypad: description.location === 3
  56. });
  57. }
  58. /**
  59. * @param {string} key
  60. * @return {number}
  61. */
  62. _modifierBit(key) {
  63. if (key === 'Alt')
  64. return 1;
  65. if (key === 'Control')
  66. return 2;
  67. if (key === 'Meta')
  68. return 4;
  69. if (key === 'Shift')
  70. return 8;
  71. return 0;
  72. }
  73. /**
  74. * @param {string} keyString
  75. * @return {KeyDescription}
  76. */
  77. _keyDescriptionForString(keyString) {
  78. const shift = this._modifiers & 8;
  79. const description = {
  80. key: '',
  81. keyCode: 0,
  82. code: '',
  83. text: '',
  84. location: 0
  85. };
  86. const definition = keyDefinitions[keyString];
  87. assert(definition, `Unknown key: "${keyString}"`);
  88. if (definition.key)
  89. description.key = definition.key;
  90. if (shift && definition.shiftKey)
  91. description.key = definition.shiftKey;
  92. if (definition.keyCode)
  93. description.keyCode = definition.keyCode;
  94. if (shift && definition.shiftKeyCode)
  95. description.keyCode = definition.shiftKeyCode;
  96. if (definition.code)
  97. description.code = definition.code;
  98. if (definition.location)
  99. description.location = definition.location;
  100. if (description.key.length === 1)
  101. description.text = description.key;
  102. if (definition.text)
  103. description.text = definition.text;
  104. if (shift && definition.shiftText)
  105. description.text = definition.shiftText;
  106. // if any modifiers besides shift are pressed, no text should be sent
  107. if (this._modifiers & ~8)
  108. description.text = '';
  109. return description;
  110. }
  111. /**
  112. * @param {string} key
  113. */
  114. async up(key) {
  115. const description = this._keyDescriptionForString(key);
  116. this._modifiers &= ~this._modifierBit(description.key);
  117. this._pressedKeys.delete(description.code);
  118. await this._client.send('Input.dispatchKeyEvent', {
  119. type: 'keyUp',
  120. modifiers: this._modifiers,
  121. key: description.key,
  122. windowsVirtualKeyCode: description.keyCode,
  123. code: description.code,
  124. location: description.location
  125. });
  126. }
  127. /**
  128. * @param {string} char
  129. */
  130. async sendCharacter(char) {
  131. await this._client.send('Input.insertText', {text: char});
  132. }
  133. /**
  134. * @param {string} text
  135. * @param {{delay: (number|undefined)}=} options
  136. */
  137. async type(text, options) {
  138. const delay = (options && options.delay) || null;
  139. for (const char of text) {
  140. if (keyDefinitions[char]) {
  141. await this.press(char, {delay});
  142. } else {
  143. if (delay)
  144. await new Promise(f => setTimeout(f, delay));
  145. await this.sendCharacter(char);
  146. }
  147. }
  148. }
  149. /**
  150. * @param {string} key
  151. * @param {!{delay?: number, text?: string}=} options
  152. */
  153. async press(key, options = {}) {
  154. const {delay = null} = options;
  155. await this.down(key, options);
  156. if (delay)
  157. await new Promise(f => setTimeout(f, options.delay));
  158. await this.up(key);
  159. }
  160. }
  161. class Mouse {
  162. /**
  163. * @param {Puppeteer.CDPSession} client
  164. * @param {!Keyboard} keyboard
  165. */
  166. constructor(client, keyboard) {
  167. this._client = client;
  168. this._keyboard = keyboard;
  169. this._x = 0;
  170. this._y = 0;
  171. /** @type {'none'|'left'|'right'|'middle'} */
  172. this._button = 'none';
  173. }
  174. /**
  175. * @param {number} x
  176. * @param {number} y
  177. * @param {!{steps?: number}=} options
  178. */
  179. async move(x, y, options = {}) {
  180. const {steps = 1} = options;
  181. const fromX = this._x, fromY = this._y;
  182. this._x = x;
  183. this._y = y;
  184. for (let i = 1; i <= steps; i++) {
  185. await this._client.send('Input.dispatchMouseEvent', {
  186. type: 'mouseMoved',
  187. button: this._button,
  188. x: fromX + (this._x - fromX) * (i / steps),
  189. y: fromY + (this._y - fromY) * (i / steps),
  190. modifiers: this._keyboard._modifiers
  191. });
  192. }
  193. }
  194. /**
  195. * @param {number} x
  196. * @param {number} y
  197. * @param {!{delay?: number, button?: "left"|"right"|"middle", clickCount?: number}=} options
  198. */
  199. async click(x, y, options = {}) {
  200. const {delay = null} = options;
  201. if (delay !== null) {
  202. await Promise.all([
  203. this.move(x, y),
  204. this.down(options),
  205. ]);
  206. await new Promise(f => setTimeout(f, delay));
  207. await this.up(options);
  208. } else {
  209. await Promise.all([
  210. this.move(x, y),
  211. this.down(options),
  212. this.up(options),
  213. ]);
  214. }
  215. }
  216. /**
  217. * @param {!{button?: "left"|"right"|"middle", clickCount?: number}=} options
  218. */
  219. async down(options = {}) {
  220. const {button = 'left', clickCount = 1} = options;
  221. this._button = button;
  222. await this._client.send('Input.dispatchMouseEvent', {
  223. type: 'mousePressed',
  224. button,
  225. x: this._x,
  226. y: this._y,
  227. modifiers: this._keyboard._modifiers,
  228. clickCount
  229. });
  230. }
  231. /**
  232. * @param {!{button?: "left"|"right"|"middle", clickCount?: number}=} options
  233. */
  234. async up(options = {}) {
  235. const {button = 'left', clickCount = 1} = options;
  236. this._button = 'none';
  237. await this._client.send('Input.dispatchMouseEvent', {
  238. type: 'mouseReleased',
  239. button,
  240. x: this._x,
  241. y: this._y,
  242. modifiers: this._keyboard._modifiers,
  243. clickCount
  244. });
  245. }
  246. }
  247. class Touchscreen {
  248. /**
  249. * @param {Puppeteer.CDPSession} client
  250. * @param {Keyboard} keyboard
  251. */
  252. constructor(client, keyboard) {
  253. this._client = client;
  254. this._keyboard = keyboard;
  255. }
  256. /**
  257. * @param {number} x
  258. * @param {number} y
  259. */
  260. async tap(x, y) {
  261. // Touches appear to be lost during the first frame after navigation.
  262. // This waits a frame before sending the tap.
  263. // @see https://crbug.com/613219
  264. await this._client.send('Runtime.evaluate', {
  265. expression: 'new Promise(x => requestAnimationFrame(() => requestAnimationFrame(x)))',
  266. awaitPromise: true
  267. });
  268. const touchPoints = [{x: Math.round(x), y: Math.round(y)}];
  269. await this._client.send('Input.dispatchTouchEvent', {
  270. type: 'touchStart',
  271. touchPoints,
  272. modifiers: this._keyboard._modifiers
  273. });
  274. await this._client.send('Input.dispatchTouchEvent', {
  275. type: 'touchEnd',
  276. touchPoints: [],
  277. modifiers: this._keyboard._modifiers
  278. });
  279. }
  280. }
  281. module.exports = { Keyboard, Mouse, Touchscreen};