{"record":{"id":"31852c6ba9e95647","repo":"grafana/k6","slug":"sending-key-down-w","errorCode":null,"errorMessage":"sending key down: %w","messagePattern":"sending key down: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/js/modules/k6/browser/common/keyboard.go","lineNumber":56,"sourceCode":"\tlayoutName    string // us by default\n\tlayout        keyboardlayout.KeyboardLayout\n}\n\n// NewKeyboard returns a new keyboard with a \"us\" layout.\nfunc NewKeyboard(ctx context.Context, s session) *Keyboard {\n\treturn &Keyboard{\n\t\tctx:         ctx,\n\t\tsession:     s,\n\t\tpressedKeys: make(map[int64]struct{}),\n\t\tlayoutName:  \"us\",\n\t\tlayout:      keyboardlayout.GetKeyboardLayout(\"us\"),\n\t}\n}\n\n// Down sends a key down message to a session target.\nfunc (k *Keyboard) Down(key string) error {\n\tif err := k.down(key); err != nil {\n\t\treturn fmt.Errorf(\"sending key down: %w\", err)\n\t}\n\treturn nil\n}\n\n// Up sends a key up message to a session target.\nfunc (k *Keyboard) Up(key string) error {\n\tif err := k.up(key); err != nil {\n\t\treturn fmt.Errorf(\"sending key up: %w\", err)\n\t}\n\treturn nil\n}\n\n// Press sends a key press message to a session target.\n// It delays the action if `Delay` option is specified.\n// A press message consists of successive key down and up messages.\nfunc (k *Keyboard) Press(key string, kbdOpts KeyboardOptions) error {\n\tif err := k.comboPress(key, kbdOpts); err != nil {\n\t\treturn fmt.Errorf(\"pressing key: %w\", err)","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/js/modules/k6/browser/common/keyboard.go#L38-L74","documentation":"Top-level wrapper returned by Keyboard.Down when the private k.down() fails. It aggregates two distinct failure modes: the key name is not valid for the active keyboard layout (validation error), or the CDP Input.dispatchKeyEvent(KeyDown) call fails (protocol/transport error).","triggerScenarios":"keyboard.down('Return') or keyboard.down('é') — names absent from the default 'us' layout's ValidKeys; keyboard.down('Enter') after the page or browser was closed, so the CDP session rejects the event.","commonSituations":"Using OS-specific or X11 key names (Return, Backspace aliases) instead of the browser's US-layout names; pressing keys after page.close(); target crashed mid-interaction.","solutions":["Use exact US-layout key names: 'Enter', 'Backspace', 'ArrowLeft', 'a', 'F1', single characters","Ensure the page and browser are still open before sending key events","For non-US characters use insertText() or type() which falls back to text insertion","Catch the error and check the inner message to distinguish invalid-key from transport failure"],"exampleFix":"// before\nawait page.keyboard.down('Return');\n\n// after\nawait page.keyboard.down('Enter');","handlingStrategy":"validation","validationCode":"const VALID = new Set(['Enter','Backspace','Delete','Tab','Escape','ArrowUp','ArrowDown','ArrowLeft','ArrowRight','Home','End','PageUp','PageDown','Control','Shift','Alt','Meta',' ']);\nfunction isValidKey(k) {\n  return VALID.has(k) || /^[a-zA-Z0-9]$/.test(k) || /^F([1-9]|1[0-2])$/.test(k);\n}\nif (!isValidKey(key)) throw new Error(`invalid key: ${key}`);","typeGuard":"function isValidKey(k) {\n  const VALID = new Set(['Enter','Backspace','Tab','Escape','ArrowUp','ArrowDown','ArrowLeft','ArrowRight','Home','End','PageUp','PageDown','Control','Shift','Alt','Meta']);\n  return VALID.has(k) || /^[a-zA-Z0-9]$/.test(k) || /^F([1-9]|1[0-2])$/.test(k);\n}","tryCatchPattern":"try {\n  await page.keyboard.down(key);\n} catch (e) {\n  if (/not a valid key/.test(e.message)) throw new Error(`bad key name: ${key} (use US-layout names like 'Enter')`);\n  throw e;\n}","preventionTips":["Keep a whitelist of US-layout key names and validate before calling","Never reuse OS-specific names (Return, BackTab)","For non-ASCII input use insertText()"],"tags":["browser","keyboard","cdp","input","validation"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}