Skip to content

Commit

Permalink
Allow to press Alt hotkeys on focus in other fields
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed May 6, 2024
1 parent fb508bc commit 4830f22
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
2 changes: 1 addition & 1 deletion hotkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function findHotKey(event, window, transformers) {
export function hotkeyKeyUX(transformers = []) {
return window => {
function keyDown(event) {
if (ignoreHotkeysIn(event.target)) return
if (!event.altKey && ignoreHotkeysIn(event.target)) return
let press = findHotKey(event, window, transformers)
if (press) press.click()
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"import": {
"./index.js": "{ startKeyUX, hotkeyKeyUX, pressKeyUX, focusGroupKeyUX, jumpKeyUX, hiddenKeyUX, likelyWithKeyboard, getHotKeyHint, hotkeyOverrides, hotkeyMacCompat }"
},
"limit": "2059 B"
"limit": "2070 B"
}
],
"clean-publish": {
Expand Down
21 changes: 21 additions & 0 deletions test/hotkey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ test('ignores hot keys when focus is inside text fields', () => {
equal(clicked, 2)
})

test('does not ignore hotkeys with Alt on focus in text field', () => {
let window = new JSDOM().window
startKeyUX(window, [hotkeyKeyUX()])
window.document.body.innerHTML =
'<input type="text">' +
'<a></a>' +
'<button aria-keyshortcuts="alt+b"></button>'

let clicked = 0
window.document.querySelector('button')!.addEventListener('click', () => {
clicked += 1
})

press(
window,
{ altKey: true, key: 'b' },
window.document.querySelector('[type=text]')!
)
equal(clicked, 1)
})

test('supports non-English keyboard layouts', () => {
let window = new JSDOM().window
startKeyUX(window, [hotkeyKeyUX()])
Expand Down

0 comments on commit 4830f22

Please sign in to comment.