From f84c72dc7191706895063c8d78c1c3ae8ca141ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A5=94=E8=B7=91=E7=9A=84=E9=9D=A2=E6=9D=A1?= <1262327911@qq.com> Date: Wed, 22 Jun 2022 14:31:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=94=AE=E7=9B=98?= =?UTF-8?q?=E6=8C=89=E9=94=AE=E8=AE=B0=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/views/chart/hooks/useKeyboard.hook.ts | 23 ++++++++++++++++++++--- types/global.d.ts | 2 ++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/views/chart/hooks/useKeyboard.hook.ts b/src/views/chart/hooks/useKeyboard.hook.ts index 0d7d4ad2..58ab28c4 100644 --- a/src/views/chart/hooks/useKeyboard.hook.ts +++ b/src/views/chart/hooks/useKeyboard.hook.ts @@ -75,9 +75,21 @@ const macKeyList: Array = [ macKeyboardValue.forward, ] +// 处理键盘记录 +const keyRecordHandle = () => { + document.onkeydown = throttle((e: KeyboardEvent) => { + if(window.$KeyboardActive) window.$KeyboardActive.add(e.key.toLocaleLowerCase()) + else window.$KeyboardActive = new Set([e.key]) + }, 200) + + document.onkeyup = throttle((e: KeyboardEvent) => { + if(window.$KeyboardActive) window.$KeyboardActive.delete(e.key.toLocaleLowerCase()) + }, 200) +} + // 初始化监听事件 export const useAddKeyboard = () => { - const switchHande = (keyboardValue: typeof winKeyboardValue, e: string) => { + const switchHandle = (keyboardValue: typeof winKeyboardValue, e: string) => { switch (e) { // ct+↑ case keyboardValue.up: @@ -124,15 +136,20 @@ export const useAddKeyboard = () => { } } winKeyList.forEach((key: string) => { - switchHande(winKeyboardValue, key) + switchHandle(winKeyboardValue, key) }) macKeyList.forEach((key: string) => { - switchHande(macKeyboardValue, key) + switchHandle(macKeyboardValue, key) }) + + keyRecordHandle() } // 卸载监听事件 export const useRemoveKeyboard = () => { + document.onkeydown = () => {}; + document.onkeyup = () => {}; + winKeyList.forEach((key: string) => { keymaster.unbind(key) }) diff --git a/types/global.d.ts b/types/global.d.ts index 74863473..3636172e 100644 --- a/types/global.d.ts +++ b/types/global.d.ts @@ -5,6 +5,8 @@ interface Window { // 语言 $t: any $vue: any + // 键盘按键记录 + $KeyboardActive?: Set } declare type Recordable = Record \ No newline at end of file