fix: parse hotkey (#5167)

* fix: incorrectly parse hotkey

* refactor: parse hotkey

* fix: panic on linux

* chore: update

* chore: update style

* fix: register hotkey error on windows

* chore: update style

---------

Co-authored-by: Tunglies <tunglies.dev@outlook.com>
This commit is contained in:
oomeow
2025-10-23 15:54:48 +08:00
committed by GitHub
parent 585963e751
commit d7859b07a6
6 changed files with 25 additions and 80 deletions

View File

@@ -7,7 +7,7 @@ import { parseHotkey } from "@/utils/parse-hotkey";
const KeyWrapper = styled("div")(({ theme }) => ({
position: "relative",
width: 165,
width: 230,
minHeight: 36,
"> input": {
@@ -39,6 +39,7 @@ const KeyWrapper = styled("div")(({ theme }) => ({
},
},
".item": {
fontSize: "14px",
color: theme.palette.text.primary,
border: "1px solid",
borderColor: alpha(theme.palette.text.secondary, 0.2),
@@ -76,11 +77,10 @@ export const HotkeyInput = (props: Props) => {
}
}}
onKeyDown={(e) => {
const evt = e.nativeEvent;
e.preventDefault();
e.stopPropagation();
const key = parseHotkey(evt.key);
const key = parseHotkey(e);
if (key === "UNIDENTIFIED") return;
changeRef.current = [...new Set([...changeRef.current, key])];

View File

@@ -82,7 +82,7 @@ export const HotkeyViewer = forwardRef<DialogRef>((props, ref) => {
});
setOpen(false);
} catch (err: any) {
showNotice("error", err.toString());
showNotice("error", err.message || err.toString());
}
});

View File

@@ -1,26 +1,6 @@
import { KeyboardEvent } from "react";
const KEY_MAP: Record<string, string> = {
// 特殊字符映射
"-": "Minus",
"=": "Equal",
"[": "BracketLeft",
"]": "BracketRight",
"\\": "Backslash",
";": "Semicolon",
"'": "Quote",
",": "Comma",
".": "Period",
"/": "Slash",
// 数字键映射
"1": "Digit1",
"2": "Digit2",
"3": "Digit3",
"4": "Digit4",
"5": "Digit5",
"6": "Digit6",
"7": "Digit7",
"8": "Digit8",
"9": "Digit9",
"0": "Digit0",
// Option + 特殊字符映射
"": "Minus", // Option + -
"≠": "Equal", // Option + =
@@ -66,55 +46,11 @@ const mapKeyCombination = (key: string): string => {
const mappedKey = KEY_MAP[key] || key;
return `${mappedKey}`;
};
export const parseHotkey = (key: string) => {
export const parseHotkey = (keyEvent: KeyboardEvent) => {
const nativeEvent = keyEvent.nativeEvent;
const key = nativeEvent.code;
let temp = key.toUpperCase();
// 处理特殊符号到键位的映射
switch (temp) {
// 数字键符号
case "!":
return "DIGIT1"; // shift + 1
case "@":
return "DIGIT2"; // shift + 2
case "#":
return "DIGIT3"; // shift + 3
case "$":
return "DIGIT4"; // shift + 4
case "%":
return "DIGIT5"; // shift + 5
case "^":
return "DIGIT6"; // shift + 6
case "&":
return "DIGIT7"; // shift + 7
case "*":
return "DIGIT8"; // shift + 8
case "(":
return "DIGIT9"; // shift + 9
case ")":
return "DIGIT0"; // shift + 0
// 其他特殊符号
case "?":
return "SLASH"; // shift + /
case ":":
return "SEMICOLON"; // shift + ;
case "+":
return "EQUAL"; // shift + =
case "_":
return "MINUS"; // shift + -
case '"':
return "QUOTE"; // shift + '
case "<":
return "COMMA"; // shift + ,
case ">":
return "PERIOD"; // shift + .
case "{":
return "BRACKETLEFT"; // shift + [
case "}":
return "BRACKETRIGHT"; // shift + ]
case "|":
return "BACKSLASH"; // shift + \
}
if (temp.startsWith("ARROW")) {
temp = temp.slice(5);
} else if (temp.startsWith("DIGIT")) {