Skip to content

Commit

Permalink
feat(keyboard): make keyboard.down generate input event by default (#…
Browse files Browse the repository at this point in the history
…1016)

This patch starts generating input events for `keyboard.down`, addressing bullet 2 of
#723. With this patch, there's no longer any difference between `keboard.press('a')` and
`keyboard.press('a', {text: 'a'})`.

BREAKING CHANGE:
`keyboard.down('a')` starts generating input event (wasn't the case before).

References #723
  • Loading branch information
JoelEinbinder authored and aslushnikov committed Oct 13, 2017
1 parent a02347e commit 6a8865c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Expand Up @@ -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).

Expand Down
7 changes: 5 additions & 2 deletions lib/Input.js
Expand Up @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion test/test.js
Expand Up @@ -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);
Expand Down

0 comments on commit 6a8865c

Please sign in to comment.