mirror of
https://github.com/2dust/v2rayN.git
synced 2025-04-19 13:42:24 +00:00
Improved global hotkey setting
This commit is contained in:
parent
cdfb621c59
commit
4ae25b2f34
3 changed files with 96 additions and 73 deletions
|
@ -11,15 +11,8 @@ namespace v2rayN.Handler
|
|||
{
|
||||
private static readonly Lazy<HotkeyHandler> _instance = new(() => new());
|
||||
public static HotkeyHandler Instance = _instance.Value;
|
||||
|
||||
private const int WmHotkey = 0x0312;
|
||||
|
||||
private Config _config
|
||||
{
|
||||
get => AppHandler.Instance.Config;
|
||||
}
|
||||
|
||||
private Dictionary<int, List<EGlobalHotkey>> _hotkeyTriggerDic;
|
||||
private readonly Dictionary<int, List<EGlobalHotkey>> _hotkeyTriggerDic = new();
|
||||
|
||||
public bool IsPause { get; set; } = false;
|
||||
|
||||
|
@ -29,7 +22,6 @@ namespace v2rayN.Handler
|
|||
|
||||
public HotkeyHandler()
|
||||
{
|
||||
_hotkeyTriggerDic = new();
|
||||
ComponentDispatcher.ThreadPreprocessMessage += OnThreadPreProcessMessage;
|
||||
Init();
|
||||
}
|
||||
|
@ -37,20 +29,31 @@ namespace v2rayN.Handler
|
|||
private void Init()
|
||||
{
|
||||
_hotkeyTriggerDic.Clear();
|
||||
if (_config.GlobalHotkeys == null)
|
||||
if (AppHandler.Instance.Config.GlobalHotkeys == null)
|
||||
{
|
||||
return;
|
||||
foreach (var item in _config.GlobalHotkeys)
|
||||
}
|
||||
foreach (var item in AppHandler.Instance.Config.GlobalHotkeys)
|
||||
{
|
||||
if (item.KeyCode != null && (Key)item.KeyCode != Key.None)
|
||||
{
|
||||
int key = KeyInterop.VirtualKeyFromKey((Key)item.KeyCode);
|
||||
KeyModifiers modifiers = KeyModifiers.None;
|
||||
var key = KeyInterop.VirtualKeyFromKey((Key)item.KeyCode);
|
||||
var modifiers = KeyModifiers.None;
|
||||
if (item.Control)
|
||||
{
|
||||
modifiers |= KeyModifiers.Ctrl;
|
||||
}
|
||||
|
||||
if (item.Shift)
|
||||
{
|
||||
modifiers |= KeyModifiers.Shift;
|
||||
}
|
||||
|
||||
if (item.Alt)
|
||||
{
|
||||
modifiers |= KeyModifiers.Alt;
|
||||
}
|
||||
|
||||
key = (key << 16) | (int)modifiers;
|
||||
if (!_hotkeyTriggerDic.ContainsKey(key))
|
||||
{
|
||||
|
@ -59,7 +62,9 @@ namespace v2rayN.Handler
|
|||
else
|
||||
{
|
||||
if (!_hotkeyTriggerDic[key].Contains(item.EGlobalHotkey))
|
||||
{
|
||||
_hotkeyTriggerDic[key].Add(item.EGlobalHotkey);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -70,8 +75,8 @@ namespace v2rayN.Handler
|
|||
foreach (var _hotkeyCode in _hotkeyTriggerDic.Keys)
|
||||
{
|
||||
var hotkeyInfo = GetHotkeyInfo(_hotkeyCode);
|
||||
bool isSuccess = false;
|
||||
string msg;
|
||||
var isSuccess = false;
|
||||
var msg = string.Empty;
|
||||
|
||||
Application.Current?.Dispatcher.Invoke(() =>
|
||||
{
|
||||
|
@ -106,29 +111,38 @@ namespace v2rayN.Handler
|
|||
Load();
|
||||
}
|
||||
|
||||
private (int fsModifiers, int vKey, string hotkeyStr, List<string> Names) GetHotkeyInfo(int hotkeycode)
|
||||
private (int fsModifiers, int vKey, string hotkeyStr, List<string> Names) GetHotkeyInfo(int hotkeyCode)
|
||||
{
|
||||
var _fsModifiers = hotkeycode & 0xffff;
|
||||
var _vkey = (hotkeycode >> 16) & 0xffff;
|
||||
var _hotkeyStr = new StringBuilder();
|
||||
var _names = new List<string>();
|
||||
var fsModifiers = hotkeyCode & 0xffff;
|
||||
var vKey = (hotkeyCode >> 16) & 0xffff;
|
||||
var hotkeyStr = new StringBuilder();
|
||||
var names = new List<string>();
|
||||
|
||||
var mdif = (KeyModifiers)_fsModifiers;
|
||||
var key = KeyInterop.KeyFromVirtualKey(_vkey);
|
||||
if ((mdif & KeyModifiers.Ctrl) == KeyModifiers.Ctrl)
|
||||
_hotkeyStr.Append($"{KeyModifiers.Ctrl}+");
|
||||
if ((mdif & KeyModifiers.Alt) == KeyModifiers.Alt)
|
||||
_hotkeyStr.Append($"{KeyModifiers.Alt}+");
|
||||
if ((mdif & KeyModifiers.Shift) == KeyModifiers.Shift)
|
||||
_hotkeyStr.Append($"{KeyModifiers.Shift}+");
|
||||
_hotkeyStr.Append(key.ToString());
|
||||
|
||||
foreach (var name in _hotkeyTriggerDic[hotkeycode])
|
||||
var modify = (KeyModifiers)fsModifiers;
|
||||
var key = KeyInterop.KeyFromVirtualKey(vKey);
|
||||
if ((modify & KeyModifiers.Ctrl) == KeyModifiers.Ctrl)
|
||||
{
|
||||
_names.Add(name.ToString());
|
||||
hotkeyStr.Append($"{KeyModifiers.Ctrl}+");
|
||||
}
|
||||
|
||||
return (_fsModifiers, _vkey, _hotkeyStr.ToString(), _names);
|
||||
if ((modify & KeyModifiers.Alt) == KeyModifiers.Alt)
|
||||
{
|
||||
hotkeyStr.Append($"{KeyModifiers.Alt}+");
|
||||
}
|
||||
|
||||
if ((modify & KeyModifiers.Shift) == KeyModifiers.Shift)
|
||||
{
|
||||
hotkeyStr.Append($"{KeyModifiers.Shift}+");
|
||||
}
|
||||
|
||||
hotkeyStr.Append(key.ToString());
|
||||
|
||||
foreach (var name in _hotkeyTriggerDic[hotkeyCode])
|
||||
{
|
||||
names.Add(name.ToString());
|
||||
}
|
||||
|
||||
return (fsModifiers, vKey, hotkeyStr.ToString(), names);
|
||||
}
|
||||
|
||||
private void OnThreadPreProcessMessage(ref MSG msg, ref bool handled)
|
||||
|
@ -138,21 +152,18 @@ namespace v2rayN.Handler
|
|||
return;
|
||||
}
|
||||
handled = true;
|
||||
var _hotKeyCode = (int)msg.lParam;
|
||||
var hotKeyCode = (int)msg.lParam;
|
||||
if (IsPause)
|
||||
{
|
||||
Application.Current?.Dispatcher.Invoke(() =>
|
||||
{
|
||||
UIElement? element = Keyboard.FocusedElement as UIElement;
|
||||
if (element != null)
|
||||
if (Keyboard.FocusedElement is UIElement element)
|
||||
{
|
||||
var _keyEventArgs = new KeyEventArgs(Keyboard.PrimaryDevice,
|
||||
PresentationSource.FromVisual(element), 0,
|
||||
KeyInterop.KeyFromVirtualKey(GetHotkeyInfo(_hotKeyCode).vKey))
|
||||
var keyEventArgs = new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(element), 0, KeyInterop.KeyFromVirtualKey(GetHotkeyInfo(hotKeyCode).vKey))
|
||||
{
|
||||
RoutedEvent = UIElement.KeyDownEvent
|
||||
};
|
||||
element.RaiseEvent(_keyEventArgs);
|
||||
element.RaiseEvent(keyEventArgs);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,18 +1,17 @@
|
|||
<reactiveui:ReactiveWindow
|
||||
x:Class="v2rayN.Views.GlobalHotkeySettingWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
|
||||
xmlns:reactiveui="http://reactiveui.net"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:reactiveui="http://reactiveui.net"
|
||||
xmlns:resx="clr-namespace:ServiceLib.Resx;assembly=ServiceLib"
|
||||
xmlns:vms="clr-namespace:ServiceLib.ViewModels;assembly=ServiceLib"
|
||||
Title="{x:Static resx:ResUI.menuSetting}"
|
||||
Width="700"
|
||||
Height="500"
|
||||
x:TypeArguments="vms:SubEditViewModel"
|
||||
KeyDown="GlobalHotkeySettingWindow_KeyDown"
|
||||
ShowInTaskbar="False"
|
||||
Style="{StaticResource WindowGlobal}"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
|
|
|
@ -8,8 +8,8 @@ namespace v2rayN.Views
|
|||
{
|
||||
public partial class GlobalHotkeySettingWindow
|
||||
{
|
||||
private static Config _config = default!;
|
||||
private Dictionary<object, KeyEventItem> _TextBoxKeyEventItem = default!;
|
||||
private static Config? _config;
|
||||
private Dictionary<object, KeyEventItem> _textBoxKeyEventItem = new();
|
||||
|
||||
public GlobalHotkeySettingWindow()
|
||||
{
|
||||
|
@ -17,7 +17,7 @@ namespace v2rayN.Views
|
|||
|
||||
this.Owner = Application.Current.MainWindow;
|
||||
_config = AppHandler.Instance.Config;
|
||||
_config.GlobalHotkeys ??= new List<KeyEventItem>();
|
||||
_config.GlobalHotkeys ??= new();
|
||||
|
||||
btnReset.Click += btnReset_Click;
|
||||
btnSave.Click += btnSave_ClickAsync;
|
||||
|
@ -36,7 +36,7 @@ namespace v2rayN.Views
|
|||
|
||||
private void InitData()
|
||||
{
|
||||
_TextBoxKeyEventItem = new()
|
||||
_textBoxKeyEventItem = new()
|
||||
{
|
||||
{ txtGlobalHotkey0,GetKeyEventItemByEGlobalHotkey(_config.GlobalHotkeys,EGlobalHotkey.ShowForm) },
|
||||
{ txtGlobalHotkey1,GetKeyEventItemByEGlobalHotkey(_config.GlobalHotkeys,EGlobalHotkey.SystemProxyClear) },
|
||||
|
@ -44,24 +44,32 @@ namespace v2rayN.Views
|
|||
{ txtGlobalHotkey3,GetKeyEventItemByEGlobalHotkey(_config.GlobalHotkeys,EGlobalHotkey.SystemProxyUnchanged)},
|
||||
{ txtGlobalHotkey4,GetKeyEventItemByEGlobalHotkey(_config.GlobalHotkeys,EGlobalHotkey.SystemProxyPac)}
|
||||
};
|
||||
|
||||
BindingData();
|
||||
}
|
||||
|
||||
private void TxtGlobalHotkey_PreviewKeyDown(object sender, KeyEventArgs e)
|
||||
private void TxtGlobalHotkey_PreviewKeyDown(object? sender, KeyEventArgs e)
|
||||
{
|
||||
e.Handled = true;
|
||||
var _ModifierKeys = new Key[] { Key.LeftCtrl, Key.RightCtrl, Key.LeftShift,
|
||||
Key.RightShift, Key.LeftAlt, Key.RightAlt, Key.LWin, Key.RWin};
|
||||
_TextBoxKeyEventItem[sender].KeyCode = (int)(e.Key == Key.System ? (_ModifierKeys.Contains(e.SystemKey) ? Key.None : e.SystemKey) : (_ModifierKeys.Contains(e.Key) ? Key.None : e.Key));
|
||||
_TextBoxKeyEventItem[sender].Alt = (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt;
|
||||
_TextBoxKeyEventItem[sender].Control = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control;
|
||||
_TextBoxKeyEventItem[sender].Shift = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
|
||||
(sender as TextBox)!.Text = KeyEventItemToString(_TextBoxKeyEventItem[sender]);
|
||||
if (sender is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var item = _textBoxKeyEventItem[sender];
|
||||
var modifierKeys = new Key[] { Key.LeftCtrl, Key.RightCtrl, Key.LeftShift, Key.RightShift, Key.LeftAlt, Key.RightAlt, Key.LWin, Key.RWin };
|
||||
|
||||
item.KeyCode = (int)(e.Key == Key.System ? (modifierKeys.Contains(e.SystemKey) ? Key.None : e.SystemKey) : (modifierKeys.Contains(e.Key) ? Key.None : e.Key));
|
||||
item.Alt = (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt;
|
||||
item.Control = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control;
|
||||
item.Shift = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
|
||||
|
||||
(sender as TextBox)!.Text = KeyEventItemToString(item);
|
||||
}
|
||||
|
||||
private KeyEventItem GetKeyEventItemByEGlobalHotkey(List<KeyEventItem> KEList, EGlobalHotkey eg)
|
||||
private KeyEventItem GetKeyEventItemByEGlobalHotkey(List<KeyEventItem> lstKey, EGlobalHotkey eg)
|
||||
{
|
||||
return JsonUtils.DeepCopy(KEList.Find((it) => it.EGlobalHotkey == eg) ?? new()
|
||||
return JsonUtils.DeepCopy(lstKey.Find((it) => it.EGlobalHotkey == eg) ?? new()
|
||||
{
|
||||
EGlobalHotkey = eg,
|
||||
Control = false,
|
||||
|
@ -76,20 +84,31 @@ namespace v2rayN.Views
|
|||
var res = new StringBuilder();
|
||||
|
||||
if (item.Control)
|
||||
{
|
||||
res.Append($"{ModifierKeys.Control}+");
|
||||
}
|
||||
|
||||
if (item.Shift)
|
||||
{
|
||||
res.Append($"{ModifierKeys.Shift}+");
|
||||
}
|
||||
|
||||
if (item.Alt)
|
||||
{
|
||||
res.Append($"{ModifierKeys.Alt}+");
|
||||
}
|
||||
|
||||
if (item.KeyCode != null && (Key)item.KeyCode != Key.None)
|
||||
{
|
||||
res.Append($"{(Key)item.KeyCode}");
|
||||
}
|
||||
|
||||
return res.ToString();
|
||||
}
|
||||
|
||||
private void BindingData()
|
||||
{
|
||||
foreach (var item in _TextBoxKeyEventItem)
|
||||
foreach (var item in _textBoxKeyEventItem)
|
||||
{
|
||||
if (item.Value.KeyCode != null && (Key)item.Value.KeyCode != Key.None)
|
||||
{
|
||||
|
@ -104,7 +123,7 @@ namespace v2rayN.Views
|
|||
|
||||
private async void btnSave_ClickAsync(object sender, RoutedEventArgs e)
|
||||
{
|
||||
_config.GlobalHotkeys = _TextBoxKeyEventItem.Values.ToList();
|
||||
_config.GlobalHotkeys = _textBoxKeyEventItem.Values.ToList();
|
||||
|
||||
if (await ConfigHandler.SaveConfig(_config) == 0)
|
||||
{
|
||||
|
@ -119,22 +138,16 @@ namespace v2rayN.Views
|
|||
|
||||
private void btnReset_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
foreach (var k in _TextBoxKeyEventItem.Keys)
|
||||
foreach (var k in _textBoxKeyEventItem.Keys)
|
||||
{
|
||||
_TextBoxKeyEventItem[k].Alt = false;
|
||||
_TextBoxKeyEventItem[k].Control = false;
|
||||
_TextBoxKeyEventItem[k].Shift = false;
|
||||
_TextBoxKeyEventItem[k].KeyCode = (int)Key.None;
|
||||
var item = _textBoxKeyEventItem[k];
|
||||
|
||||
item.Alt = false;
|
||||
item.Control = false;
|
||||
item.Shift = false;
|
||||
item.KeyCode = (int)Key.None;
|
||||
}
|
||||
BindingData();
|
||||
}
|
||||
|
||||
private void GlobalHotkeySettingWindow_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.Key == Key.Escape)
|
||||
{
|
||||
this.Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue