3737
3838<template>
3939 <div class="container">
40+ <p class="description">{{ loggedKey }}</p>
4041 <p class="description">Press <kbd>W</kbd> <kbd>S</kbd> <kbd>A</kbd> <kbd>D</kbd> keys to move the ball</p>
4142 <div class="square">
4243 <div class="circle" :style="{ left: `${circleX}px`, top: `${circleY}px` }"></div>
@@ -57,12 +58,36 @@ where:
5758 import { ref } from 'vue'
5859 import { useHotKey } from '../../src/composables/useHotKey/index.js'
5960
61+ const isMac = /mac|ipad|iphone|darwin/i.test(navigator.userAgent)
62+
6063 export default {
6164 setup() {
65+ const loggedKey = ref('Press any key')
6266 const circleX = ref(20)
6367 const circleY = ref(20)
6468 const highlighted = ref(false)
6569
70+ const updateLoggedKey = (event) => {
71+ if (['Ctrl', 'Meta', 'Alt', 'Shift'].includes(event.key)) {
72+ return
73+ }
74+
75+ loggedKey.value = `Key pressed: ${event.key} | `
76+ if (event.ctrlKey) {
77+ loggedKey.value += 'Ctrl + '
78+ }
79+ if (event.metaKey) {
80+ loggedKey.value += isMac ? 'Cmd + ' : 'Meta + '
81+ }
82+ if (event.altKey) {
83+ loggedKey.value += 'Alt + '
84+ }
85+ if (event.shiftKey) {
86+ loggedKey.value += 'Shift + '
87+ }
88+ loggedKey.value += event.code
89+ }
90+
6691 const moveUp = (event) => {
6792 circleY.value = Math.max(0, circleY.value - 10)
6893 }
@@ -79,13 +104,15 @@ where:
79104 highlighted.value = !highlighted.value
80105 }
81106
107+ useHotKey(true, updateLoggedKey)
82108 useHotKey('w', moveUp)
83109 useHotKey('s', moveDown)
84110 useHotKey('a', moveLeft)
85111 useHotKey('d', moveRight)
86112 const stop = useHotKey('m', toggleHighlighted, { push: true })
87113
88114 return {
115+ loggedKey,
89116 circleX,
90117 circleY,
91118 highlighted,
0 commit comments