热键相关代码更新

This commit is contained in:
chao wan 2023-03-04 00:40:06 +08:00
parent c40d88d0b6
commit ba702ba041
5 changed files with 94 additions and 110 deletions

View file

@ -21,7 +21,7 @@ namespace v2rayN.Handler
} }
private Dictionary<int, List<EGlobalHotkey>> _hotkeyTriggerDic; private Dictionary<int, List<EGlobalHotkey>> _hotkeyTriggerDic;
public bool IsPause { get; private set; } = false; public bool IsPause { get; set; } = false;
public event Action<bool, string>? UpdateViewEvent; public event Action<bool, string>? UpdateViewEvent;
public event Action<EGlobalHotkey>? HotkeyTriggerEvent; public event Action<EGlobalHotkey>? HotkeyTriggerEvent;
public HotkeyHandler() public HotkeyHandler()
@ -61,26 +61,28 @@ namespace v2rayN.Handler
{ {
foreach(var hotkey in _hotkeyTriggerDic.Keys) foreach(var hotkey in _hotkeyTriggerDic.Keys)
{ {
var _fsModifiers = hotkey & 0xffff; var hotkeyInfo = GetHotkeyInfo(hotkey);
var _vkey = (hotkey >> 16) & 0xffff;
var hotkeyStr = HotkeyToString(_fsModifiers, _vkey);
bool isSuccess = false; bool isSuccess = false;
string msg; string msg;
Application.Current.Dispatcher.Invoke(() => Application.Current.Dispatcher.Invoke(() =>
{ {
isSuccess = RegisterHotKey(IntPtr.Zero, hotkey, _fsModifiers, _vkey); isSuccess = RegisterHotKey(IntPtr.Zero, hotkey, hotkeyInfo.fsModifiers, hotkeyInfo.vKey);
}); });
if (isSuccess) foreach (var name in hotkeyInfo.Names)
{ {
msg = string.Format(ResUI.RegisterGlobalHotkeySuccessfully, $"{hotkeyStr}"); 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);
} }
else
{
var errInfo = new Win32Exception(Marshal.GetLastWin32Error()).Message;
msg = string.Format(ResUI.RegisterGlobalHotkeyFailed, $"{hotkeyStr}", errInfo);
}
UpdateViewEvent?.Invoke(false, msg);
} }
} }
@ -96,14 +98,38 @@ namespace v2rayN.Handler
Init(); Init();
Load(); Load();
} }
private (int fsModifiers, int vKey, string HotkeyStr, List<string> Names) GetHotkeyInfo(int hotkey)
{
var _fsModifiers = hotkey & 0xffff;
var _vkey = (hotkey >> 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.Values)
{
_names.Add(name.ToString()!);
}
return (_fsModifiers, _vkey, _hotkeyStr.ToString(), _names);
}
private void OnThreadPreProcessMessage(ref MSG msg, ref bool handled) private void OnThreadPreProcessMessage(ref MSG msg, ref bool handled)
{ {
if (msg.message != WmHotkey || IsPause || !_hotkeyTriggerDic.Keys.Contains((int)msg.lParam)) if (msg.message != WmHotkey|| !_hotkeyTriggerDic.ContainsKey((int)msg.lParam))
{
return; return;
}
handled = true; handled = true;
foreach (var keyEvent in _hotkeyTriggerDic[(int)msg.lParam]) foreach (var keyEvent in _hotkeyTriggerDic[(int)msg.lParam])
{ {
if (IsPause) return;
HotkeyTriggerEvent?.Invoke(keyEvent); HotkeyTriggerEvent?.Invoke(keyEvent);
} }
} }
@ -112,17 +138,6 @@ namespace v2rayN.Handler
[DllImport("user32.dll", SetLastError = true)] [DllImport("user32.dll", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id); private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private static string HotkeyToString(int fsModifiers,int vk)
{
var sb = new StringBuilder();
var mdif = (KeyModifiers)fsModifiers;
var key = KeyInterop.KeyFromVirtualKey(vk);
if ((mdif | KeyModifiers.Ctrl) == KeyModifiers.Ctrl) sb.Append($"{KeyModifiers.Ctrl}+");
if ((mdif | KeyModifiers.Alt) == KeyModifiers.Alt) sb.Append($"{KeyModifiers.Alt}+");
if ((mdif | KeyModifiers.Shift) == KeyModifiers.Shift) sb.Append($"{KeyModifiers.Shift}+");
sb.Append(key.ToString());
return sb.ToString();
}
[Flags] [Flags]
private enum KeyModifiers private enum KeyModifiers
{ {

View file

@ -1,5 +1,4 @@
using NHotkey; 
using NHotkey.Wpf;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Windows.Forms; using System.Windows.Forms;

View file

@ -3,7 +3,6 @@ using DynamicData.Binding;
using MaterialDesignColors; using MaterialDesignColors;
using MaterialDesignColors.ColorManipulation; using MaterialDesignColors.ColorManipulation;
using MaterialDesignThemes.Wpf; using MaterialDesignThemes.Wpf;
using NHotkey;
using ReactiveUI; using ReactiveUI;
using ReactiveUI.Fody.Helpers; using ReactiveUI.Fody.Helpers;
using Splat; using Splat;

View file

@ -1,4 +1,6 @@
using Microsoft.Win32.TaskScheduler; using Microsoft.Win32.TaskScheduler;
using System.Diagnostics;
using System.Text;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Input; using System.Windows.Input;
@ -11,9 +13,8 @@ namespace v2rayN.Views
{ {
public partial class GlobalHotkeySettingWindow public partial class GlobalHotkeySettingWindow
{ {
private static Config _config; private static Config _config = default!;
List<KeyEventItem> lstKey; private Dictionary<object, KeyEventItem> _TextBoxKeyEventItem = default!;
private Dictionary<object, KeyEventItem> _TextBoxKeyEventItem;
public GlobalHotkeySettingWindow() public GlobalHotkeySettingWindow()
{ {
@ -22,6 +23,20 @@ namespace v2rayN.Views
_config = LazyConfig.Instance.GetConfig(); _config = LazyConfig.Instance.GetConfig();
_config.globalHotkeys ??= new List<KeyEventItem>(); _config.globalHotkeys ??= new List<KeyEventItem>();
txtGlobalHotkey0.PreviewKeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey1.PreviewKeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey2.PreviewKeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey3.PreviewKeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey4.PreviewKeyDown += TxtGlobalHotkey_KeyDown;
this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false;
HotkeyHandler.Instance.IsPause = true;
Utils.SetDarkBorder(this, _config.uiItem.colorModeDark);
InitData();
}
private void InitData()
{
_TextBoxKeyEventItem = new() _TextBoxKeyEventItem = new()
{ {
{ txtGlobalHotkey0,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.ShowForm) }, { txtGlobalHotkey0,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.ShowForm) },
@ -30,35 +45,20 @@ namespace v2rayN.Views
{ txtGlobalHotkey3,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.SystemProxyUnchanged)}, { txtGlobalHotkey3,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.SystemProxyUnchanged)},
{ txtGlobalHotkey4,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.SystemProxyPac)} { txtGlobalHotkey4,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.SystemProxyPac)}
}; };
txtGlobalHotkey0.KeyDown += TxtGlobalHotkey_KeyDown; BindingData();
txtGlobalHotkey1.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey2.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey3.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey4.KeyDown += TxtGlobalHotkey_KeyDown;
BindingData(-1);
Utils.SetDarkBorder(this, _config.uiItem.colorModeDark);
} }
private void TxtGlobalHotkey_KeyDown(object sender, KeyEventArgs e) private void TxtGlobalHotkey_KeyDown(object sender, KeyEventArgs e)
{ {
Debug.WriteLine($"{e.Key}{e.SystemKey}");
e.Handled = true; e.Handled = true;
var _ModifierKeys = new Key[] { Key.LeftCtrl, Key.RightCtrl, Key.LeftShift, Key.RightShift, Key.LeftAlt, Key.RightAlt }; 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)) _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;
var txt = ((TextBox)sender); _TextBoxKeyEventItem[sender].Control = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control;
var index = Utils.ToInt(txt.Name.Substring(txt.Name.Length - 1, 1)); _TextBoxKeyEventItem[sender].Shift = (Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift;
var formsKey = e.Key == Key.System ? e.SystemKey : e.Key; (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;
BindingData(index);
}
} }
private KeyEventItem GetKeyEventItemByEGlobalHotkey(List<KeyEventItem> KELsit,EGlobalHotkey eg) private KeyEventItem GetKeyEventItemByEGlobalHotkey(List<KeyEventItem> KELsit,EGlobalHotkey eg)
@ -73,44 +73,40 @@ namespace v2rayN.Views
}); });
} }
private void BindingData(int index) private string KeyEventItemToString(KeyEventItem item)
{ {
for (int k = 0; k < lstKey.Count; k++) 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}");
return res.ToString();
}
private void BindingData()
{
foreach(var item in _TextBoxKeyEventItem)
{ {
if (index >= 0 && index != k) if (item.Value.KeyCode != null && item.Value.KeyCode != Key.None)
{ {
continue; (item.Key as TextBox)!.Text = KeyEventItemToString(item.Value);
} }
var item = lstKey[k]; else
var keys = string.Empty;
if (item.Control)
{ {
keys += $"{Forms.Keys.Control} + "; (item.Key as TextBox)!.Text = string.Empty;
} }
if (item.Alt)
{
keys += $"{Forms.Keys.Alt} + ";
}
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) private void btnSave_Click(object sender, RoutedEventArgs e)
{ {
_config.globalHotkeys = lstKey; _config.globalHotkeys = _TextBoxKeyEventItem.Values.ToList();
if (ConfigHandler.SaveConfig(ref _config, false) == 0) if (ConfigHandler.SaveConfig(ref _config, false) == 0)
{ {
HotkeyHandler.Instance.ReLoad();
this.DialogResult = true; this.DialogResult = true;
} }
else else
@ -126,37 +122,14 @@ namespace v2rayN.Views
private void btnReset_Click(object sender, RoutedEventArgs e) private void btnReset_Click(object sender, RoutedEventArgs e)
{ {
lstKey.Clear(); foreach(var k in _TextBoxKeyEventItem.Keys)
foreach (EGlobalHotkey it in Enum.GetValues(typeof(EGlobalHotkey)))
{ {
if (lstKey.FindIndex(t => t.eGlobalHotkey == it) >= 0) _TextBoxKeyEventItem[k].Alt = false;
{ _TextBoxKeyEventItem[k].Control= false;
continue; _TextBoxKeyEventItem[k].Shift = false;
} _TextBoxKeyEventItem[k].KeyCode = Key.None;
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;
}
}
} }
BindingData();
} }
private void GlobalHotkeySettingWindow_KeyDown(object sender, KeyEventArgs e) private void GlobalHotkeySettingWindow_KeyDown(object sender, KeyEventArgs e)

View file

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