Merge pull request #3405 from hvvvvvvv/master

优化全局热键相关代码和机制
pull/3492/head^2
2dust 2 years ago committed by GitHub
commit df6179a1a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

1
.gitignore vendored

@ -16,3 +16,4 @@
/v2rayN/v2rayUpgrade/bin/Release
/v2rayN/v2rayUpgrade/obj/
*.user
/.vs/v2rayN

@ -0,0 +1,172 @@
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using v2rayN.Mode;
using v2rayN.Resx;
namespace v2rayN.Handler
{
public sealed class HotkeyHandler
{
private static readonly Lazy<HotkeyHandler> _instance = new(() => new());
public static HotkeyHandler Instance = _instance.Value;
private const int WmHotkey = 0x0312;
private Config _config
{
get => LazyConfig.Instance.GetConfig();
}
private Dictionary<int, List<EGlobalHotkey>> _hotkeyTriggerDic;
public bool IsPause { get; set; } = false;
public event Action<bool, string>? UpdateViewEvent;
public event Action<EGlobalHotkey>? HotkeyTriggerEvent;
public HotkeyHandler()
{
_hotkeyTriggerDic = new();
ComponentDispatcher.ThreadPreprocessMessage += OnThreadPreProcessMessage;
Init();
}
private void Init()
{
_hotkeyTriggerDic.Clear();
if (_config.globalHotkeys == null) return;
foreach(var item in _config.globalHotkeys)
{
if (item.KeyCode != null && item.KeyCode != Key.None)
{
int key = KeyInterop.VirtualKeyFromKey((Key)item.KeyCode);
KeyModifiers 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))
{
_hotkeyTriggerDic.Add(key, new() { item.eGlobalHotkey });
}
else
{
if (!_hotkeyTriggerDic[key].Contains(item.eGlobalHotkey))
_hotkeyTriggerDic[key].Add(item.eGlobalHotkey);
}
}
}
}
public void Load()
{
foreach(var _hotkeyCode in _hotkeyTriggerDic.Keys)
{
var hotkeyInfo = GetHotkeyInfo(_hotkeyCode);
bool isSuccess = false;
string msg;
Application.Current.Dispatcher.Invoke(() =>
{
isSuccess = RegisterHotKey(IntPtr.Zero, _hotkeyCode, hotkeyInfo.fsModifiers, hotkeyInfo.vKey);
});
foreach (var name in hotkeyInfo.Names)
{
if (isSuccess)
{
msg = string.Format(ResUI.RegisterGlobalHotkeySuccessfully, $"{name}({hotkeyInfo.hotkeyStr})");
}
else
{
var errInfo = new Win32Exception(Marshal.GetLastWin32Error()).Message;
msg = string.Format(ResUI.RegisterGlobalHotkeyFailed, $"{name}({hotkeyInfo.hotkeyStr})", errInfo);
}
UpdateViewEvent?.Invoke(false, msg);
}
}
}
public void ReLoad()
{
foreach(var hotkey in _hotkeyTriggerDic.Keys)
{
Application.Current.Dispatcher.Invoke(() =>
{
UnregisterHotKey(IntPtr.Zero, hotkey);
});
}
Init();
Load();
}
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 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])
{
_names.Add(name.ToString());
}
return (_fsModifiers, _vkey, _hotkeyStr.ToString(), _names);
}
private void OnThreadPreProcessMessage(ref MSG msg, ref bool handled)
{
if (msg.message != WmHotkey|| !_hotkeyTriggerDic.ContainsKey((int)msg.lParam))
{
return;
}
handled = true;
var _hotKeyCode = (int)msg.lParam;
if (IsPause)
{
Application.Current.Dispatcher.Invoke(() =>
{
UIElement? element = Keyboard.FocusedElement as UIElement;
if (element != null)
{
var _keyEventArgs = new KeyEventArgs(Keyboard.PrimaryDevice,
PresentationSource.FromVisual(element), 0,
KeyInterop.KeyFromVirtualKey(GetHotkeyInfo(_hotKeyCode).vKey))
{
RoutedEvent = UIElement.KeyDownEvent
};
element.RaiseEvent(_keyEventArgs);
}
});
}
else
{
foreach (var keyEvent in _hotkeyTriggerDic[(int)msg.lParam])
{
HotkeyTriggerEvent?.Invoke(keyEvent);
}
}
}
[DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
[Flags]
private enum KeyModifiers
{
None = 0x0000,
Alt = 0x0001,
Ctrl = 0x0002,
Shift = 0x0004,
Win = 0x0008,
NoRepeat = 0x4000
}
}
}

@ -1,5 +1,4 @@
using NHotkey;
using NHotkey.Wpf;

