Vue Global Events is a lightweight library for Vue 3 that enables developers to register global keyboard and mouse events directly within Vue templates using familiar Vue event modifiers. It abstracts away the complexities of `addEventListener` and `removeEventListener` on `document` or `window`, automatically handling event lifecycle management in sync with component unmounting. The current stable version is 3.0.1, specifically designed for Vue 3. It differs from manual global event handling by leveraging Vue's declarative syntax and reactivity system, allowing events to be conditionally enabled/disabled with `v-if` and ensuring proper cleanup, including support for Server-Side Rendering (SSR). While there isn't a strict release cadence, updates are typically made to align with new Vue versions or to introduce minor enhancements and bug fixes.
npm install vue-global-eventsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to install, register, and use the `GlobalEvents` component to listen for global keyboard shortcuts like `Ctrl+Space` and `Alt+S` within a Vue 3 application. It also shows conditional event listening with `v-if` and includes a basic input field to illustrate event filtering caveats.
For Vue 2 projects, install `vue-global-events@^2.0.0` or refer to the `v1` branch for specific Vue 2 instructions. For Vue 3, simply install `vue-global-events`.
If the `target` prop needs to be reactive, you must bind a `key` attribute to the `GlobalEvents` component with the same reactive value. This forces Vue to re-render and re-mount the component when the `key` changes, effectively reapplying the listeners to the new target.
Avoid using shortcuts that are reserved by the operating system or browser, as they will override your application's handlers. Always test critical shortcuts across different browsers and OSes.
Add `.prevent` to your event listener: `@keyup.ctrl.space.prevent="myHandler"`.
Specify the literal key character in your event modifier (e.g., `@keyup..` for a period) instead of relying on deprecated `keyCode` values or guessing based on layout.
Ensure `app.component('GlobalEvents', GlobalEvents)` is called for global registration, or include `GlobalEvents` in the `components` option of your consuming Vue component.Check the `filter` function provided to `GlobalEvents`. If you want events to fire even in inputs, ensure your `filter` function does not exclude them (the default is `() => true`). Also, verify the `target` prop is correctly set to 'document' or 'window' as intended.
Ensure you've added the `.prevent` modifier to your event: `@keyup.ctrl.s.prevent="myHandler"`. If the issue persists, the shortcut is likely reserved by the browser/OS and cannot be overridden.