2019-12-19 16:53:24 -08:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-08-24 06:51:51 -07:00
|
|
|
import * as input from '../input';
|
|
|
|
import * as types from '../types';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { CRSession } from './crConnection';
|
2020-08-24 06:51:51 -07:00
|
|
|
import { macEditingCommands } from '../macEditingCommands';
|
2020-08-22 15:46:42 -07:00
|
|
|
import { isString } from '../../utils/utils';
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
function toModifiersMask(modifiers: Set<types.KeyboardModifier>): number {
|
2019-12-19 16:53:24 -08:00
|
|
|
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 {
|
2020-07-21 16:33:46 -07:00
|
|
|
constructor(
|
|
|
|
private _client: CRSession,
|
|
|
|
private _isMac: boolean,
|
|
|
|
) { }
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-07-21 16:33:46 -07:00
|
|
|
_commandsForCode(code: string, modifiers: Set<types.KeyboardModifier>) {
|
|
|
|
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] || [];
|
2020-08-22 07:07:13 -07:00
|
|
|
if (isString(commands))
|
2020-07-21 16:33:46 -07:00
|
|
|
commands = [commands];
|
|
|
|
// remove the trailing : to match the Chromium command names.
|
|
|
|
return commands.map(c => c.substring(0, c.length - 1));
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async keydown(modifiers: Set<types.KeyboardModifier>, code: string, keyCode: number, keyCodeWithoutLocation: number, key: string, location: number, autoRepeat: boolean, text: string | undefined): Promise<void> {
|
2020-07-21 16:33:46 -07:00
|
|
|
const commands = this._commandsForCode(code, modifiers);
|
2019-12-19 16:53:24 -08:00
|
|
|
await this._client.send('Input.dispatchKeyEvent', {
|
|
|
|
type: text ? 'keyDown' : 'rawKeyDown',
|
|
|
|
modifiers: toModifiersMask(modifiers),
|
|
|
|
windowsVirtualKeyCode: keyCodeWithoutLocation,
|
|
|
|
code,
|
2020-07-21 16:33:46 -07:00
|
|
|
commands,
|
2019-12-19 16:53:24 -08:00
|
|
|
key,
|
|
|
|
text,
|
|
|
|
unmodifiedText: text,
|
|
|
|
autoRepeat,
|
|
|
|
location,
|
|
|
|
isKeypad: location === input.keypadLocation
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async keyup(modifiers: Set<types.KeyboardModifier>, code: string, keyCode: number, keyCodeWithoutLocation: number, key: string, location: number): Promise<void> {
|
2019-12-19 16:53:24 -08:00
|
|
|
await this._client.send('Input.dispatchKeyEvent', {
|
|
|
|
type: 'keyUp',
|
|
|
|
modifiers: toModifiersMask(modifiers),
|
|
|
|
key,
|
|
|
|
windowsVirtualKeyCode: keyCodeWithoutLocation,
|
|
|
|
code,
|
|
|
|
location
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendText(text: string): Promise<void> {
|
|
|
|
await this._client.send('Input.insertText', { text });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class RawMouseImpl implements input.RawMouse {
|
|
|
|
private _client: CRSession;
|
|
|
|
|
|
|
|
constructor(client: CRSession) {
|
|
|
|
this._client = client;
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async move(x: number, y: number, button: types.MouseButton | 'none', buttons: Set<types.MouseButton>, modifiers: Set<types.KeyboardModifier>): Promise<void> {
|
2019-12-19 16:53:24 -08:00
|
|
|
await this._client.send('Input.dispatchMouseEvent', {
|
|
|
|
type: 'mouseMoved',
|
|
|
|
button,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
modifiers: toModifiersMask(modifiers)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async down(x: number, y: number, button: types.MouseButton, buttons: Set<types.MouseButton>, modifiers: Set<types.KeyboardModifier>, clickCount: number): Promise<void> {
|
2019-12-19 16:53:24 -08:00
|
|
|
await this._client.send('Input.dispatchMouseEvent', {
|
|
|
|
type: 'mousePressed',
|
|
|
|
button,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
modifiers: toModifiersMask(modifiers),
|
|
|
|
clickCount
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-23 14:51:06 -07:00
|
|
|
async up(x: number, y: number, button: types.MouseButton, buttons: Set<types.MouseButton>, modifiers: Set<types.KeyboardModifier>, clickCount: number): Promise<void> {
|
2019-12-19 16:53:24 -08:00
|
|
|
await this._client.send('Input.dispatchMouseEvent', {
|
|
|
|
type: 'mouseReleased',
|
|
|
|
button,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
modifiers: toModifiersMask(modifiers),
|
|
|
|
clickCount
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|