Skip to content

[GLFW] Allow Backspace and Tab in <input> and <textarea> elements - #28143

Open
sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:glfw-input-backspace
Open

sbc100 wants to merge 1 commit into
emscripten-core:mainfrom
sbc100:glfw-input-backspace

Conversation

@sbc100

@sbc100 sbc100 commented Nov 3, 2026

Copy link
Copy Markdown
Collaborator

Previously GLFW.onKeydown unconditionally called event.preventDefault() on Backspace and Tab keydown events on window, preventing users from deleting text or tabbing between HTML <input> and <textarea> elements on the same page.

Based on #22879 by @uysalibov and tests from #23285 by @kripken.

Fixes: #22866

Previously `GLFW.onKeydown` unconditionally called
`event.preventDefault()` on `Backspace` and `Tab` keydown events on
`window`, preventing users from deleting text or tabbing between HTML
`<input>` and `<textarea>` elements on the same page.

Based on emscripten-core#22879 by @uysalibov and tests from emscripten-core#23285 by @kripken.

Fixes: emscripten-core#22866

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request modifies libglfw.js to prevent default backspace and tab behaviors only when the event target is not an INPUT or TEXTAREA element, and adds a browser test to verify this functionality. The review feedback points out two important issues: first, simulateKeyEvent is undefined in the test file, which will lead to a runtime error; second, the event handler should guard against a null event.target and also avoid preventing default behaviors on contenteditable elements.

Comment on lines +141 to +142
EM_ASM({
// Create input and textarea elements and add them to the DOM.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The test uses simulateKeyEvent to dispatch events and assert whether they were prevented, but simulateKeyEvent is not defined anywhere in this file or in the global scope. This will cause a ReferenceError when the test runs. We should define simulateKeyEvent inside the EM_ASM block.

  EM_ASM({
    const simulateKeyEvent = (type, keyCode, code, key, target) => {
      target = target || Module['canvas'];
      const event = new KeyboardEvent(type, {
        keyCode: keyCode,
        code: code,
        key: key,
        bubbles: true,
        cancelable: true
      });
      Object.defineProperty(event, 'keyCode', { get: () => keyCode });
      return target.dispatchEvent(event);
    };

    // Create input and textarea elements and add them to the DOM.

Comment thread src/lib/libglfw.js
Comment on lines +425 to +426
if (event.target.tagName !== 'INPUT' && event.target.tagName !== 'TEXTAREA' &&
(event.key == 'Backspace' || event.key == 'Tab')) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent potential runtime errors, we should guard against event.target being null or undefined before accessing its properties. Additionally, we should also avoid preventing default behavior for contenteditable elements (e.g., rich text editors) on the same page, as users would expect Backspace and Tab to work there as well. Checking event.target.isContentEditable handles this case cleanly.

      if (event.target &&
          event.target.tagName !== 'INPUT' &&
          event.target.tagName !== 'TEXTAREA' &&
          !event.target.isContentEditable &&
          (event.key == 'Backspace' || event.key == 'Tab')) {

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

library_glfw.js blocks html inputs

1 participant