Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Add the ability to combine keyboard presses.
Browse files Browse the repository at this point in the history
This will split the string on the `+` separater in the `down` and `up`
methods.
  • Loading branch information
ankur22 committed Jun 21, 2022
1 parent 72b006d commit e65be16
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 43 deletions.
95 changes: 53 additions & 42 deletions common/keyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package common
import (
"context"
"fmt"
"strings"
"time"

"github.com/grafana/xk6-browser/api"
Expand Down Expand Up @@ -115,58 +116,68 @@ func (k *Keyboard) Type(text string, opts goja.Value) {
}
}

func (k *Keyboard) down(key string) error {
keyInput := keyboardlayout.KeyInput(key)
if _, ok := k.layout.ValidKeys[keyInput]; !ok {
return fmt.Errorf("%q is not a valid key for layout %q", key, k.layoutName)
}
// Can pass in more than one key as long as they are separated by a `+`.
func (k *Keyboard) down(keys string) error {
kk := strings.Split(keys, "+")

keyDef := k.keyDefinitionFromKey(keyInput)
k.modifiers |= k.modifierBitFromKeyName(keyDef.Key)
text := keyDef.Text
_, autoRepeat := k.pressedKeys[keyDef.KeyCode]
k.pressedKeys[keyDef.KeyCode] = true
for _, key := range kk {
keyInput := keyboardlayout.KeyInput(key)
if _, ok := k.layout.ValidKeys[keyInput]; !ok {
return fmt.Errorf("%q is not a valid key for layout %q", key, k.layoutName)
}

keyType := input.KeyDown
if text == "" {
keyType = input.KeyRawDown
}
keyDef := k.keyDefinitionFromKey(keyInput)
k.modifiers |= k.modifierBitFromKeyName(keyDef.Key)
text := keyDef.Text
_, autoRepeat := k.pressedKeys[keyDef.KeyCode]
k.pressedKeys[keyDef.KeyCode] = true

action := input.DispatchKeyEvent(keyType).
WithModifiers(input.Modifier(k.modifiers)).
WithKey(keyDef.Key).
WithWindowsVirtualKeyCode(keyDef.KeyCode).
WithCode(keyDef.Code).
WithLocation(keyDef.Location).
WithIsKeypad(keyDef.Location == 3).
WithText(text).
WithUnmodifiedText(text).
WithAutoRepeat(autoRepeat)
if err := action.Do(cdp.WithExecutor(k.ctx, k.session)); err != nil {
return fmt.Errorf("cannot execute dispatch key event down: %w", err)
keyType := input.KeyDown
if text == "" {
keyType = input.KeyRawDown
}

action := input.DispatchKeyEvent(keyType).
WithModifiers(input.Modifier(k.modifiers)).
WithKey(keyDef.Key).
WithWindowsVirtualKeyCode(keyDef.KeyCode).
WithCode(keyDef.Code).
WithLocation(keyDef.Location).
WithIsKeypad(keyDef.Location == 3).
WithText(text).
WithUnmodifiedText(text).
WithAutoRepeat(autoRepeat)
if err := action.Do(cdp.WithExecutor(k.ctx, k.session)); err != nil {
return fmt.Errorf("cannot execute dispatch key event down: %w", err)
}
}

return nil
}

func (k *Keyboard) up(key string) error {
keyInput := keyboardlayout.KeyInput(key)
if _, ok := k.layout.ValidKeys[keyInput]; !ok {
return fmt.Errorf("'%s' is not a valid key for layout '%s'", key, k.layoutName)
}
// Can pass in more than one key as long as they are separated by a `+`.
func (k *Keyboard) up(keys string) error {
kk := strings.Split(keys, "+")

for _, key := range kk {
keyInput := keyboardlayout.KeyInput(key)
if _, ok := k.layout.ValidKeys[keyInput]; !ok {
return fmt.Errorf("'%s' is not a valid key for layout '%s'", key, k.layoutName)
}

keyDef := k.keyDefinitionFromKey(keyInput)
k.modifiers &= ^k.modifierBitFromKeyName(keyDef.Key)
delete(k.pressedKeys, keyDef.KeyCode)
keyDef := k.keyDefinitionFromKey(keyInput)
k.modifiers &= ^k.modifierBitFromKeyName(keyDef.Key)
delete(k.pressedKeys, keyDef.KeyCode)

action := input.DispatchKeyEvent(input.KeyUp).
WithModifiers(input.Modifier(k.modifiers)).
WithKey(keyDef.Key).
WithWindowsVirtualKeyCode(keyDef.KeyCode).
WithCode(keyDef.Code).
WithLocation(keyDef.Location)
if err := action.Do(cdp.WithExecutor(k.ctx, k.session)); err != nil {
return fmt.Errorf("cannot execute dispatch key event up: %w", err)
action := input.DispatchKeyEvent(input.KeyUp).
WithModifiers(input.Modifier(k.modifiers)).
WithKey(keyDef.Key).
WithWindowsVirtualKeyCode(keyDef.KeyCode).
WithCode(keyDef.Code).
WithLocation(keyDef.Location)
if err := action.Do(cdp.WithExecutor(k.ctx, k.session)); err != nil {
return fmt.Errorf("cannot execute dispatch key event up: %w", err)
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion tests/keyboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestKeyboardPress(t *testing.T) {
})

t.Run("combo", func(t *testing.T) {
t.Skip("FIXME") // See https://github.com/grafana/xk6-browser/issues/285
// t.Skip("FIXME") // See https://github.com/grafana/xk6-browser/issues/285
p := tb.NewPage(nil)
cp, ok := p.(*common.Page)
require.True(t, ok)
Expand Down

0 comments on commit e65be16

Please sign in to comment.