From ab3eef14f3bc01e68320f042dad053887ed266e4 Mon Sep 17 00:00:00 2001 From: Lev Rusanov <30170278+JDM170@users.noreply.github.com> Date: Fri, 26 Sep 2025 13:14:42 +0700 Subject: [PATCH] Update files Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com> --- KeyListener.cs | 31 +++++++++---------------------- Program.cs | 6 +++++- 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/KeyListener.cs b/KeyListener.cs index fee87a5..7152e69 100644 --- a/KeyListener.cs +++ b/KeyListener.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; @@ -20,10 +21,13 @@ namespace EpisodeRenamer [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + private static extern IntPtr GetForegroundWindow(); #endregion #region Key Codes - private static class VirtualKeyCodes + public static class VirtualKeyCodes { public const int VK_CONTROL = 0x11; public const int VK_ALT = 0x12; @@ -58,6 +62,7 @@ namespace EpisodeRenamer private bool _isDisposed; private readonly object _lockObject = new object(); private bool _isRunning; + private IntPtr consoleHandle = Process.GetCurrentProcess().MainWindowHandle; #endregion #region Key Combination Class @@ -81,10 +86,6 @@ namespace EpisodeRenamer { _keyCombinations = new List(); _cancellationTokenSource = new CancellationTokenSource(); - - // Добавляем комбинацию по умолчанию - RegisterCombination("ControlAltR", - new[] { VirtualKeyCodes.VK_CONTROL, VirtualKeyCodes.VK_ALT, VirtualKeyCodes.VK_R }); } #endregion @@ -156,10 +157,8 @@ namespace EpisodeRenamer try { // Переводим консоль на передний план для лучшего захвата клавиш - BringConsoleToFront(); - _listenerTask = Task.Run(() => ListenForHotkeys(_cancellationTokenSource.Token), - _cancellationTokenSource.Token); + _cancellationTokenSource.Token); await Task.CompletedTask; } @@ -283,6 +282,8 @@ namespace EpisodeRenamer private void TriggerHotkeyEvent(string combinationName) { + if (GetForegroundWindow() != consoleHandle) + return; try { OnHotkeyPressed?.Invoke(combinationName); @@ -293,20 +294,6 @@ namespace EpisodeRenamer $"Ошибка при обработке события для комбинации {combinationName}", ex)); } } - - private void BringConsoleToFront() - { - try - { - var consoleHandle = GetConsoleWindow(); - SetForegroundWindow(consoleHandle); - } - catch (Exception ex) - { - // Не критичная ошибка, просто логируем - OnError?.Invoke(new InvalidOperationException("Не удалось перевести консоль на передний план", ex)); - } - } #endregion #region IDisposable Implementation diff --git a/Program.cs b/Program.cs index d099e25..45ce5ac 100644 --- a/Program.cs +++ b/Program.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.IO; using System.Reflection; using System.Text.RegularExpressions; +using System.Threading; using System.Threading.Tasks; namespace EpisodeRenamer @@ -57,6 +58,7 @@ namespace EpisodeRenamer break; case "ControlC": Console.WriteLine("\nВыход из программы."); + Thread.Sleep(1500); Environment.Exit(0); break; default: @@ -64,7 +66,9 @@ namespace EpisodeRenamer } }; hotkeyListener.OnError += (ex) => Console.WriteLine($"Ошибка: {ex.Message}"); - hotkeyListener.RegisterCombination("ControlC", new[] { 0x11, 0x43 }); + hotkeyListener.RegisterCombination("ControlAltR", + new[] { HotkeyListener.VirtualKeyCodes.VK_CONTROL, HotkeyListener.VirtualKeyCodes.VK_ALT, HotkeyListener.VirtualKeyCodes.VK_R }); + hotkeyListener.RegisterCombination("ControlC", new[] { HotkeyListener.VirtualKeyCodes.VK_CONTROL, 0x43 }); // Запускаем прослушивание await hotkeyListener.StartListeningAsync();