Update files

Signed-off-by: Lev Rusanov <30170278+JDM170@users.noreply.github.com>
This commit is contained in:
2025-09-26 13:14:42 +07:00
parent bcc119d4fe
commit ab3eef14f3
2 changed files with 14 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -20,10 +21,13 @@ namespace EpisodeRenamer
[DllImport("user32.dll")] [DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd); private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
#endregion #endregion
#region Key Codes #region Key Codes
private static class VirtualKeyCodes public static class VirtualKeyCodes
{ {
public const int VK_CONTROL = 0x11; public const int VK_CONTROL = 0x11;
public const int VK_ALT = 0x12; public const int VK_ALT = 0x12;
@@ -58,6 +62,7 @@ namespace EpisodeRenamer
private bool _isDisposed; private bool _isDisposed;
private readonly object _lockObject = new object(); private readonly object _lockObject = new object();
private bool _isRunning; private bool _isRunning;
private IntPtr consoleHandle = Process.GetCurrentProcess().MainWindowHandle;
#endregion #endregion
#region Key Combination Class #region Key Combination Class
@@ -81,10 +86,6 @@ namespace EpisodeRenamer
{ {
_keyCombinations = new List<KeyCombination>(); _keyCombinations = new List<KeyCombination>();
_cancellationTokenSource = new CancellationTokenSource(); _cancellationTokenSource = new CancellationTokenSource();
// Добавляем комбинацию по умолчанию
RegisterCombination("ControlAltR",
new[] { VirtualKeyCodes.VK_CONTROL, VirtualKeyCodes.VK_ALT, VirtualKeyCodes.VK_R });
} }
#endregion #endregion
@@ -156,10 +157,8 @@ namespace EpisodeRenamer
try try
{ {
// Переводим консоль на передний план для лучшего захвата клавиш // Переводим консоль на передний план для лучшего захвата клавиш
BringConsoleToFront();
_listenerTask = Task.Run(() => ListenForHotkeys(_cancellationTokenSource.Token), _listenerTask = Task.Run(() => ListenForHotkeys(_cancellationTokenSource.Token),
_cancellationTokenSource.Token); _cancellationTokenSource.Token);
await Task.CompletedTask; await Task.CompletedTask;
} }
@@ -283,6 +282,8 @@ namespace EpisodeRenamer
private void TriggerHotkeyEvent(string combinationName) private void TriggerHotkeyEvent(string combinationName)
{ {
if (GetForegroundWindow() != consoleHandle)
return;
try try
{ {
OnHotkeyPressed?.Invoke(combinationName); OnHotkeyPressed?.Invoke(combinationName);
@@ -293,20 +294,6 @@ namespace EpisodeRenamer
$"Ошибка при обработке события для комбинации {combinationName}", ex)); $"Ошибка при обработке события для комбинации {combinationName}", ex));
} }
} }
private void BringConsoleToFront()
{
try
{
var consoleHandle = GetConsoleWindow();
SetForegroundWindow(consoleHandle);
}
catch (Exception ex)
{
// Не критичная ошибка, просто логируем
OnError?.Invoke(new InvalidOperationException("Не удалось перевести консоль на передний план", ex));
}
}
#endregion #endregion
#region IDisposable Implementation #region IDisposable Implementation

View File

@@ -4,6 +4,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace EpisodeRenamer namespace EpisodeRenamer
@@ -57,6 +58,7 @@ namespace EpisodeRenamer
break; break;
case "ControlC": case "ControlC":
Console.WriteLine("\nВыход из программы."); Console.WriteLine("\nВыход из программы.");
Thread.Sleep(1500);
Environment.Exit(0); Environment.Exit(0);
break; break;
default: default:
@@ -64,7 +66,9 @@ namespace EpisodeRenamer
} }
}; };
hotkeyListener.OnError += (ex) => Console.WriteLine($"Ошибка: {ex.Message}"); 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(); await hotkeyListener.StartListeningAsync();