using System.Drawing;
using System.IO;
using System.Windows.Forms;
@ -343,48 +342,11 @@ namespace v2rayN.Handler
}
}
public void RegisterGlobalHotkey(Config config, EventHandler<HotkeyEventArgs> handler, Action<bool, string> update)
public void RegisterGlobalHotkey(Config config, Action<EGlobalHotkey> handler, Action<bool, string> update)
{
if (config.globalHotkeys == null)
{
return;
}
foreach (var item in config.globalHotkeys)
{
if (item.KeyCode == null)
{
continue;
}
var modifiers = ModifierKeys.None;
if (item.Control)
{
modifiers |= ModifierKeys.Control;
}
if (item.Alt)
{
modifiers |= ModifierKeys.Alt;
}
if (item.Shift)
{
modifiers |= ModifierKeys.Shift;
}
var gesture = new KeyGesture(KeyInterop.KeyFromVirtualKey((int)item.KeyCode), modifiers);
try
{
HotkeyManager.Current.AddOrReplace(((int)item.eGlobalHotkey).ToString(), gesture, handler);
var msg = string.Format(ResUI.RegisterGlobalHotkeySuccessfully, $"{item.eGlobalHotkey}");
update(false, msg);
}
catch (Exception ex)
{
var msg = string.Format(ResUI.RegisterGlobalHotkeyFailed, $"{item.eGlobalHotkey}", ex.Message);
update(false, msg);
Utils.SaveLog(msg);
}
}
HotkeyHandler.Instance.UpdateViewEvent += update;
HotkeyHandler.Instance.HotkeyTriggerEvent += handler;
HotkeyHandler.Instance.Load();
}
}

@ -1,5 +1,4 @@
using System.Windows.Forms;
using System.Windows.Input;
namespace v2rayN.Mode
{
[Serializable]
@ -144,7 +143,7 @@ namespace v2rayN.Mode
public bool Shift { get; set; }
public Keys? KeyCode { get; set; }
public Key? KeyCode { get; set; }
}

