handlePressKey function

Last updated: 2026-03-05T20:45:07.036Z

Metrics

LOC: 27 Complexity: 7 Params: 1

Signature

handlePressKey(step: FlowStep): : StepResult

Source Code

function handlePressKey(step: FlowStep): StepResult {
  if (!step.key) {
    return { status: "failed", error: "press-key step missing key" };
  }

  const target = step.selector ? findElement(step) : document.activeElement;
  if (!target) {
    return { status: "failed", error: "No target for key press" };
  }

  target.dispatchEvent(
    new KeyboardEvent("keydown", { key: step.key, bubbles: true }),
  );
  target.dispatchEvent(
    new KeyboardEvent("keyup", { key: step.key, bubbles: true }),
  );

  // Handle Enter on forms
  if (step.key === "Enter" && target instanceof HTMLElement) {
    const form = target.closest("form");
    if (form) {
      form.dispatchEvent(new Event("submit", { bubbles: true }));
    }
  }

  return { status: "success" };
}

Dependencies (Outgoing)

graph LR handlePressKey["handlePressKey"] findElement["findElement"] handlePressKey -->|calls| findElement style handlePressKey fill:#dbeafe,stroke:#2563eb,stroke-width:2px click handlePressKey "195a61c527b09c31.html" click findElement "db37f185eea489b4.html"
TargetType
findElement calls

Impact (Incoming)

graph LR handlePressKey["handlePressKey"] executeStep["executeStep"] executeStep -->|calls| handlePressKey style handlePressKey fill:#dbeafe,stroke:#2563eb,stroke-width:2px click handlePressKey "195a61c527b09c31.html" click executeStep "a26ccfb820921de2.html"
SourceType
executeStep calls