import { ref, onMounted, onUnmounted } from 'vue' function useKeyPress(key: string) { const status = ref(false); function keydown(event: any) { if (event.key === key) status.value = true } function keyup() { status.value = false } onMounted(() => { //按键监听 document.addEventListener('keydown', keydown) document.addEventListener('keyup', keyup) }) onUnmounted(() => { //去除监听 document.removeEventListener('keydown', keydown) document.removeEventListener('keyup', keyup) }) return status } export default useKeyPress;