diff --git a/src/core/main/custom/hotkey.ts b/src/core/main/custom/hotkey.ts index 7e40dcb..6aa0cae 100644 --- a/src/core/main/custom/hotkey.ts +++ b/src/core/main/custom/hotkey.ts @@ -275,8 +275,15 @@ export function unwarpBinary(bin: number): AssistHoykey { } export function checkAssist(bin: number, key: KeyCode) { - return !!( - (1 << (key === KeyCode.Ctrl ? 0 : key === KeyCode.Shift ? 1 : 2)) & - bin + return ( + isAssist(key) && + !!( + (1 << (key === KeyCode.Ctrl ? 0 : key === KeyCode.Shift ? 1 : 2)) & + bin + ) ); } + +export function isAssist(key: KeyCode) { + return key === KeyCode.Ctrl || key === KeyCode.Shift || key === KeyCode.Alt; +} diff --git a/src/core/main/custom/keyboard.ts b/src/core/main/custom/keyboard.ts index 357d8ed..48b55db 100644 --- a/src/core/main/custom/keyboard.ts +++ b/src/core/main/custom/keyboard.ts @@ -26,6 +26,7 @@ interface KeyboardItem { interface VirtualKeyEmit { preventDefault(): void; + preventAssist(): void; } type VirtualKeyEmitFn = ( @@ -155,6 +156,7 @@ export class Keyboard extends EventEmitter { this.keys.push(...toClone); this.emit('extend', keyboard); + return this; } /** @@ -163,10 +165,15 @@ export class Keyboard extends EventEmitter { */ emitKey(key: KeyboardItem, index: number) { let prevent = false; + let preventAss = false; const preventDefault = () => (prevent = true); - this.emit('emit', key, this.assist, index, { preventDefault }); + const preventAssist = () => (preventAss = true); + this.emit('emit', key, this.assist, index, { + preventDefault, + preventAssist + }); - if (!prevent) { + if (!preventAss) { if (key.key === KeyCode.Ctrl) { this.assist = flipBinary(this.assist, 0); } else if (key.key === KeyCode.Shift) { @@ -174,6 +181,8 @@ export class Keyboard extends EventEmitter { } else if (key.key === KeyCode.Alt) { this.assist = flipBinary(this.assist, 2); } + } + if (!prevent) { const ev = generateKeyboardEvent(key.key, this.assist); gameKey.emitKey(key.key, this.assist, 'up', ev); } @@ -194,7 +203,3 @@ export function generateKeyboardEvent(key: KeyCode, assist: number) { return ev; } - -export function isAssist(key: KeyCode) { - return key === KeyCode.Ctrl || key === KeyCode.Shift || key === KeyCode.Alt; -} diff --git a/src/core/main/init/keyboard.ts b/src/core/main/init/keyboard.ts index 6474cd6..788903b 100644 --- a/src/core/main/init/keyboard.ts +++ b/src/core/main/init/keyboard.ts @@ -1,8 +1,21 @@ import { KeyCode } from '@/plugin/keyCodes'; import { Keyboard } from '../custom/keyboard'; -const qweKey = new Keyboard('qwe'); -qweKey.fontSize = 20; +const qweKey = new Keyboard('qwe'); // 字母键盘,A-Z +const numKey = new Keyboard('num'); // 数字键盘,1-0 +const charKey = new Keyboard('charKey'); // 符号键盘,!@<>等 +const fnKey = new Keyboard('fn'); // Fn键盘,F1-F12 +const assistKey = new Keyboard('assist'); // 辅助键盘,tab,capslk,shift,ctrl等 +const arrowKey = new Keyboard('arrow'); // 方向键 +const numpadKey = new Keyboard('numpad'); // 小键盘 +const mediaKey = new Keyboard('media'); // 媒体按键 +const toolKey = new Keyboard('tool'); // Home,End,Insert,Delete,PageUp,PageDn + +const mainKey = new Keyboard('main'); // 主键盘,不包含小键盘和方向键 +const toolArrowKey = new Keyboard('toolArrow'); // 辅助键盘和方向键 +const miniKey = new Keyboard('mini'); // 小键盘,包含媒体按键 +const fullKey = new Keyboard('full'); // 全部键盘 + qweKey .add({ key: KeyCode.KeyQ, @@ -18,10 +31,858 @@ qweKey width: 45, height: 45 }) + .add({ + key: KeyCode.KeyE, + x: 100, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyR, + x: 150, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyT, + x: 200, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyY, + x: 250, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyU, + x: 300, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyI, + x: 350, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyO, + x: 400, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyP, + x: 450, + y: 0, + width: 45, + height: 45 + }) .add({ key: KeyCode.KeyA, x: 10, y: 50, width: 45, height: 45 + }) + .add({ + key: KeyCode.KeyS, + x: 60, + y: 50, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyD, + x: 110, + y: 50, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyF, + x: 160, + y: 50, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyG, + x: 210, + y: 50, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyH, + x: 260, + y: 50, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyJ, + x: 310, + y: 50, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyK, + x: 360, + y: 50, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyL, + x: 410, + y: 50, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyZ, + x: 40, + y: 100, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyX, + x: 90, + y: 100, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyC, + x: 140, + y: 100, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyV, + x: 190, + y: 100, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyB, + x: 240, + y: 100, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyN, + x: 290, + y: 100, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.KeyM, + x: 340, + y: 100, + width: 45, + height: 45 }); + +numKey + .add({ + key: KeyCode.Backquote, + x: 0, + y: 0, + width: 45, + height: 45, + text: `~
\`
` + }) + .add({ + key: KeyCode.Digit1, + x: 50, + y: 0, + width: 45, + height: 45, + text: `!
1
` + }) + .add({ + key: KeyCode.Digit2, + x: 100, + y: 0, + width: 45, + height: 45, + text: `@
2
` + }) + .add({ + key: KeyCode.Digit3, + x: 150, + y: 0, + width: 45, + height: 45, + text: `#
3
` + }) + .add({ + key: KeyCode.Digit4, + x: 200, + y: 0, + width: 45, + height: 45, + text: `\$
4
` + }) + .add({ + key: KeyCode.Digit5, + x: 250, + y: 0, + width: 45, + height: 45, + text: `%
5
` + }) + .add({ + key: KeyCode.Digit6, + x: 300, + y: 0, + width: 45, + height: 45, + text: `^
6
` + }) + .add({ + key: KeyCode.Digit7, + x: 350, + y: 0, + width: 45, + height: 45, + text: `&
7
` + }) + .add({ + key: KeyCode.Digit8, + x: 400, + y: 0, + width: 45, + height: 45, + text: `*
8
` + }) + .add({ + key: KeyCode.Digit9, + x: 450, + y: 0, + width: 45, + height: 45, + text: `(
9
` + }) + .add({ + key: KeyCode.Digit0, + x: 500, + y: 0, + width: 45, + height: 45, + text: `)
0
` + }) + .add({ + key: KeyCode.Minus, + x: 550, + y: 0, + width: 45, + height: 45, + text: `
-
` + }) + .add({ + key: KeyCode.Equal, + x: 600, + y: 0, + width: 45, + height: 45, + text: `+
=
` + }) + .add({ + key: KeyCode.Backspace, + x: 650, + y: 0, + width: 70, + height: 45, + text: `Backspace` + }); + +charKey + .add({ + key: KeyCode.BracketLeft, + x: 110, + y: 0, + width: 45, + height: 45, + text: `{
[
` + }) + .add({ + key: KeyCode.BracketRight, + x: 160, + y: 0, + width: 45, + height: 45, + text: `}
]
` + }) + .add({ + key: KeyCode.Backslash, + x: 210, + y: 0, + width: 45, + height: 45, + text: `|
\\
` + }) + .add({ + key: KeyCode.Semicolon, + x: 70, + y: 50, + width: 45, + height: 45, + text: `:
;
` + }) + .add({ + key: KeyCode.Quote, + x: 120, + y: 50, + width: 45, + height: 45, + text: `"
'
` + }) + .add({ + key: KeyCode.Comma, + x: 0, + y: 100, + width: 45, + height: 45, + text: `<
,
` + }) + .add({ + key: KeyCode.Period, + x: 50, + y: 100, + width: 45, + height: 45, + text: `>
.
` + }) + .add({ + key: KeyCode.Slash, + x: 100, + y: 100, + width: 45, + height: 45, + text: `?
/
` + }); + +fnKey + .add({ + key: KeyCode.Escape, + x: 0, + y: 0, + width: 45, + height: 45, + text: `Esc` + }) + .add({ + key: KeyCode.F1, + x: 50 + 25, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F2, + x: 100 + 25, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F3, + x: 150 + 25, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F4, + x: 200 + 25, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F5, + x: 250 + 50, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F6, + x: 300 + 50, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F7, + x: 350 + 50, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F8, + x: 400 + 50, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F9, + x: 450 + 75, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F10, + x: 500 + 75, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F11, + x: 550 + 75, + y: 0, + width: 45, + height: 45 + }) + .add({ + key: KeyCode.F12, + x: 600 + 75, + y: 0, + width: 45, + height: 45 + }); + +assistKey + .add({ + key: KeyCode.Tab, + x: 0, + y: 0, + width: 70, + height: 45 + }) + .add({ + key: KeyCode.CapsLock, + x: 0, + y: 50, + width: 80, + height: 45, + text: `CapsLk` + }) + .add({ + key: KeyCode.Shift, + x: 0, + y: 100, + width: 110, + height: 45, + text: `Shift` + }) + .add({ + key: KeyCode.Ctrl, + x: 0, + y: 150, + width: 75, + height: 45, + text: `Ctrl` + }) + .add({ + key: KeyCode.Meta, + x: 80, + y: 150, + width: 45, + height: 45, + text: /* html */ ` + + + +` + }) + .add({ + key: KeyCode.Alt, + x: 130, + y: 150, + width: 70, + height: 45, + text: `Alt` + }) + .add({ + key: KeyCode.Space, + x: 205, + y: 150, + width: 320, + height: 45, + text: `` + }) + .add({ + key: KeyCode.Alt, + x: 530, + y: 150, + width: 70, + height: 45, + text: `Alt` + }) + .add({ + key: KeyCode.Meta, + x: 605, + y: 150, + width: 45, + height: 45, + text: /* html */ ` + + + +` + }) + .add({ + key: KeyCode.Ctrl, + x: 655, + y: 150, + width: 65, + height: 45, + text: `Ctrl` + }) + .add({ + key: KeyCode.Shift, + x: 615, + y: 100, + width: 105, + height: 45, + text: `Shift` + }) + .add({ + key: KeyCode.Enter, + x: 635, + y: 50, + width: 85, + height: 45, + text: `Enter` + }); + +arrowKey + .add({ + key: KeyCode.UpArrow, + x: 50, + y: 0, + width: 45, + height: 45, + text: `↑` + }) + .add({ + key: KeyCode.LeftArrow, + x: 0, + y: 50, + width: 45, + height: 45, + text: `←` + }) + .add({ + key: KeyCode.DownArrow, + x: 50, + y: 50, + width: 45, + height: 45, + text: `↓` + }) + .add({ + key: KeyCode.RightArrow, + x: 100, + y: 50, + width: 45, + height: 45, + text: `→` + }); + +toolKey + .add({ + key: KeyCode.Insert, + x: 0, + y: 60, + width: 45, + height: 45, + text: `Insert` + }) + .add({ + key: KeyCode.Delete, + x: 0, + y: 110, + width: 45, + height: 45, + text: `Delete` + }) + .add({ + key: KeyCode.Home, + x: 50, + y: 60, + width: 45, + height: 45, + text: `Home` + }) + .add({ + key: KeyCode.End, + x: 50, + y: 110, + width: 45, + height: 45, + text: `End` + }) + .add({ + key: KeyCode.PageUp, + x: 100, + y: 60, + width: 45, + height: 45, + text: `PgUp` + }) + .add({ + key: KeyCode.PageDown, + x: 100, + y: 110, + width: 45, + height: 45, + text: `PgDn` + }) + .add({ + key: KeyCode.ScrollLock, + x: 0, + y: 0, + width: 45, + height: 45, + text: `ScrLk` + }) + .add({ + key: KeyCode.PauseBreak, + x: 100, + y: 0, + width: 45, + height: 45, + text: `Pause
Break
` + }); + +numpadKey + .add({ + key: KeyCode.NumLock, + x: 0, + y: 0, + width: 45, + height: 45, + text: `NumLk` + }) + .add({ + key: KeyCode.NumpadDivide, + x: 50, + y: 0, + width: 45, + height: 45, + text: `/` + }) + .add({ + key: KeyCode.NumpadMultiply, + x: 100, + y: 0, + width: 45, + height: 45, + text: `*` + }) + .add({ + key: KeyCode.NumpadSubtract, + x: 150, + y: 0, + width: 45, + height: 45, + text: `-` + }) + .add({ + key: KeyCode.NumpadAdd, + x: 150, + y: 50, + width: 45, + height: 95, + text: `+` + }) + .add({ + key: KeyCode.Enter, + x: 150, + y: 150, + width: 45, + height: 95, + text: `Enter` + }) + .add({ + key: KeyCode.NumpadDecimal, + x: 100, + y: 200, + width: 45, + height: 45, + text: `.` + }) + .add({ + key: KeyCode.Numpad0, + x: 0, + y: 200, + width: 95, + height: 45, + text: `0` + }) + .add({ + key: KeyCode.Numpad7, + x: 0, + y: 50, + width: 45, + height: 45, + text: `7` + }) + .add({ + key: KeyCode.Numpad8, + x: 50, + y: 50, + width: 45, + height: 45, + text: `8` + }) + .add({ + key: KeyCode.Numpad9, + x: 100, + y: 50, + width: 45, + height: 45, + text: `9` + }) + .add({ + key: KeyCode.Numpad4, + x: 0, + y: 100, + width: 45, + height: 45, + text: `4` + }) + .add({ + key: KeyCode.Numpad5, + x: 50, + y: 100, + width: 45, + height: 45, + text: `5` + }) + .add({ + key: KeyCode.Numpad6, + x: 100, + y: 100, + width: 45, + height: 45, + text: `6` + }) + .add({ + key: KeyCode.Numpad1, + x: 0, + y: 150, + width: 45, + height: 45, + text: `1` + }) + .add({ + key: KeyCode.Numpad2, + x: 50, + y: 150, + width: 45, + height: 45, + text: `2` + }) + .add({ + key: KeyCode.Numpad3, + x: 100, + y: 150, + width: 45, + height: 45, + text: `3` + }); + +mediaKey + .add({ + key: KeyCode.MediaPlayPause, + x: 0, + y: 0, + width: 45, + height: 45, + text: /* html */ ` + + + + / + + + +` + }) + .add({ + key: KeyCode.MediaStop, + x: 50, + y: 0, + width: 45, + height: 45, + text: /* html */ ` + + + +` + }) + .add({ + key: KeyCode.MediaTrackPrevious, + x: 100, + y: 0, + width: 45, + height: 45, + text: /* html */ ` + + + +` + }) + .add({ + key: KeyCode.MediaTrackNext, + x: 150, + y: 0, + width: 45, + height: 45, + text: /* html */ ` + + + +` + }); + +toolArrowKey.extend(arrowKey, 0, 210).extend(toolKey, 0, 0); +miniKey.extend(numpadKey, 0, 0); + +mainKey + .extend(numKey, 0, 60) + .extend(qweKey, 75, 110) + .extend(charKey, 465, 110) + .extend(fnKey, 0, 0) + .extend(assistKey, 0, 110); + +fullKey + .extend(mainKey, 0, 0) + .extend(toolArrowKey, 740, 0) + .extend(miniKey, 905, 60) + .extend(mediaKey, 905, 0); diff --git a/src/panel/keyboard.vue b/src/panel/keyboard.vue index 940396d..7264858 100644 --- a/src/panel/keyboard.vue +++ b/src/panel/keyboard.vue @@ -12,9 +12,10 @@ height: `${key.height}px` }" > - {{ - key.text ?? KeyCodeUtils.toString(key.key) - }} + @@ -24,7 +25,7 @@ import { checkAssist } from '@/core/main/custom/hotkey'; import { Keyboard } from '@/core/main/custom/keyboard'; import { KeyboardEmits } from '@/core/main/custom/keyboard'; import { KeyCodeUtils } from '@/plugin/keyCodes'; -import { onUnmounted, ref } from 'vue'; +import { nextTick, onUnmounted, ref } from 'vue'; const props = defineProps<{ keyboard: Keyboard; @@ -45,10 +46,9 @@ const [width, height] = (() => { return [`${mw}px`, `${mh}px`]; })(); -function onAssist(_: any, ass: number) { - new Promise(res => { - assist.value = ass; - res(); +function onAssist() { + nextTick(() => { + assist.value = props.keyboard.assist; }); } props.keyboard.on('emit', onAssist); @@ -69,6 +69,7 @@ onUnmounted(() => { display: block; font-size: v-bind(fontSize); position: relative; + font-family: Arial; } .keyboard-item { @@ -80,6 +81,7 @@ onUnmounted(() => { align-items: center; transition: all 0.1s linear; cursor: pointer; + color: white; } .keyboard-item:hover, @@ -87,13 +89,14 @@ onUnmounted(() => { background-color: #555; } -.keyboard-key[active='true'] { +.keyboard-item[active='true']::v-deep(> *) { color: gold; + font-weight: 700; } .keyboard-key { height: 100%; - min-width: 50px; + width: 100%; display: flex; justify-content: center; align-items: center; diff --git a/src/plugin/utils.ts b/src/plugin/utils.ts index 9ed1057..7d0170a 100644 --- a/src/plugin/utils.ts +++ b/src/plugin/utils.ts @@ -7,8 +7,9 @@ import { EVENT_KEY_CODE_MAP, KeyCode } from './keyCodes'; import axios from 'axios'; import { decompressFromBase64 } from 'lz-string'; import { parseColor } from './webgl/utils'; -import { isAssist, Keyboard, KeyboardEmits } from '@/core/main/custom/keyboard'; +import { Keyboard, KeyboardEmits } from '@/core/main/custom/keyboard'; import { mainUi } from '@/core/main/init/ui'; +import { isAssist } from '@/core/main/custom/hotkey'; type CanParseCss = keyof { [P in keyof CSSStyleDeclaration as CSSStyleDeclaration[P] extends string @@ -358,7 +359,7 @@ export function getVitualKeyOnce( assist: number = 0 ): Promise { return new Promise(res => { - const key = Keyboard.get('qwe')!; + const key = Keyboard.get('full')!; key.withAssist(assist); const id = mainUi.open('virtualKey', { keyboard: key }); key.on('emit', (item, assist, index, ev) => { @@ -366,13 +367,14 @@ export function getVitualKeyOnce( if (emitAssist) { res({ key: item.key, assist: 0 }); key.disposeScope(); + mainUi.close(id); } else { if (!isAssist(item.key)) { res({ key: item.key, assist }); key.disposeScope(); + mainUi.close(id); } } - mainUi.close(id); }); }); }