Skip to content

Shortcuts intercepting '?' in interactive exercises/playgrounds #3064

@kartik-py12

Description

@kartik-py12

Problem

The ? keyboard shortcut (Help menu) intercepts the keypress globally. It is a "breaking" issue for Rust by Example because typing the ? operator is required for many of the code exercises (error handling).

Currently, if you are solving a "Todo" in the embedded editor and try to use the ? operator, the help popup opens and the character is never inserted into the code.

Expected Behavior
Shortcuts should be suppressed if the focus is within an editable element, including inputs, textareas, or contenteditable containers used by code editors.

Steps

  1. Open any Rust by Example page with an exercise (e.g., Hello World).
  2. Click into the interactive code block to edit.
  3. Try to type ?.
  4. Result: The Help modal opens; the editor remains unchanged and the character is not typed.

Possible Solution(s)

The keydown listener in book.js needs a guard clause to check the event target before calling e.preventDefault().

// Suggested guard at the start of the keydown handler
const target = e.target;
const isEditable = target.tagName === 'INPUT' || 
                   target.tagName === 'TEXTAREA' || 
                   target.isContentEditable ||
                   (target.closest && target.closest('.ace_editor'));

if (isEditable) { return; }

### Notes

* **Environment:** Affects all mdBook sites with interactive elements. Reproducible on Chrome/Firefox/Safari.
* **Willingness to contribute:** I have the fix ready and can submit a PR if this issue is confirmed.

### Version

```text
0.5.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: A bug, incorrect or unintended behavior

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions