diff --git a/CommandHandler.cs b/CommandHandler.cs index f8b2a59..04fd8b4 100644 --- a/CommandHandler.cs +++ b/CommandHandler.cs @@ -26,10 +26,9 @@ namespace DualScreenDemo /// 遙控器接收資料 /// public async Task ProcessData(string indata) - { - AddToHistory(indata); + { AddToHistory(indata); // 遙控器測試 - Console.WriteLine("遙控器: " + indata); + // Console.WriteLine($"[{DateTime.Now:HH:mm:ss.fff}] 遙控器: {indata}"); switch (indata) { case "A261A4": // 輸入 @@ -70,12 +69,12 @@ namespace DualScreenDemo // 原唱 case "A26CA4": Console.WriteLine("ToggleVocalRemoval Invoked"); - InvokeAction(() => VideoPlayerForm.Instance.ToggleVocalRemoval()); - InvokeAction(() => OverlayForm.MainForm.ShowOriginalSongLabel()); + SafeInvokeAction("A26CA4",() => VideoPlayerForm.Instance.ToggleVocalRemoval()); + SafeInvokeAction("A26CA4",() => OverlayForm.MainForm.ShowOriginalSongLabel()); break; // 導唱 case "A26EA4": - InvokeAction(() => VideoPlayerForm.Instance.ToggleVocalRemoval()); + SafeInvokeAction("A26EA4",() => VideoPlayerForm.Instance.ToggleVocalRemoval()); break; case "A26DA4": PauseOrResumeSong(); @@ -88,106 +87,131 @@ namespace DualScreenDemo HandleArtistAnnouncements(); break; case "A2B3A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowVolumeUpLabel()); + SafeInvokeAction("A2B3A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A2B3A4",() => OverlayForm.MainForm.ShowVolumeUpLabel()); break; case "A2B4A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowVolumeDownLabel()); + SafeInvokeAction("A2B4A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A2B4A4",() => OverlayForm.MainForm.ShowVolumeDownLabel()); break; case "A2B5A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowMicUpLabel()); + SafeInvokeAction("A2B5A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A2B5A4",() => OverlayForm.MainForm.ShowMicUpLabel()); break; case "A2B6A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowMicDownLabel()); + SafeInvokeAction("A2B6A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A2B6A4",() => OverlayForm.MainForm.ShowMicDownLabel()); break; case "A2C2A4": - InvokeAction(() => OverlayForm.MainForm.HidemicLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowStandardLabel()); + SafeInvokeAction("A2C2A4",() => OverlayForm.MainForm.HidemicLabels()); + SafeInvokeAction("A2C2A4",() => OverlayForm.MainForm.ShowStandardLabel()); break; case "A2C3A4": - InvokeAction(() => OverlayForm.MainForm.HidemicLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowProfessionalLabel()); + SafeInvokeAction("A2C3A4",() => OverlayForm.MainForm.HidemicLabels()); + SafeInvokeAction("A2C3A4",() => OverlayForm.MainForm.ShowProfessionalLabel()); break; case "A2C4A4": - InvokeAction(() => OverlayForm.MainForm.HidemicLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowSquareLabel()); + SafeInvokeAction("A2C4A4",() => OverlayForm.MainForm.HidemicLabels()); + SafeInvokeAction("A2C4A4",() => OverlayForm.MainForm.ShowSquareLabel()); break; case "A2C1A4": - InvokeAction(() => OverlayForm.MainForm.HidemicLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowSingDownLabel()); + SafeInvokeAction("A2C1A4",() => OverlayForm.MainForm.HidemicLabels()); + SafeInvokeAction("A2C1A4",() => OverlayForm.MainForm.ShowSingDownLabel()); break; case "A2D5A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowBrightLabel()); + SafeInvokeAction("A2D5A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A2D5A4",() => OverlayForm.MainForm.ShowBrightLabel()); break; case "A2D7A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowRomanticLabel()); + SafeInvokeAction("A2D7A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A2D7A4",() => OverlayForm.MainForm.ShowRomanticLabel()); break; - /* case "A27CA4": - InvokeAction(() => OverlayForm.MainForm.ShowMaleKeyLabel()); - break; - case "A282A4": - InvokeAction(() => OverlayForm.MainForm.ShowFemaleKeyLabel()); - break;*/ + /* case "A27CA4": + InvokeAction(() => OverlayForm.MainForm.ShowMaleKeyLabel()); + break; + case "A282A4": + InvokeAction(() => OverlayForm.MainForm.ShowFemaleKeyLabel()); + break;*/ case "A2D6A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowSoftLabel()); + SafeInvokeAction("A2D6A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A2D6A4",() => OverlayForm.MainForm.ShowSoftLabel()); break; case "A2D8A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowDynamicLabel()); + SafeInvokeAction("A2D8A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A2D8A4",() => OverlayForm.MainForm.ShowDynamicLabel()); break; case "A275A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowTintLabel()); + SafeInvokeAction("A275A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A275A4",() => OverlayForm.MainForm.ShowTintLabel()); break; case "A283A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowKeyUpLabel("↑升4調")); - break; + SafeInvokeAction("A283A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A283A4",() => OverlayForm.MainForm.ShowKeyUpLabel("↑升4調")); + break; case "A282A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowKeyUpLabel("↑升3調")); + SafeInvokeAction("A282A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A282A4",() => OverlayForm.MainForm.ShowKeyUpLabel("↑升3調")); break; case "A281A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowKeyUpLabel("↑升2調")); + SafeInvokeAction("A281A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A281A4",() => OverlayForm.MainForm.ShowKeyUpLabel("↑升2調")); break; case "A280A4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowKeyUpLabel("↑升1調")); + SafeInvokeAction("A280A4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A280A4",() => OverlayForm.MainForm.ShowKeyUpLabel("↑升1調")); break; case "A27FA4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowStandardKeyLabel()); + SafeInvokeAction("A27FA4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A27FA4",() => OverlayForm.MainForm.ShowStandardKeyLabel()); break; case "A27EA4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowKeyDownLabel("↓降1調")); + SafeInvokeAction("A27EA4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A27EA4",() => OverlayForm.MainForm.ShowKeyDownLabel("↓降1調")); break; case "A27DA4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowKeyDownLabel("↓降2調")); + SafeInvokeAction("A27EA4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A27EA4",() => OverlayForm.MainForm.ShowKeyDownLabel("↓降2調")); break; case "A27CA4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowKeyDownLabel("↓降3調")); + SafeInvokeAction("A27CA4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A27CA4",() => OverlayForm.MainForm.ShowKeyDownLabel("↓降3調")); break; case "A27BA4": - InvokeAction(() => OverlayForm.MainForm.HideAllLabels()); - InvokeAction(() => OverlayForm.MainForm.ShowKeyDownLabel("↓降4調")); + SafeInvokeAction("A27BA4",() => OverlayForm.MainForm.HideAllLabels()); + SafeInvokeAction("A27BA4",() => OverlayForm.MainForm.ShowKeyDownLabel("↓降4調")); break; default: - if (Regex.IsMatch(indata, @"^A23\d+A4$")) + if (Regex.IsMatch(indata, @"^A23\d+A4$")) { HandleNumberInput(indata); } break; } + + } + private Dictionary _recentCommands = new(); + private readonly TimeSpan _debounceInterval = TimeSpan.FromMilliseconds(300); // 最短觸發間隔 + private void SafeInvokeAction(string commandKey, Action action) + { + /*var now = DateTime.Now; + + if (_recentCommands.TryGetValue(commandKey, out DateTime lastTime)) + { + if (now - lastTime < _debounceInterval) + return; // 忽略短時間內的重複指令 + } + + _recentCommands[commandKey] = now; + */ + // 真正執行 UI 操作 + if (OverlayForm.MainForm.InvokeRequired) + { + OverlayForm.MainForm.BeginInvoke(action); + } + else + { + action(); + } } private void AddToHistory(string indata)