/** * Copyright 2017 Google Inc. All rights reserved. * Modifications copyright (c) Microsoft Corporation. * * Licensed under the Apache License, Version 2.0 (the 'License'); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an 'AS IS' BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import * as input from '../input'; import * as types from '../types'; import { CRSession } from './crConnection'; import { macEditingCommands } from '../macEditingCommands'; import { isString } from '../utils/utils'; function toModifiersMask(modifiers: Set): number { let mask = 0; if (modifiers.has('Alt')) mask |= 1; if (modifiers.has('Control')) mask |= 2; if (modifiers.has('Meta')) mask |= 4; if (modifiers.has('Shift')) mask |= 8; return mask; } export class RawKeyboardImpl implements input.RawKeyboard { constructor( private _client: CRSession, private _isMac: boolean, ) { } _commandsForCode(code: string, modifiers: Set) { if (!this._isMac) return []; const parts = []; for (const modifier of (['Shift', 'Control', 'Alt', 'Meta']) as types.KeyboardModifier[]) { if (modifiers.has(modifier)) parts.push(modifier); } parts.push(code); const shortcut = parts.join('+'); let commands = macEditingCommands[shortcut] || []; if (isString(commands)) commands = [commands]; // remove the trailing : to match the Chromium command names. return commands.map(c => c.substring(0, c.length - 1)); } async keydown(modifiers: Set, code: string, keyCode: number, keyCodeWithoutLocation: number, key: string, location: number, autoRepeat: boolean, text: string | undefined): Promise { const commands = this._commandsForCode(code, modifiers); await this._client.send('Input.dispatchKeyEvent', { type: text ? 'keyDown' : 'rawKeyDown', modifiers: toModifiersMask(modifiers), windowsVirtualKeyCode: keyCodeWithoutLocation, code, commands, key, text, unmodifiedText: text, autoRepeat, location, isKeypad: location === input.keypadLocation }); } async keyup(modifiers: Set, code: string, keyCode: number, keyCodeWithoutLocation: number, key: string, location: number): Promise { await this._client.send('Input.dispatchKeyEvent', { type: 'keyUp', modifiers: toModifiersMask(modifiers), key, windowsVirtualKeyCode: keyCodeWithoutLocation, code, location }); } async sendText(text: string): Promise { await this._client.send('Input.insertText', { text }); } } export class RawMouseImpl implements input.RawMouse { private _client: CRSession; constructor(client: CRSession) { this._client = client; } async move(x: number, y: number, button: types.MouseButton | 'none', buttons: Set, modifiers: Set): Promise { await this._client.send('Input.dispatchMouseEvent', { type: 'mouseMoved', button, x, y, modifiers: toModifiersMask(modifiers) }); } async down(x: number, y: number, button: types.MouseButton, buttons: Set, modifiers: Set, clickCount: number): Promise { await this._client.send('Input.dispatchMouseEvent', { type: 'mousePressed', button, x, y, modifiers: toModifiersMask(modifiers), clickCount }); } async up(x: number, y: number, button: types.MouseButton, buttons: Set, modifiers: Set, clickCount: number): Promise { await this._client.send('Input.dispatchMouseEvent', { type: 'mouseReleased', button, x, y, modifiers: toModifiersMask(modifiers), clickCount }); } }