MCPcopy
hub / github.com/game1024/OpenSpeedy / ShortcutField

Function ShortcutField

src/components/ShortcutField.tsx:41–307  ·  view source on GitHub ↗
({ value, onChange, disable = false, minWidth = '10rem' }: ShortcutFieldProps)

Source from the content-addressed store, hash-verified

39}
40
41export default function ShortcutField({ value, onChange, disable = false, minWidth = '10rem' }: ShortcutFieldProps) {
42 const { t } = useTranslation();
43 const [isRecording, setIsRecording] = useState(false);
44 const [currentCombo, setCurrentCombo] = useState('');
45 const [isHovered, setIsHovered] = useState(false);
46 const { notify } = useSnackbar();
47 const inputRef = useRef<HTMLDivElement>(null);
48 const osRef = useRef<string>("");
49
50 // Load platform once
51 useEffect(() => { osRef.current = platform(); }, []);
52
53 // 处理快捷键输入
54 const handleShortcutKeyDown = (event: React.KeyboardEvent<HTMLDivElement>) => {
55 if (!isRecording) return;
56
57 event.preventDefault();
58 event.stopPropagation();
59
60 // 构建当前按键组合用于实时显示
61 const keys: string[] = [];
62
63 if (event.ctrlKey) keys.push('Ctrl');
64 if (event.shiftKey) keys.push('Shift');
65 if (event.altKey) keys.push('Alt');
66
67 // Windows 不支持 Meta 键(Win 键)作为全局快捷键
68 const os = osRef.current;
69 if (event.metaKey) {
70 switch (os) {
71 case 'windows':
72 notify('不支持使用 Win 键作为快捷键', 'warning');
73 return;
74 case 'macos':
75 keys.push('Command');
76 break;
77 case 'linux':
78 notify('不支持使用 Meta 键作为快捷键', 'warning');
79 break;
80 }
81 }
82
83 // 过滤掉单独的修饰键
84 const isModifierKey = ['Control', 'Shift', 'Alt', 'Meta'].includes(event.key);
85
86 if (!isModifierKey) {
87 // 使用 event.code 来获取物理按键,避免 Shift 等修饰键影响
88 let mainKey = event.key;
89
90 // 处理数字键(避免 Shift+1 变成 !)
91 if (event.code.startsWith('Digit')) {
92 mainKey = event.code.replace('Digit', '');
93 }
94 // 处理字母键
95 else if (event.code.startsWith('Key')) {
96 mainKey = event.code.replace('Key', '');
97 }
98 // 处理特殊键

Callers

nothing calls this directly

Calls 1

useSnackbarFunction · 0.90

Tested by

no test coverage detected