{"version":3,"file":"chrome-extension/cdpInput.mjs","sources":["../../../src/chrome-extension/cdpInput.ts"],"sourcesContent":["// From https://github.com/puppeteer/puppeteer/blob/15abcc390862fd08cc3475532f2d9a11284aee6b/packages/puppeteer-core/src/cdp/Input.ts#L55\n// with some modifications to fit the session type\n/**\n * @license\n * Copyright 2017 Google Inc.\n * SPDX-License-Identifier: Apache-2.0\n */\n\nimport {\n  type KeyDefinition,\n  type KeyInput,\n  _keyDefinitions,\n} from '@midscene/shared/us-keyboard-layout';\nimport { assert } from '@midscene/shared/utils';\n\ntype KeyDescription = Required<\n  Pick<KeyDefinition, 'keyCode' | 'key' | 'text' | 'code' | 'location'>\n>;\n\n/**\n * @public\n */\nexport interface KeyDownOptions {\n  /**\n   * @deprecated Do not use. This is automatically handled.\n   */\n  text?: string;\n  /**\n   * @deprecated Do not use. This is automatically handled.\n   */\n  commands?: string[];\n}\n\n/**\n * @public\n */\nexport interface KeyboardTypeOptions {\n  /**\n   * Time to wait between key presses in milliseconds\n   * @default undefined\n   */\n  delay?: number;\n}\n\n/**\n * @public\n */\nexport type KeyPressOptions = KeyDownOptions & KeyboardTypeOptions;\n\ntype InternalCDPSession = {\n  send: (command: string, params: any) => Promise<void>;\n};\n\n/**\n * @internal\n */\nexport class CdpKeyboard {\n  #pressedKeys = new Set<string>();\n\n  #client: InternalCDPSession;\n\n  _modifiers = 0;\n\n  constructor(client: InternalCDPSession) {\n    this.#client = client;\n  }\n\n  updateClient(client: InternalCDPSession): void {\n    this.#client = client;\n  }\n\n  async down(\n    key: KeyInput,\n    options: Readonly<KeyDownOptions> = {\n      text: undefined,\n      commands: [],\n    },\n  ): Promise<void> {\n    const description = this.#keyDescriptionForString(key);\n\n    const autoRepeat = this.#pressedKeys.has(description.code);\n    this.#pressedKeys.add(description.code);\n    this._modifiers |= this.#modifierBit(description.key);\n\n    const text = options.text === undefined ? description.text : options.text;\n    await this.#client.send('Input.dispatchKeyEvent', {\n      type: text ? 'keyDown' : 'rawKeyDown',\n      modifiers: this._modifiers,\n      windowsVirtualKeyCode: description.keyCode,\n      code: description.code,\n      key: description.key,\n      text: text,\n      unmodifiedText: text,\n      autoRepeat,\n      location: description.location,\n      isKeypad: description.location === 3,\n      commands: options.commands,\n    });\n  }\n\n  #modifierBit(key: string): number {\n    if (key === 'Alt') {\n      return 1;\n    }\n    if (key === 'Control') {\n      return 2;\n    }\n    if (key === 'Meta') {\n      return 4;\n    }\n    if (key === 'Shift') {\n      return 8;\n    }\n    return 0;\n  }\n\n  #keyDescriptionForString(keyString: KeyInput): KeyDescription {\n    const shift = this._modifiers & 8;\n    const description = {\n      key: '',\n      keyCode: 0,\n      code: '',\n      text: '',\n      location: 0,\n    };\n\n    const definition = _keyDefinitions[keyString];\n\n    assert(definition, `Unknown key: \"${keyString}\"`);\n\n    if (definition.key) {\n      description.key = definition.key;\n    }\n    if (shift && definition.shiftKey) {\n      description.key = definition.shiftKey;\n    }\n\n    if (definition.keyCode) {\n      description.keyCode = definition.keyCode;\n    }\n    if (shift && definition.shiftKeyCode) {\n      description.keyCode = definition.shiftKeyCode;\n    }\n\n    if (definition.code) {\n      description.code = definition.code;\n    }\n\n    if (definition.location) {\n      description.location = definition.location;\n    }\n\n    if (description.key.length === 1) {\n      description.text = description.key;\n    }\n\n    if (definition.text) {\n      description.text = definition.text;\n    }\n    if (shift && definition.shiftText) {\n      description.text = definition.shiftText;\n    }\n\n    // if any modifiers besides shift are pressed, no text should be sent\n    if (this._modifiers & ~8) {\n      description.text = '';\n    }\n\n    return description;\n  }\n\n  async up(key: KeyInput): Promise<void> {\n    const description = this.#keyDescriptionForString(key);\n\n    this._modifiers &= ~this.#modifierBit(description.key);\n    this.#pressedKeys.delete(description.code);\n    await this.#client.send('Input.dispatchKeyEvent', {\n      type: 'keyUp',\n      modifiers: this._modifiers,\n      key: description.key,\n      windowsVirtualKeyCode: description.keyCode,\n      code: description.code,\n      location: description.location,\n    });\n  }\n\n  async sendCharacter(char: string): Promise<void> {\n    await this.#client.send('Input.insertText', { text: char });\n  }\n\n  private charIsKey(char: string): char is KeyInput {\n    return !!_keyDefinitions[char as KeyInput];\n  }\n\n  async type(\n    text: string,\n    options: Readonly<KeyboardTypeOptions> = {},\n  ): Promise<void> {\n    const delay = options.delay || undefined;\n    for (const char of text) {\n      if (this.charIsKey(char)) {\n        await this.press(char, { delay });\n      } else {\n        if (delay) {\n          await new Promise((f) => {\n            return setTimeout(f, delay);\n          });\n        }\n        await this.sendCharacter(char);\n      }\n    }\n  }\n\n  async press(\n    key: KeyInput | KeyInput[],\n    options: Readonly<KeyPressOptions> = {},\n  ): Promise<void> {\n    const { delay = null } = options;\n    const keys = Array.isArray(key) ? key : [key];\n\n    for (const k of keys) {\n      await this.down(k, options);\n    }\n\n    if (delay) {\n      await new Promise((f) => {\n        return setTimeout(f, options.delay);\n      });\n    }\n\n    for (const k of [...keys].reverse()) {\n      await this.up(k);\n    }\n  }\n}\n"],"names":["_pressedKeys","_client","modifierBit","keyDescriptionForString","CdpKeyboard","client","key","options","undefined","description","autoRepeat","text","char","_keyDefinitions","delay","Promise","f","setTimeout","keys","Array","k","Set","keyString","shift","definition","assert"],"mappings":";;;AAEA;;;;CAIC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAmDCA,eAAAA,WAAAA,GAAAA,IAAAA,WAEAC,UAAAA,WAAAA,GAAAA,IAAAA,WAyCAC,eAAAA,WAAAA,GAAAA,IAAAA,WAgBAC,2BAAAA,WAAAA,GAAAA,IAAAA;AA5DK,MAAMC;IAWX,aAAaC,MAA0B,EAAQ;uCACxCJ,SAAUI;IACjB;IAEA,MAAM,KACJC,GAAa,EACbC,UAAoC;QAClC,MAAMC;QACN,UAAU,EAAE;IACd,CAAC,EACc;QACf,MAAMC,cAAc,8BAAI,EAACN,0BAAAA,yBAAAA,IAAAA,CAAL,IAAI,EAA0BG;QAElD,MAAMI,aAAa,6BAAI,EAACV,cAAa,GAAG,CAACS,YAAY,IAAI;QACzD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