diff --git a/docs/api.md b/docs/api.md index b9d8849365ca6..744863c97319f 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1100,7 +1100,7 @@ page.keyboard.press('Backspace'); Dispatches a `keydown` event. -This will not send input events unless `text` is specified. +If `key` is a single character and no modifier keys besides `Shift` are being held down, a `keypress`/`input` event will also generated. The `text` option can be specified to force an input event to be generated. 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). diff --git a/lib/Input.js b/lib/Input.js index 1e6cfb9bcf7cf..721660a647d04 100644 --- a/lib/Input.js +++ b/lib/Input.js @@ -30,8 +30,11 @@ class Keyboard { * @param {string} key * @param {{text: string}=} options */ - async down(key, options = {text: ''}) { - const {text} = options; + async down(key, options = {text: undefined}) { + let { text } = options; + // If the key is a single character, and no modifiers are pressed except shift, a keypress should be generated by default. + if (text === undefined) + text = (key.length === 1 && !(this._modifiers & ~8)) ? key : ''; const autoRepeat = this._pressedKeys.has(key); this._pressedKeys.add(key); this._modifiers |= this._modifierBit(key); diff --git a/test/test.js b/test/test.js index c27df75b8a39e..a4b5c05f0aeb7 100644 --- a/test/test.js +++ b/test/test.js @@ -1676,7 +1676,12 @@ describe('Page', function() { await keyboard.down(modifierKey); expect(await page.evaluate(() => getResult())).toBe('Keydown: ' + modifierKey + ' ' + modifierKey + 'Left ' + codeForKey[modifierKey] + ' [' + modifierKey + ']'); await keyboard.down('!'); - expect(await page.evaluate(() => getResult())).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']'); + // Shift+! will generate a keypress + if (modifierKey === 'Shift') + expect(await page.evaluate(() => getResult())).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']\nKeypress: ! Digit1 33 33 33 [' + modifierKey + ']'); + else + expect(await page.evaluate(() => getResult())).toBe('Keydown: ! Digit1 49 [' + modifierKey + ']'); + await keyboard.up('!'); expect(await page.evaluate(() => getResult())).toBe('Keyup: ! Digit1 49 [' + modifierKey + ']'); await keyboard.up(modifierKey);