grafana/k6 · error

dispatching key event up

Error message

dispatching key event up: %w

What it means

Wrap of the CDP Input.dispatchKeyEvent (keyUp) command inside Keyboard.up: layout validation already passed and the key was removed from the pressed-keys/modifier tracking, so this wrap indicates the protocol dispatch failed — the session or context was closed (page navigating, test iteration ending) at the moment the release event was sent.

Solutions

  1. Release keys before closing or navigating away from the page
  2. Use press() so down and up happen back-to-back within one live session
  3. Retry on a fresh page if the session died unexpectedly
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at internal/js/modules/k6/browser/common/keyboard.go:160 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of grafana/k6@01ffac6f24 (2026-08-18). Data as JSON: /api/errors/20cb7d6fc9b7e408. Report an issue: GitHub.

Appendix: source

Thrown at internal/js/modules/k6/browser/common/keyboard.go:160

	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)
	k.pressedKeysMu.Lock()
	delete(k.pressedKeys, keyDef.KeyCode)
	k.pressedKeysMu.Unlock()

	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("dispatching key event up: %w", err)
	}

	return nil
}

func (k *Keyboard) insertText(text string) error {
	action := input.InsertText(text)
	if err := action.Do(cdp.WithExecutor(k.ctx, k.session)); err != nil {
		return fmt.Errorf("inserting text: %w", err)
	}
	return nil
}

func (k *Keyboard) keyDefinitionFromKey(key keyboardlayout.KeyInput) keyboardlayout.KeyDefinition {
	shift := k.modifiers & ModifierKeyShift

	// Find directly from the keyboard layout
	srcKeyDef, ok := k.layout.Keys[key]

View on GitHub (pinned to 01ffac6f24)