diff --git a/src/boot/keyShortcut.js b/src/boot/keyShortcut.js new file mode 100644 index 000000000..2d84dcc6d --- /dev/null +++ b/src/boot/keyShortcut.js @@ -0,0 +1,32 @@ +export default { + mounted: function (el, binding) { + const shortcut = binding.value ?? '+'; + + const { key, ctrl, alt, callback } = + typeof shortcut === 'string' + ? { + key: shortcut, + ctrl: true, + alt: true, + callback: () => + document.querySelector('button[shortcut="+"]')?.click(), + } + : binding.value; + + const handleKeydown = (event) => { + if (event.key === key && (!ctrl || event.ctrlKey) && (!alt || event.altKey)) { + callback(); + } + }; + + // Attach the event listener to the window + window.addEventListener('keydown', handleKeydown); + + el._handleKeydown = handleKeydown; + }, + unmounted: function (el) { + if (el._handleKeydown) { + window.removeEventListener('keydown', el._handleKeydown); + } + }, +}; diff --git a/src/boot/quasar.js b/src/boot/quasar.js index a8d9b7ad9..00dd2cd5e 100644 --- a/src/boot/quasar.js +++ b/src/boot/quasar.js @@ -1,6 +1,8 @@ import { boot } from 'quasar/wrappers'; import qFormMixin from './qformMixin'; +import keyShortcut from './keyShortcut'; export default boot(({ app }) => { app.mixin(qFormMixin); + app.directive('shortcut', keyShortcut); });