Windows desktop version add global hotkey function
Some checks are pending
release Linux / build (Release) (push) Waiting to run
release macOS / build (Release) (push) Waiting to run
release Windows desktop (Avalonia UI) / build (Release) (push) Waiting to run
release Windows / build (Release) (push) Waiting to run

This commit is contained in:
2dust 2025-02-26 10:52:36 +08:00
parent add92cfa7c
commit a7f35d4495
5 changed files with 102 additions and 20 deletions

View file

@ -11,6 +11,7 @@
<PackageVersion Include="Avalonia.ReactiveUI" Version="11.2.4" />
<PackageVersion Include="CliWrap" Version="3.8.0" />
<PackageVersion Include="Downloader" Version="3.3.3" />
<PackageVersion Include="GlobalHotKeys.Windows" Version="0.1.0" />
<PackageVersion Include="H.NotifyIcon.Wpf" Version="2.2.0" />
<PackageVersion Include="MaterialDesignThemes" Version="5.2.1" />
<PackageVersion Include="MessageBox.Avalonia" Version="3.2.0" />

View file

@ -0,0 +1,88 @@
using System.Reactive.Linq;
using Avalonia.Input;
using Avalonia.ReactiveUI;
using Avalonia.Win32.Input;
using GlobalHotKeys;
using GlobalHotKeys.Native.Types;
namespace v2rayN.Desktop.Handler
{
public sealed class HotkeyHandler
{
private static readonly Lazy<HotkeyHandler> _instance = new(() => new());
public static HotkeyHandler Instance = _instance.Value;
private readonly Dictionary<int, EGlobalHotkey> _hotkeyTriggerDic = new();
private HotKeyManager? _hotKeyManager;
private Config? _config;
private event Action<EGlobalHotkey>? _updateFunc;
public bool IsPause { get; set; } = false;
public void Init(Config config, Action<EGlobalHotkey> updateFunc)
{
_config = config;
_updateFunc = updateFunc;
_hotKeyManager = new GlobalHotKeys.HotKeyManager();
Register();
}
public void Dispose()
{
_hotKeyManager?.Dispose();
}
private void Register()
{
_hotkeyTriggerDic.Clear();
foreach (var item in _config.GlobalHotkeys)
{
if (item.KeyCode is null or 0)
{
continue;
}
var vKey = KeyInterop.VirtualKeyFromKey((Key)item.KeyCode);
var modifiers = Modifiers.NoRepeat;
if (item.Control)
{
modifiers |= Modifiers.Control;
}
if (item.Shift)
{
modifiers |= Modifiers.Shift;
}
if (item.Alt)
{
modifiers |= Modifiers.Alt;
}
var result = _hotKeyManager?.Register((VirtualKeyCode)vKey, modifiers);
if (result?.IsSuccessful == true)
{
_hotkeyTriggerDic.Add(result.Id, item.EGlobalHotkey);
}
}
_hotKeyManager?.HotKeyPressed
.ObserveOn(AvaloniaScheduler.Instance)
.Subscribe(OnNext);
}
private void OnNext(HotKey key)
{
if (_updateFunc == null || IsPause)
{
return;
}
if (_hotkeyTriggerDic.TryGetValue(key.Id, out var value))
{
_updateFunc?.Invoke(value);
}
}
}
}

View file

@ -5,6 +5,7 @@ using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.ReactiveUI;
using ReactiveUI;
using v2rayN.Desktop.Handler;
namespace v2rayN.Desktop.Views
{
@ -20,8 +21,8 @@ namespace v2rayN.Desktop.Views
btnReset.Click += btnReset_Click;
//HotkeyHandler.Instance.IsPause = true;
//this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false;
HotkeyHandler.Instance.IsPause = true;
this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false;
btnCancel.Click += (s, e) => this.Close();
this.WhenActivated(disposables =>

View file

@ -12,6 +12,7 @@ using MsBox.Avalonia.Enums;
using ReactiveUI;
using Splat;
using v2rayN.Desktop.Common;
using v2rayN.Desktop.Handler;
namespace v2rayN.Desktop.Views
{
@ -138,8 +139,7 @@ namespace v2rayN.Desktop.Views
if (Utils.IsWindows())
{
ThreadPool.RegisterWaitForSingleObject(Program.ProgramStarted, OnProgramStarted, null, -1, false);
menuGlobalHotkeySetting.IsVisible = false;
HotkeyHandler.Instance.Init(_config, OnHotkeyHandler);
}
else
{
@ -156,7 +156,6 @@ namespace v2rayN.Desktop.Views
RestoreUI();
AddHelpMenuItem();
//WindowsHandler.Instance.RegisterGlobalHotkey(_config, OnHotkeyHandler, null);
MessageBus.Current.Listen<string>(EMsgCommand.AppExit.ToString()).Subscribe(StorageUI);
}
@ -233,6 +232,7 @@ namespace v2rayN.Desktop.Views
StorageUI();
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
HotkeyHandler.Instance.Dispose();
desktop.Shutdown();
}
break;
@ -268,21 +268,12 @@ namespace v2rayN.Desktop.Views
ShowHideWindow(null);
break;
//case EGlobalHotkey.SystemProxyClear:
// ViewModel?.SetListenerType(ESysProxyType.ForcedClear);
// break;
//case EGlobalHotkey.SystemProxySet:
// ViewModel?.SetListenerType(ESysProxyType.ForcedChange);
// break;
//case EGlobalHotkey.SystemProxyUnchanged:
// ViewModel?.SetListenerType(ESysProxyType.Unchanged);
// break;
//case EGlobalHotkey.SystemProxyPac:
// ViewModel?.SetListenerType(ESysProxyType.Pac);
// break;
case EGlobalHotkey.SystemProxyClear:
case EGlobalHotkey.SystemProxySet:
case EGlobalHotkey.SystemProxyUnchanged:
case EGlobalHotkey.SystemProxyPac:
Locator.Current.GetService<StatusBarViewModel>()?.SetListenerType((ESysProxyType)((int)e - 1));
break;
}
}

View file

@ -26,6 +26,7 @@
<PackageReference Include="ReactiveUI.Fody">
<TreatAsUsed>true</TreatAsUsed>
</PackageReference>
<PackageReference Include="GlobalHotKeys.Windows" />
</ItemGroup>
<ItemGroup>