Skip to content

Conversation

falkoschindler
Copy link
Contributor

Motivation

In #4924 @samuller mentioned that the "/" key on the NiceGUI website doesn't work flawlessly to open the search dialog on Firefox because the default handler opens Firefox' built-in search field. We also discussed how to bring the preventDefault() and stopPropagation() functions to ui.keyboard.

Implementation

  • This PR adds the original JavaScript event object to the argument dictionary which is passed to js_handler.
  • A new demo shows how to use .on('key', ..., js_handler=...) to handle key events on the client and conditionally call preventDefault() or stopPropagation().
  • A similar js_handler function is used to improve the "/" key behavior on the NiceGUI website.
  • The formatting of some version specifiers is fixed.

Progress

  • I chose a meaningful title that completes the sentence: "If applied, this PR will..."
  • The implementation is complete.
  • Pytests are not necessary.
  • Documentation has been added.

@falkoschindler falkoschindler added this to the 3.1 milestone Oct 6, 2025
@falkoschindler falkoschindler requested a review from rodja October 6, 2025 14:07
@falkoschindler falkoschindler added feature Type/scope: New feature or enhancement review Status: PR is open and needs review labels Oct 6, 2025
@samuller
Copy link
Contributor

samuller commented Oct 6, 2025

Does the ui.keyboard's ignore= argument continue working, or will the js_handler side-step it?

If so, then preventDefault() would also prevent the characters from being typed in input boxes (same issue that happened to me in #4924).

@falkoschindler
Copy link
Contributor Author

@samuller Good question! I haven't thought about that.
But the ignore list is handled before the js_handler is called. So typing in an input field should still work.

Copy link
Member

@rodja rodja left a comment

Choose a reason for hiding this comment

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

Elegant!

@evnchn
Copy link
Collaborator

evnchn commented Oct 6, 2025

Briefly looked where else we are emitting events and did not expose the browser's event. I don't see any. No problems with this PR.

Just got extra stuff to address later when see fit.

On the formatting of version specifiers and other things in the docstring, it's such an easy thing to miss and I'm thinking one day we may need to put it in the CONTRIBUTING.md so that AI agents can help us check things, or maybe even put this in the pipeline code-check if we have a reliable detection method.

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

Labels

feature Type/scope: New feature or enhancement review Status: PR is open and needs review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants