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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as input from '../input';
|
2020-06-23 14:51:06 -07:00
|
|
|
import * as types from '../types';
|
2019-12-19 16:53:24 -08:00
|
|
|
import { helper } from '../helper';
|
2020-04-11 00:24:17 -07:00
|
|
|
import { macEditingCommands } from '../macEditingCommands';
|
2020-01-09 15:14:35 -08:00
|
|
|
import { WKSession } from './wkConnection';
|
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
|
|
|
// From Source/WebKit/Shared/WebEvent.h
|
|
|
|
let mask = 0;
|
|
|
|
if (modifiers.has('Shift'))
|
|
|
|
mask |= 1;
|
|
|
|
if (modifiers.has('Control'))
|
|
|
|
mask |= 2;
|
|
|
|
if (modifiers.has('Alt'))
|
|
|
|
mask |= 4;
|
|
|
|
if (modifiers.has('Meta'))
|
|
|
|
mask |= 8;
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class RawKeyboardImpl implements input.RawKeyboard {
|
2020-01-09 15:14:35 -08:00
|
|
|
private readonly _pageProxySession: WKSession;
|
2020-01-13 13:33:25 -08:00
|
|
|
private _session?: WKSession;
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-01-09 15:14:35 -08:00
|
|
|
constructor(session: WKSession) {
|
2020-01-07 17:16:27 -08:00
|
|
|
this._pageProxySession = session;
|
|
|
|
}
|
|
|
|
|
2020-01-09 11:02:55 -08:00
|
|
|
setSession(session: WKSession) {
|
2019-12-19 16:53:24 -08:00
|
|
|
this._session = session;
|
|
|
|
}
|
|
|
|
|
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> {
|
2019-12-19 16:53:24 -08:00
|
|
|
const parts = [];
|
2020-06-23 14:51:06 -07:00
|
|
|
for (const modifier of (['Shift', 'Control', 'Alt', 'Meta']) as types.KeyboardModifier[]) {
|
2019-12-19 16:53:24 -08:00
|
|
|
if (modifiers.has(modifier))
|
|
|
|
parts.push(modifier);
|
|
|
|
}
|
|
|
|
parts.push(code);
|
|
|
|
const shortcut = parts.join('+');
|
|
|
|
let commands = macEditingCommands[shortcut];
|
|
|
|
if (helper.isString(commands))
|
|
|
|
commands = [commands];
|
2020-01-07 17:16:27 -08:00
|
|
|
await this._pageProxySession.send('Input.dispatchKeyEvent', {
|
2019-12-19 16:53:24 -08:00
|
|
|
type: 'keyDown',
|
|
|
|
modifiers: toModifiersMask(modifiers),
|
|
|
|
windowsVirtualKeyCode: keyCode,
|
|
|
|
code,
|
|
|
|
key,
|
|
|
|
text,
|
|
|
|
unmodifiedText: text,
|
|
|
|
autoRepeat,
|
|
|
|
macCommands: commands,
|
|
|
|
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> {
|
2020-01-07 17:16:27 -08:00
|
|
|
await this._pageProxySession.send('Input.dispatchKeyEvent', {
|
2019-12-19 16:53:24 -08:00
|
|
|
type: 'keyUp',
|
|
|
|
modifiers: toModifiersMask(modifiers),
|
|
|
|
key,
|
|
|
|
windowsVirtualKeyCode: keyCode,
|
|
|
|
code,
|
|
|
|
isKeypad: location === input.keypadLocation
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendText(text: string): Promise<void> {
|
2020-01-13 13:33:25 -08:00
|
|
|
await this._session!.send('Page.insertText', { text });
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class RawMouseImpl implements input.RawMouse {
|
2020-01-09 15:14:35 -08:00
|
|
|
private readonly _pageProxySession: WKSession;
|
2019-12-19 16:53:24 -08:00
|
|
|
|
2020-01-09 15:14:35 -08:00
|
|
|
constructor(session: WKSession) {
|
2020-01-07 17:16:27 -08:00
|
|
|
this._pageProxySession = session;
|
2019-12-19 16:53:24 -08:00
|
|
|
}
|
|
|
|
|
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> {
|
2020-01-07 17:16:27 -08:00
|
|
|
await this._pageProxySession.send('Input.dispatchMouseEvent', {
|
2019-12-19 16:53:24 -08:00
|
|
|
type: 'move',
|
|
|
|
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> {
|
2020-01-07 17:16:27 -08:00
|
|
|
await this._pageProxySession.send('Input.dispatchMouseEvent', {
|
2019-12-19 16:53:24 -08:00
|
|
|
type: 'down',
|
|
|
|
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> {
|
2020-01-07 17:16:27 -08:00
|
|
|
await this._pageProxySession.send('Input.dispatchMouseEvent', {
|
2019-12-19 16:53:24 -08:00
|
|
|
type: 'up',
|
|
|
|
button,
|
|
|
|
x,
|
|
|
|
y,
|
|
|
|
modifiers: toModifiersMask(modifiers),
|
|
|
|
clickCount
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|