Skip to content

use event.key for search shortcut #1525

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions src/pydata_sphinx_theme/assets/scripts/pydata-sphinx-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,22 @@ var addEventListenerForSearchKeyboard = () => {
(event) => {
let input = findSearchInput();
// toggle on Ctrl+k or ⌘+k
if ((event.ctrlKey || event.metaKey) && event.code == "KeyK") {
if (
// Ignore if shift or alt are pressed
!event.shiftKey &&
!event.altKey &&
// On Mac use ⌘, all other OS use Ctrl
(isMac
? event.metaKey && !event.ctrlKey
: !event.metaKey && event.ctrlKey) &&
// Case-insensitive so the shortcut still works with caps lock
/k/i.test(event.key)
) {
event.preventDefault();
toggleSearchField();
}
// also allow Escape key to hide (but not show) the dynamic search field
else if (document.activeElement === input && event.code == "Escape") {
else if (document.activeElement === input && /Escape/i.test(event.key)) {
toggleSearchField();
}
},
Expand Down