Specify repeat property on repeated keypresses (#172)

This patch implements 'autoRepeat' functionality for `keyboard.down`.
With this patch, the subsequent calls to `keyboard.down` would generate
an event with 'autoRepeat` flag set to true.

Closes #157
This commit is contained in:
JoelEinbinder 2017-07-31 12:05:46 -07:00 committed by Andrey Lushnikov
parent 08799dd839
commit bfc40b2ee6
3 changed files with 19 additions and 3 deletions

View File

@ -802,6 +802,8 @@ This will not send input events unless `text` is specified.
If `key` is a modifier key, `Shift`, `Meta`, `Control`, or `Alt`, subsequent key presses will be sent with that modifier active. To release the modifier key, use [`keyboard.up`](#keyboardupkey). If `key` is a modifier key, `Shift`, `Meta`, `Control`, or `Alt`, subsequent key presses will be sent with that modifier active. To release the modifier key, use [`keyboard.up`](#keyboardupkey).
After the key is pressed once, subsequent calls to [`keyboard.down`](#keyboarddownkey-options) will have [repeat](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/repeat) set to true. To release the key, use [`keyboard.up`](#keyboardupkey).
#### keyboard.sendCharacter(char) #### keyboard.sendCharacter(char)
- `char` <[string]> Character to send into the page. - `char` <[string]> Character to send into the page.
- returns: <[Promise]> - returns: <[Promise]>

View File

@ -23,15 +23,18 @@ class Keyboard {
constructor(client) { constructor(client) {
this._client = client; this._client = client;
this._modifiers = 0; this._modifiers = 0;
this._pressedKeys = new Set();
} }
/** /**
* @param {string} key * @param {string} key
* @param {{text: (string|undefined)}=} options * @param {{text: (string|undefined)}} options
* @return {!Promise} * @return {!Promise}
*/ */
async down(key, options) { async down(key, options) {
let { text } = options || {}; let {text} = options || {};
let autoRepeat = this._pressedKeys.has(key);
this._pressedKeys.add(key);
this._modifiers |= this._modifierBit(key); this._modifiers |= this._modifierBit(key);
await this._client.send('Input.dispatchKeyEvent', { await this._client.send('Input.dispatchKeyEvent', {
type: text ? 'keyDown' : 'rawKeyDown', type: text ? 'keyDown' : 'rawKeyDown',
@ -39,7 +42,8 @@ class Keyboard {
windowsVirtualKeyCode: codeForKey(key), windowsVirtualKeyCode: codeForKey(key),
key: key, key: key,
text: text, text: text,
unmodifiedText: text unmodifiedText: text,
autoRepeat
}); });
} }
@ -65,6 +69,7 @@ class Keyboard {
*/ */
async up(key) { async up(key) {
this._modifiers &= ~this._modifierBit(key); this._modifiers &= ~this._modifierBit(key);
this._pressedKeys.delete(key);
await this._client.send('Input.dispatchKeyEvent', { await this._client.send('Input.dispatchKeyEvent', {
type: 'keyUp', type: 'keyUp',
modifiers: this._modifiers, modifiers: this._modifiers,

View File

@ -1098,6 +1098,15 @@ describe('Puppeteer', function() {
fail(modifiers[modifier] + ' should be false'); fail(modifiers[modifier] + ' should be false');
} }
})); }));
it('should specify repeat property', SX(async function(){
await page.navigate(PREFIX + '/input/textarea.html');
await page.focus('textarea');
await page.$('textarea', textarea => textarea.addEventListener('keydown', e => window.lastEvent = e, true));
await page.keyboard.down('a', {text: 'a'});
expect(await page.evaluate(() => window.lastEvent.repeat)).toBe(false);
await page.press('a');
expect(await page.evaluate(() => window.lastEvent.repeat)).toBe(true);
}));
function dimensions() { function dimensions() {
let rect = document.querySelector('textarea').getBoundingClientRect(); let rect = document.querySelector('textarea').getBoundingClientRect();
return { return {