Scope Keyboard Shortcuts Around Focus and Composition

Drafted and reviewed using AI assistance.

On this page

Describe a shortcut by the work it may interrupt. A key combination that previews a note should not also accept an input-method candidate, change an unrelated toolbar action or fire repeatedly while someone holds the key down. Give the coding agent a focus map, a composition policy and a precise action boundary before asking it to add the listener.

The fictional Cove Review editor below has one shortcut: Control+Enter previews the current note. Preview means updating a local panel; it does not send, publish or save anything. The example is an authored interaction contract, not a recording of a browser defect.

Name the three places focus can be

The page has a multiline note editor, a Preview button and an unrelated page background. Keep the visible button available so the task does not depend on a keyboard shortcut.

Focus location Object Who owns ordinary keyboard behavior?
E · Editor note-draft, a textarea Text editing and the active input method
T · Toolbar preview-note, a native button The focused button's normal activation
B · Background Page body or another non-editor location That location; the app does not borrow it for this shortcut

MDN's keydown reference explains that key events target the focused element and can bubble. A page-level listener therefore needs an explicit scope decision. Receiving an editor event at the document does not make every document event an editor command.

For this case, a preview is available only when the note contains non-whitespace text. Its contents are copied exactly into the local preview; the source draft and focus stay where they were. Whitespace is used only to decide whether the action is available, not to trim a valid note.

Make the combination exact

The proposed combination is Control+Enter: Control down, with Shift, Alt/Option and Meta/Command all up. This is the example's chosen binding, not a claim that the combination is free in every keyboard setup.

Apply these rules in order:

  1. If a nested control already handled the event, leave it alone.
  2. If focus is outside note-draft, do not run the shortcut.
  3. If the gesture belongs to composition or its commit boundary, leave it to text input.
  4. If any modifier differs from the exact combination, do not run the shortcut.
  5. If the key is held or the same physical press already triggered preview, do not preview again.
  6. If the preview is unavailable because the note is empty, do not preview.
  7. Otherwise, preview once on the eligible keydown. Cancel that handled shortcut's default action so it does not also edit the note.

Ignored events are not canceled by this shortcut handler. In particular, ordinary Enter remains available to the editor and input method. Native activation of the visible Preview button is a separate path to the same preview action.

Separate composition from a fresh command

KeyboardEvent.isComposing describes an event's position within a composition session. It is a useful signal, but a single false value is not enough to classify every edge event. MDN's keydown guidance discusses first and last composition keystrokes whose event ordering can put them outside that interval.

Use three meaningful phases in the request:

  • Ready before composition: no composition or unfinished commit gesture is active.
  • Composing or committing: candidate selection, confirmation and its boundary gesture belong to input.
  • Ready after composition: the commit gesture has been released; the user starts a new command press.

“After” does not mean the very next keydown after a compositionend notification. It means a separate, eligible user gesture. The implementation needs to distinguish those states on the selected browser/input-method pair. Do not paper over an uncertain boundary with a guessed delay that might swallow the next intentional shortcut.

Here is the full three-by-three policy matrix. Every cell assumes the exact shortcut combination, a nonempty note, an unhandled event and a fresh non-repeating press where the phase permits one.

Focus Ready before composition Composing or committing Ready after composition
E · Editor Preview once; keep editor focus No shortcut action; preserve text-input handling Preview once on the new press; keep editor focus
T · Toolbar No shortcut action No shortcut action No shortcut action
B · Background No shortcut action No shortcut action No shortcut action

For the toolbar row, “no shortcut action” concerns this combination. Pressing ordinary Enter on the focused Preview button can still activate the button normally. Focus is not transferred into the editor to manufacture eligibility.

Walk one complete note through the rules

In this invented session, the user wants to preview the Japanese note 旅のメモ. The rows describe intended user actions, not a captured DOM-event sequence; input-method event order is one of the things the later browser run must reveal.

Step User action or state Expected application outcome
K1 Focus the empty editor Preview is unavailable; no preview yet
K2 Begin Japanese input and select the candidate 旅のメモ Candidate work remains inside text input; no preview
K3 Confirm the candidate with Enter Commit the candidate; no preview from the commit gesture
K4 Release that Enter gesture and finish composition Note contains 旅のメモ; a new shortcut may now be considered
K5 Press fresh Control+Enter in the editor Preview count becomes 1; text and focus stay in the editor
K6 Keep that combination held; repeated keydowns arrive Count stays 1
K7 Release it, then press Control+Shift+Enter Count stays 1; the extra modifier does not match
K8 Focus the Preview button and press Control+Enter Shortcut does nothing; count stays 1
K9 Release the combination; press ordinary Enter on the button Native button activation previews; count becomes 2
K10 Focus the page background and press Control+Enter Count stays 2; no focus jump
K11 Return to the editor, replace the note with spaces, press fresh Control+Enter Count stays 2; no empty preview replaces the existing one
K12 Type Review outline, release input, press fresh Control+Enter Count becomes 3; preview shows that exact note

K3 deserves a second run with the selected input method's actual confirmation behavior. If the browser emits the ending notification before the relevant keydown, that event must still not become a preview command. Likewise, the key that starts composition must not leak into an application action just because the starting notification has not arrived yet.

Copy the implementation request

Add one local-preview shortcut to the fictional Cove Review editor.

Objects: note-draft textarea, preview-note native button, local preview panel.
First verification target: Safari 26 on macOS 26, with Japanese—Romaji
and English (U.S.) input sources. These are chosen targets, not detected
installed versions. Record exact browser/OS builds and input settings at run time.

Control+Enter means Control only, without Shift, Alt/Option or Meta/Command.
Only an eligible fresh press in note-draft may invoke preview once.
Preserve the draft exactly and keep focus in the editor.
The visible button continues to provide a separate native activation path.

Implement the nine focus/phase cells and K1–K12 as specified.
Do not handle an event already consumed by a nested control.
Do not steal ordinary Enter, IME confirmation, mismatched modifiers,
background events or held/repeated presses. Empty/whitespace-only input
does not replace the previous preview.

Inspect real start/end composition ordering and commit-key release.
Do not treat isComposing alone or synthetic KeyboardEvents as proof that
the boundary works. Explain any compatibility fallback with its scope.
If reliable separation cannot be established, leave the shortcut disabled
for that input combination and retain the visible Preview button.

Preserve ordinary editing, the rest of the toolbar and all send/save behavior.
Return the focused change, event-order observations and the completed run sheet.
Do not change CueSpec's Fn setting or add an operating-system shortcut.

Keep actual observations beside expected ones

Use this run sheet for each selected input source. The author has not run a browser or real input method for this example.

Check Expected Actual observation Run status
Ordinary editor Enter Normal text-input behavior; no preview NOT_RUN
Candidate confirmation and start/end boundary Commit succeeds; no preview leaks through NOT_RUN
Fresh editor Control+Enter One preview; draft and focus retained NOT_RUN
Held combination No second preview from the same press NOT_RUN
Extra modifiers No shortcut action NOT_RUN
Toolbar combination versus native Enter Only native button activation previews NOT_RUN
Background and whitespace-only note No shortcut action NOT_RUN

If an operating-system or input-method binding intercepts the combination, record that outcome before choosing another binding. The useful deliverable is a shortcut that fits its input context, plus a usable button when it does not.