@ -3,7 +3,6 @@ using DynamicData.Binding;
using MaterialDesignColors;
using MaterialDesignColors.ColorManipulation;
using MaterialDesignThemes.Wpf;
using NHotkey;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;
using Splat;
@ -630,27 +629,26 @@ namespace v2rayN.ViewModels
}
}
private void OnHotkeyHandler(object sender, HotkeyEventArgs e)
private void OnHotkeyHandler(EGlobalHotkey e)
{
switch (Utils.ToInt(e.Name))
switch (e)
{
case (int)EGlobalHotkey.ShowForm:
case EGlobalHotkey.ShowForm:
ShowHideWindow(null);
break;
case (int)EGlobalHotkey.SystemProxyClear:
case EGlobalHotkey.SystemProxyClear:
SetListenerType(ESysProxyType.ForcedClear);
break;
case (int)EGlobalHotkey.SystemProxySet:
case EGlobalHotkey.SystemProxySet:
SetListenerType(ESysProxyType.ForcedChange);
break;
case (int)EGlobalHotkey.SystemProxyUnchanged:
case EGlobalHotkey.SystemProxyUnchanged:
SetListenerType(ESysProxyType.Unchanged);
break;
case (int)EGlobalHotkey.SystemProxyPac:
case EGlobalHotkey.SystemProxyPac:
SetListenerType(ESysProxyType.Pac);
break;
}
e.Handled = true;
}
public void MyAppExit(bool blWindowsShutDown)
{

@ -1,117 +1,111 @@
using System.Windows;
using Microsoft.Win32.TaskScheduler;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using v2rayN.Handler;
using v2rayN.Mode;
using v2rayN.Resx;
using Forms = System.Windows.Forms;
namespace v2rayN.Views
{
public partial class GlobalHotkeySettingWindow
{
private static Config _config;
List<KeyEventItem> lstKey;
private static Config _config = default!;
private Dictionary<object, KeyEventItem> _TextBoxKeyEventItem = default!;
public GlobalHotkeySettingWindow()
{
InitializeComponent();
this.Owner = Application.Current.MainWindow;
_config = LazyConfig.Instance.GetConfig();
if (_config.globalHotkeys == null)
{
_config.globalHotkeys = new List<KeyEventItem>();
}
foreach (EGlobalHotkey it in Enum.GetValues(typeof(EGlobalHotkey)))
{
if (_config.globalHotkeys.FindIndex(t => t.eGlobalHotkey == it) >= 0)
{
continue;
}
_config.globalHotkeys.Add(new KeyEventItem()
{
eGlobalHotkey = it,
Alt = false,
Control = false,
Shift = false,
KeyCode = null
});
}
lstKey = Utils.DeepCopy(_config.globalHotkeys);
_config.globalHotkeys ??= new List<KeyEventItem>();
txtGlobalHotkey0.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey1.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey2.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey3.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey4.KeyDown += TxtGlobalHotkey_KeyDown;
BindingData(-1);
HotkeyHandler.Instance.IsPause = true;
this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false;
Utils.SetDarkBorder(this, _config.uiItem.colorModeDark);
InitData();
}
private void InitData()
{
_TextBoxKeyEventItem = new()
{
{ txtGlobalHotkey0,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.ShowForm) },
{ txtGlobalHotkey1,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.SystemProxyClear) },
{ txtGlobalHotkey2,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.SystemProxySet) },
{ txtGlobalHotkey3,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.SystemProxyUnchanged)},
{ txtGlobalHotkey4,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.SystemProxyPac)}
};
BindingData();
}
private void TxtGlobalHotkey_KeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
var _ModifierKeys = new Key[] { Key.LeftCtrl, Key.RightCtrl, Key.LeftShift, Key.RightShift, Key.LeftAlt, Key.RightAlt };
if (!_ModifierKeys.Contains(e.Key) && !_ModifierKeys.Contains(e.SystemKey))
{
var txt = ((TextBox)sender);
var index = Utils.ToInt(txt.Name.Substring(txt.Name.Length - 1, 1));
var formsKey = (Forms.Keys)KeyInterop.VirtualKeyFromKey(e.Key == Key.System ? e.SystemKey : e.Key);
var _ModifierKeys = new Key[] { Key.LeftCtrl, Key.RightCtrl, Key.LeftShift,
Key.RightShift, Key.LeftAlt, Key.RightAlt, Key.LWin, Key.RWin};
_TextBoxKeyEventItem[sender].KeyCode = 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]);
}
lstKey[index].KeyCode = formsKey;
lstKey[index].Alt = (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt;
lstKey[index].Control = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control;
lstKey[index].Shift = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
private KeyEventItem GetKeyEventItemByEGlobalHotkey(List<KeyEventItem> KELsit,EGlobalHotkey eg)
{
return Utils.DeepCopy(KELsit.Find((it) => it.eGlobalHotkey == eg) ?? new()
{
eGlobalHotkey = eg,
Control = false,
Alt = false,
Shift = false,
KeyCode = null
});
BindingData(index);
}
}
private string KeyEventItemToString(KeyEventItem item)
{
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 && item.KeyCode != Key.None)
res.Append($"{item.KeyCode}");
private void BindingData(int index)
return res.ToString();
}
private void BindingData()
{
for (int k = 0; k < lstKey.Count; k++)
foreach(var item in _TextBoxKeyEventItem)
{
if (index >= 0 && index != k)
{
continue;
}
var item = lstKey[k];
var keys = string.Empty;
if (item.Control)
if (item.Value.KeyCode != null && item.Value.KeyCode != Key.None)
{
keys += $"{Forms.Keys.Control} + ";
(item.Key as TextBox)!.Text = KeyEventItemToString(item.Value);
}
if (item.Alt)
else
{
keys += $"{Forms.Keys.Alt} + ";
(item.Key as TextBox)!.Text = string.Empty;
}
if (item.Shift)
{
keys += $"{Forms.Keys.Shift} + ";
}
if (item.KeyCode != null)
{
keys += $"{item.KeyCode}";
}
SetText($"txtGlobalHotkey{k}", keys);
}
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
_config.globalHotkeys = lstKey;
_config.globalHotkeys = _TextBoxKeyEventItem.Values.ToList();
if (ConfigHandler.SaveConfig(ref _config, false) == 0)
{
HotkeyHandler.Instance.ReLoad();
this.DialogResult = true;
}
else
@ -127,37 +121,14 @@ namespace v2rayN.Views
private void btnReset_Click(object sender, RoutedEventArgs e)
{
lstKey.Clear();
foreach (EGlobalHotkey it in Enum.GetValues(typeof(EGlobalHotkey)))
foreach(var k in _TextBoxKeyEventItem.Keys)
{
if (lstKey.FindIndex(t => t.eGlobalHotkey == it) >= 0)
{
continue;
}
lstKey.Add(new KeyEventItem()
{
eGlobalHotkey = it,
Alt = false,
Control = false,
Shift = false,
KeyCode = null
});
}
BindingData(-1);
}
private void SetText(string name, string txt)
{
foreach (UIElement element in gridText.Children)
{
if (element is TextBox box)
{
if (box.Name == name)
{
box.Text = txt;
}
}
_TextBoxKeyEventItem[k].Alt = false;
_TextBoxKeyEventItem[k].Control= false;
_TextBoxKeyEventItem[k].Shift = false;
_TextBoxKeyEventItem[k].KeyCode = Key.None;
}
BindingData();
}
private void GlobalHotkeySettingWindow_KeyDown(object sender, KeyEventArgs e)

@ -18,8 +18,6 @@
<PackageReference Include="MaterialDesignThemes" Version="4.7.1" />
<PackageReference Include="Hardcodet.NotifyIcon.Wpf" Version="1.1.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="NHotkey" Version="2.1.0" />
<PackageReference Include="NHotkey.Wpf" Version="2.1.0" />
<PackageReference Include="QRCoder.Xaml" Version="1.4.3" />
<PackageReference Include="sqlite-net-pcl" Version="1.8.116" />
<PackageReference Include="TaskScheduler" Version="2.10.1" />

Loading…
Cancel
Save