Skip to content

Commit

Permalink
fix(useHotKey): Allow to listen for a key with or without modifiers
Browse files Browse the repository at this point in the history
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Jan 23, 2025
1 parent 01065ad commit c27f4b3
Showing 1 changed file with 29 additions and 29 deletions.
58 changes: 29 additions & 29 deletions src/composables/useHotKey/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,36 @@ type KeyboardEventHandler = (event: KeyboardEvent) => void

function eventHandler(callback, options: UseHotKeyOptions): KeyboardEventHandler {
return (event: KeyboardEvent) => {
const ctrlKeyPressed = isMac ? event.metaKey : event.ctrlKey
if (ctrlKeyPressed !== Boolean(options.ctrl)) {
/**
* Ctrl is required and not pressed, or the opposite
* As on macOS 'cmd' key is used instead of 'ctrl' key for most key combinations,
* 'event.metaKey' should be checked
*/
return
} else if (event.altKey !== Boolean(options.alt)) {
// Alt is required and not pressed, or the opposite
return
} else if (options.shift !== undefined && event.shiftKey !== Boolean(options.shift)) {
/**
* Shift is required and not pressed, or the opposite
* As shift key is used to type capital letters and alternate characters,
* option should be explicitly defined
*/
return
} else if (shouldIgnoreEvent(event)) {
// Keyboard shortcuts are disabled, because active element assumes input
return
}
const ctrlKeyPressed = isMac ? event.metaKey : event.ctrlKey
if (options.ctrl !== undefined && ctrlKeyPressed !== options.ctrl) {
/**
* Ctrl is required and not pressed, or the opposite
* As on macOS 'cmd' key is used instead of 'ctrl' key for most key combinations,
* 'event.metaKey' should be checked
*/
return
} else if (options.alt !== undefined && event.altKey !== options.alt) {
// Alt is required and not pressed, or the opposite
return
} else if (options.shift !== undefined && event.shiftKey !== options.shift) {
/**
* Shift is required and not pressed, or the opposite
* As shift key is used to type capital letters and alternate characters,
* option should be explicitly defined
*/
return
} else if (shouldIgnoreEvent(event)) {
// Keyboard shortcuts are disabled, because active element assumes input
return
}

if (options.prevent) {
event.preventDefault()
}
if (options.stop) {
event.stopPropagation()
}
callback(event)
if (options.prevent) {
event.preventDefault()
}
if (options.stop) {
event.stopPropagation()
}
callback(event)
}
}

Expand Down

0 comments on commit c27f4b3

Please sign in to comment.