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.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<KeyCombination>();
_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