Skip to content

fix(useMagicKeys): doesn't trigger on releasing and pressing again the second key in a combination #4691

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fixes #3026 useMagicKeys - doesn't trigger on releasing and pressing …
…again the second key in a combination
  • Loading branch information
hunterwilhelm committed Apr 3, 2025
commit 4218aa536f918f35eef9945ac3c776e82fa3befe
44 changes: 44 additions & 0 deletions packages/core/useMagicKeys/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,48 @@ describe('useMagicKeys', () => {
}))
expect(Ctrl_Shift_Period.value).toBe(true)
})
it('macos command key combinations with double keydown', async () => {
// #3026: doesn't trigger on releasing and pressing again the second key in a combination
const { Meta_Z } = useMagicKeys({ target })
expect(Meta_Z.value).toBe(false)

// Simulate first keydown of command key
target.dispatchEvent(new KeyboardEvent('keydown', {
key: 'Meta',
metaKey: true,
}))
expect(Meta_Z.value).toBe(false)

// Simulate first keydown of Z
target.dispatchEvent(new KeyboardEvent('keydown', {
key: 'z',
metaKey: true,
}))
expect(Meta_Z.value).toBe(true) // true because of the first keydown of command key + z
await new Promise(resolve => setTimeout(resolve, 1))
expect(Meta_Z.value).toBe(true) // after 1ms, it's still true

// Simulate second keydown of Z without keyup (macOS behavior)
target.dispatchEvent(new KeyboardEvent('keydown', {
key: 'z',
metaKey: true,
}))
expect(Meta_Z.value).toBe(false) // false because it has to manually trigger keyup event
await new Promise(resolve => setTimeout(resolve, 1))
expect(Meta_Z.value).toBe(true) // after 1ms, it's true again

// Simulate keyup of Z
target.dispatchEvent(new KeyboardEvent('keyup', {
key: 'z',
metaKey: true,
}))
expect(Meta_Z.value).toBe(false)

// Simulate keyup of command key
target.dispatchEvent(new KeyboardEvent('keyup', {
key: 'Meta',
metaKey: false,
}))
expect(Meta_Z.value).toBe(false)
})
})
13 changes: 12 additions & 1 deletion packages/core/useMagicKeys/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,18 @@ export function useMagicKeys(options: UseMagicKeysOptions<boolean> = {}): any {
}

useEventListener(target, 'keydown', (e: KeyboardEvent) => {
updateRefs(e, true)
const key = e.key?.toLowerCase()
// #3026: doesn't trigger on releasing and pressing again the second key in a combination
// Solution: Trigger "keyup" event manually when "keydown" event is fired without "keyup"
if (key && e.getModifierState('Meta') && current.has(key)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

it this a meta key only problem? what about shift and ctrl?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updateRefs(e, false)
setTimeout(() => {
updateRefs(e, true)
}, 0)
}
else {
updateRefs(e, true)
}
return onEventFired(e)
}, { passive })
useEventListener(target, 'keyup', (e: KeyboardEvent) => {
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy