keyboard.js 792 B

1234567891011121314151617181920212223242526272829
  1. export default {
  2. mounted() {
  3. for (const event of this.events) {
  4. document.addEventListener(event, (evt) => {
  5. // https://stackoverflow.com/a/36469636/3419103
  6. const focus = document.activeElement;
  7. if (focus && this.ignore.includes(focus.tagName.toLowerCase())) return;
  8. if (evt.repeat && !this.repeating) return;
  9. this.$emit("key", {
  10. action: event,
  11. altKey: evt.altKey,
  12. ctrlKey: evt.ctrlKey,
  13. shiftKey: evt.shiftKey,
  14. metaKey: evt.metaKey,
  15. code: evt.code,
  16. key: evt.key,
  17. location: evt.location,
  18. repeat: evt.repeat,
  19. locale: evt.locale,
  20. });
  21. });
  22. }
  23. },
  24. props: {
  25. events: Array,
  26. repeating: Boolean,
  27. ignore: Array,
  28. },
  29. };