keyboard.js 865 B

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