From c40d88d0b6d0401a7f61cc9e6e8ce7155686571f Mon Sep 17 00:00:00 2001 From: chao wan <1013448513@qq.com> Date: Fri, 3 Mar 2023 18:03:29 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=83=AD=E9=94=AE?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E4=BB=A3=E7=A0=81=EF=BC=88=E6=9C=AA=E5=AE=8C?= =?UTF-8?q?=E6=88=90=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + v2rayN/v2rayN/Handler/HotkeyHandler.cs | 137 ++++++++++++++++++ v2rayN/v2rayN/Handler/MainFormHandler.cs | 45 +----- v2rayN/v2rayN/Mode/ConfigItems.cs | 5 +- .../v2rayN/ViewModels/MainWindowViewModel.cs | 15 +- .../Views/GlobalHotkeySettingWindow.xaml.cs | 49 +++---- 6 files changed, 175 insertions(+), 77 deletions(-) create mode 100644 v2rayN/v2rayN/Handler/HotkeyHandler.cs diff --git a/.gitignore b/.gitignore index 42698ddd9f..4f3e075c07 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ /v2rayN/v2rayUpgrade/bin/Release /v2rayN/v2rayUpgrade/obj/ *.user +/.vs/v2rayN diff --git a/v2rayN/v2rayN/Handler/HotkeyHandler.cs b/v2rayN/v2rayN/Handler/HotkeyHandler.cs new file mode 100644 index 0000000000..5b5a4e0f12 --- /dev/null +++ b/v2rayN/v2rayN/Handler/HotkeyHandler.cs @@ -0,0 +1,137 @@ +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 _instance = new(() => new()); + public static HotkeyHandler Instance = _instance.Value; + + private const int WmHotkey = 0x0312; + private Config _config + { + get => LazyConfig.Instance.GetConfig(); + } + private Dictionary> _hotkeyTriggerDic; + + public bool IsPause { get; private set; } = false; + public event Action? UpdateViewEvent; + public event Action? 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 hotkey in _hotkeyTriggerDic.Keys) + { + var _fsModifiers = hotkey & 0xffff; + var _vkey = (hotkey >> 16) & 0xffff; + var hotkeyStr = HotkeyToString(_fsModifiers, _vkey); + bool isSuccess = false; + string msg; + + Application.Current.Dispatcher.Invoke(() => + { + isSuccess = RegisterHotKey(IntPtr.Zero, hotkey, _fsModifiers, _vkey); + }); + if (isSuccess) + { + msg = string.Format(ResUI.RegisterGlobalHotkeySuccessfully, $"{hotkeyStr}"); + } + else + { + var errInfo = new Win32Exception(Marshal.GetLastWin32Error()).Message; + msg = string.Format(ResUI.RegisterGlobalHotkeyFailed, $"{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 void OnThreadPreProcessMessage(ref MSG msg, ref bool handled) + { + if (msg.message != WmHotkey || IsPause || !_hotkeyTriggerDic.Keys.Contains((int)msg.lParam)) + return; + handled = true; + 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); + 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] + private enum KeyModifiers + { + None = 0x0000, + Alt = 0x0001, + Ctrl = 0x0002, + Shift = 0x0004, + Win = 0x0008, + NoRepeat = 0x4000 + } + } +} diff --git a/v2rayN/v2rayN/Handler/MainFormHandler.cs b/v2rayN/v2rayN/Handler/MainFormHandler.cs index cdf7215c1e..e04d94663d 100644 --- a/v2rayN/v2rayN/Handler/MainFormHandler.cs +++ b/v2rayN/v2rayN/Handler/MainFormHandler.cs @@ -343,48 +343,11 @@ private void UpdateTaskRun(Config config, Action update) } } - public void RegisterGlobalHotkey(Config config, EventHandler handler, Action update) + public void RegisterGlobalHotkey(Config config, Action handler, Action 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(); } } diff --git a/v2rayN/v2rayN/Mode/ConfigItems.cs b/v2rayN/v2rayN/Mode/ConfigItems.cs index 3f9e9eacbb..198d3dd95d 100644 --- a/v2rayN/v2rayN/Mode/ConfigItems.cs +++ b/v2rayN/v2rayN/Mode/ConfigItems.cs @@ -1,5 +1,4 @@ -using System.Windows.Forms; - +using System.Windows.Input; namespace v2rayN.Mode { [Serializable] @@ -144,7 +143,7 @@ public class KeyEventItem public bool Shift { get; set; } - public Keys? KeyCode { get; set; } + public Key? KeyCode { get; set; } } diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index e50dc36a9b..c670f83dc3 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -630,27 +630,26 @@ private void SetTestResult(string indexId, string delay, string speed) } } - 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) { diff --git a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs index 16cc7fce3a..54b4828314 100644 --- a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs @@ -1,4 +1,5 @@ -using System.Windows; +using Microsoft.Win32.TaskScheduler; +using System.Windows; using System.Windows.Controls; using System.Windows.Input; using v2rayN.Handler; @@ -12,37 +13,23 @@ public partial class GlobalHotkeySettingWindow { private static Config _config; List lstKey; + private Dictionary _TextBoxKeyEventItem; public GlobalHotkeySettingWindow() { InitializeComponent(); this.Owner = Application.Current.MainWindow; _config = LazyConfig.Instance.GetConfig(); + _config.globalHotkeys ??= new List(); - if (_config.globalHotkeys == null) + _TextBoxKeyEventItem = new() { - _config.globalHotkeys = new List(); - } - - 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); - + { 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)} + }; txtGlobalHotkey0.KeyDown += TxtGlobalHotkey_KeyDown; txtGlobalHotkey1.KeyDown += TxtGlobalHotkey_KeyDown; txtGlobalHotkey2.KeyDown += TxtGlobalHotkey_KeyDown; @@ -63,7 +50,7 @@ private void TxtGlobalHotkey_KeyDown(object sender, KeyEventArgs e) { 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 formsKey = e.Key == Key.System ? e.SystemKey : e.Key; lstKey[index].KeyCode = formsKey; lstKey[index].Alt = (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt; @@ -74,6 +61,18 @@ private void TxtGlobalHotkey_KeyDown(object sender, KeyEventArgs e) } } + private KeyEventItem GetKeyEventItemByEGlobalHotkey(List KELsit,EGlobalHotkey eg) + { + return Utils.DeepCopy(KELsit.Find((it) => it.eGlobalHotkey == eg) ?? new() + { + eGlobalHotkey = eg, + Control = false, + Alt = false, + Shift = false, + KeyCode = null + }); + + } private void BindingData(int index) { for (int k = 0; k < lstKey.Count; k++) From ba702ba041c9e2a413512ea8e26d457c9def68be Mon Sep 17 00:00:00 2001 From: chao wan <1013448513@qq.com> Date: Sat, 4 Mar 2023 00:40:06 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=83=AD=E9=94=AE=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/v2rayN/Handler/HotkeyHandler.cs | 69 ++++++---- v2rayN/v2rayN/Handler/MainFormHandler.cs | 3 +- .../v2rayN/ViewModels/MainWindowViewModel.cs | 1 - .../Views/GlobalHotkeySettingWindow.xaml.cs | 129 +++++++----------- v2rayN/v2rayN/v2rayN.csproj | 2 - 5 files changed, 94 insertions(+), 110 deletions(-) diff --git a/v2rayN/v2rayN/Handler/HotkeyHandler.cs b/v2rayN/v2rayN/Handler/HotkeyHandler.cs index 5b5a4e0f12..55f6576bc0 100644 --- a/v2rayN/v2rayN/Handler/HotkeyHandler.cs +++ b/v2rayN/v2rayN/Handler/HotkeyHandler.cs @@ -21,7 +21,7 @@ private Config _config } private Dictionary> _hotkeyTriggerDic; - public bool IsPause { get; private set; } = false; + public bool IsPause { get; set; } = false; public event Action? UpdateViewEvent; public event Action? HotkeyTriggerEvent; public HotkeyHandler() @@ -61,26 +61,28 @@ public void Load() { foreach(var hotkey in _hotkeyTriggerDic.Keys) { - var _fsModifiers = hotkey & 0xffff; - var _vkey = (hotkey >> 16) & 0xffff; - var hotkeyStr = HotkeyToString(_fsModifiers, _vkey); + var hotkeyInfo = GetHotkeyInfo(hotkey); bool isSuccess = false; string msg; Application.Current.Dispatcher.Invoke(() => { - isSuccess = RegisterHotKey(IntPtr.Zero, hotkey, _fsModifiers, _vkey); - }); - if (isSuccess) - { - msg = string.Format(ResUI.RegisterGlobalHotkeySuccessfully, $"{hotkeyStr}"); - } - else + isSuccess = RegisterHotKey(IntPtr.Zero, hotkey, hotkeyInfo.fsModifiers, hotkeyInfo.vKey); + }); + foreach (var name in hotkeyInfo.Names) { - var errInfo = new Win32Exception(Marshal.GetLastWin32Error()).Message; - msg = string.Format(ResUI.RegisterGlobalHotkeyFailed, $"{hotkeyStr}", errInfo); + 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); } - UpdateViewEvent?.Invoke(false, msg); + } } @@ -96,14 +98,38 @@ public void ReLoad() Init(); Load(); } - + private (int fsModifiers, int vKey, string HotkeyStr, List Names) GetHotkeyInfo(int hotkey) + { + var _fsModifiers = hotkey & 0xffff; + var _vkey = (hotkey >> 16) & 0xffff; + var _hotkeyStr = new StringBuilder(); + var _names = new List(); + + 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) { - if (msg.message != WmHotkey || IsPause || !_hotkeyTriggerDic.Keys.Contains((int)msg.lParam)) + if (msg.message != WmHotkey|| !_hotkeyTriggerDic.ContainsKey((int)msg.lParam)) + { return; + } handled = true; foreach (var keyEvent in _hotkeyTriggerDic[(int)msg.lParam]) { + if (IsPause) return; HotkeyTriggerEvent?.Invoke(keyEvent); } } @@ -112,17 +138,6 @@ private void OnThreadPreProcessMessage(ref MSG msg, ref bool handled) [DllImport("user32.dll", SetLastError = true)] 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] private enum KeyModifiers { diff --git a/v2rayN/v2rayN/Handler/MainFormHandler.cs b/v2rayN/v2rayN/Handler/MainFormHandler.cs index e04d94663d..6f3a47b8ec 100644 --- a/v2rayN/v2rayN/Handler/MainFormHandler.cs +++ b/v2rayN/v2rayN/Handler/MainFormHandler.cs @@ -1,5 +1,4 @@ -using NHotkey; -using NHotkey.Wpf; + using System.Drawing; using System.IO; using System.Windows.Forms; diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index c670f83dc3..17bd0e3942 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -3,7 +3,6 @@ using MaterialDesignColors; using MaterialDesignColors.ColorManipulation; using MaterialDesignThemes.Wpf; -using NHotkey; using ReactiveUI; using ReactiveUI.Fody.Helpers; using Splat; diff --git a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs index 54b4828314..81d3518eec 100644 --- a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs @@ -1,4 +1,6 @@ using Microsoft.Win32.TaskScheduler; +using System.Diagnostics; +using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -11,9 +13,8 @@ namespace v2rayN.Views { public partial class GlobalHotkeySettingWindow { - private static Config _config; - List lstKey; - private Dictionary _TextBoxKeyEventItem; + private static Config _config = default!; + private Dictionary _TextBoxKeyEventItem = default!; public GlobalHotkeySettingWindow() { @@ -21,7 +22,21 @@ public GlobalHotkeySettingWindow() this.Owner = Application.Current.MainWindow; _config = LazyConfig.Instance.GetConfig(); _config.globalHotkeys ??= new List(); + + 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() { { txtGlobalHotkey0,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.ShowForm) }, @@ -30,35 +45,20 @@ public GlobalHotkeySettingWindow() { txtGlobalHotkey3,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.SystemProxyUnchanged)}, { txtGlobalHotkey4,GetKeyEventItemByEGlobalHotkey(_config.globalHotkeys,EGlobalHotkey.SystemProxyPac)} }; - txtGlobalHotkey0.KeyDown += TxtGlobalHotkey_KeyDown; - txtGlobalHotkey1.KeyDown += TxtGlobalHotkey_KeyDown; - txtGlobalHotkey2.KeyDown += TxtGlobalHotkey_KeyDown; - txtGlobalHotkey3.KeyDown += TxtGlobalHotkey_KeyDown; - txtGlobalHotkey4.KeyDown += TxtGlobalHotkey_KeyDown; - - BindingData(-1); - - Utils.SetDarkBorder(this, _config.uiItem.colorModeDark); + BindingData(); } private void TxtGlobalHotkey_KeyDown(object sender, KeyEventArgs e) { + Debug.WriteLine($"{e.Key}{e.SystemKey}"); 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 = e.Key == Key.System ? e.SystemKey : e.Key; - - 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); - } + _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]); } private KeyEventItem GetKeyEventItemByEGlobalHotkey(List KELsit,EGlobalHotkey eg) @@ -73,44 +73,40 @@ private KeyEventItem GetKeyEventItemByEGlobalHotkey(List KELsit,EG }); } - private void BindingData(int index) + private string KeyEventItemToString(KeyEventItem item) { - for (int k = 0; k < lstKey.Count; k++) - { - if (index >= 0 && index != k) - { - continue; - } - var item = lstKey[k]; - var keys = string.Empty; + var res = new StringBuilder(); - if (item.Control) - { - keys += $"{Forms.Keys.Control} + "; - } - if (item.Alt) - { - keys += $"{Forms.Keys.Alt} + "; - } - if (item.Shift) + 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 (item.Value.KeyCode != null && item.Value.KeyCode != Key.None) { - keys += $"{Forms.Keys.Shift} + "; + (item.Key as TextBox)!.Text = KeyEventItemToString(item.Value); } - if (item.KeyCode != null) + else { - keys += $"{item.KeyCode}"; + (item.Key as TextBox)!.Text = string.Empty; } - - 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 @@ -126,37 +122,14 @@ private void btnCancel_Click(object sender, RoutedEventArgs e) private void btnReset_Click(object sender, RoutedEventArgs e) { - lstKey.Clear(); - foreach (EGlobalHotkey it in Enum.GetValues(typeof(EGlobalHotkey))) - { - 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) + foreach(var k in _TextBoxKeyEventItem.Keys) { - 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) diff --git a/v2rayN/v2rayN/v2rayN.csproj b/v2rayN/v2rayN/v2rayN.csproj index 405266e0d0..1d20dca3e0 100644 --- a/v2rayN/v2rayN/v2rayN.csproj +++ b/v2rayN/v2rayN/v2rayN.csproj @@ -18,8 +18,6 @@ - - From 5e9f4ad926e408e36fff33d9fe8f24f11abba4e7 Mon Sep 17 00:00:00 2001 From: chao wan <1013448513@qq.com> Date: Sat, 4 Mar 2023 11:46:36 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=BD=93=E5=89=8D?= =?UTF-8?q?=E5=B7=B2=E6=B3=A8=E5=86=8C=E7=83=AD=E9=94=AE=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E8=A7=A6=E5=8F=91UI=E7=BA=BF=E7=A8=8B=E6=8E=A7=E4=BB=B6?= =?UTF-8?q?=E7=9A=84KeyDown=E4=BA=8B=E4=BB=B6=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/v2rayN/Handler/HotkeyHandler.cs | 46 +++++++++++++------ .../Views/GlobalHotkeySettingWindow.xaml.cs | 13 +++--- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/v2rayN/v2rayN/Handler/HotkeyHandler.cs b/v2rayN/v2rayN/Handler/HotkeyHandler.cs index 55f6576bc0..57944dabce 100644 --- a/v2rayN/v2rayN/Handler/HotkeyHandler.cs +++ b/v2rayN/v2rayN/Handler/HotkeyHandler.cs @@ -59,26 +59,26 @@ private void Init() } public void Load() { - foreach(var hotkey in _hotkeyTriggerDic.Keys) + foreach(var _hotkeyCode in _hotkeyTriggerDic.Keys) { - var hotkeyInfo = GetHotkeyInfo(hotkey); + var hotkeyInfo = GetHotkeyInfo(_hotkeyCode); bool isSuccess = false; string msg; Application.Current.Dispatcher.Invoke(() => { - isSuccess = RegisterHotKey(IntPtr.Zero, hotkey, hotkeyInfo.fsModifiers, hotkeyInfo.vKey); + 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})"); + 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); + msg = string.Format(ResUI.RegisterGlobalHotkeyFailed, $"{name}({hotkeyInfo.hotkeyStr})", errInfo); } UpdateViewEvent?.Invoke(false, msg); } @@ -98,10 +98,10 @@ public void ReLoad() Init(); Load(); } - private (int fsModifiers, int vKey, string HotkeyStr, List Names) GetHotkeyInfo(int hotkey) + private (int fsModifiers, int vKey, string hotkeyStr, List Names) GetHotkeyInfo(int hotkeycode) { - var _fsModifiers = hotkey & 0xffff; - var _vkey = (hotkey >> 16) & 0xffff; + var _fsModifiers = hotkeycode & 0xffff; + var _vkey = (hotkeycode >> 16) & 0xffff; var _hotkeyStr = new StringBuilder(); var _names = new List(); @@ -112,9 +112,9 @@ public void ReLoad() if ((mdif | KeyModifiers.Shift) == KeyModifiers.Shift) _hotkeyStr.Append($"{KeyModifiers.Shift}+"); _hotkeyStr.Append(key.ToString()); - foreach (var name in _hotkeyTriggerDic.Values) + foreach (var name in _hotkeyTriggerDic[hotkeycode]) { - _names.Add(name.ToString()!); + _names.Add(name.ToString()); } @@ -127,10 +127,30 @@ private void OnThreadPreProcessMessage(ref MSG msg, ref bool handled) return; } handled = true; - foreach (var keyEvent in _hotkeyTriggerDic[(int)msg.lParam]) + 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 { - if (IsPause) return; - HotkeyTriggerEvent?.Invoke(keyEvent); + foreach (var keyEvent in _hotkeyTriggerDic[(int)msg.lParam]) + { + HotkeyTriggerEvent?.Invoke(keyEvent); + } } } [DllImport("user32.dll", SetLastError = true)] diff --git a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs index 81d3518eec..c0af6c083e 100644 --- a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs @@ -23,11 +23,11 @@ public GlobalHotkeySettingWindow() _config = LazyConfig.Instance.GetConfig(); _config.globalHotkeys ??= new List(); - txtGlobalHotkey0.PreviewKeyDown += TxtGlobalHotkey_KeyDown; - txtGlobalHotkey1.PreviewKeyDown += TxtGlobalHotkey_KeyDown; - txtGlobalHotkey2.PreviewKeyDown += TxtGlobalHotkey_KeyDown; - txtGlobalHotkey3.PreviewKeyDown += TxtGlobalHotkey_KeyDown; - txtGlobalHotkey4.PreviewKeyDown += TxtGlobalHotkey_KeyDown; + txtGlobalHotkey0.KeyDown += TxtGlobalHotkey_KeyDown; + txtGlobalHotkey1.KeyDown += TxtGlobalHotkey_KeyDown; + txtGlobalHotkey2.KeyDown += TxtGlobalHotkey_KeyDown; + txtGlobalHotkey3.KeyDown += TxtGlobalHotkey_KeyDown; + txtGlobalHotkey4.KeyDown += TxtGlobalHotkey_KeyDown; this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false; HotkeyHandler.Instance.IsPause = true; @@ -53,7 +53,8 @@ private void TxtGlobalHotkey_KeyDown(object sender, KeyEventArgs e) { Debug.WriteLine($"{e.Key}{e.SystemKey}"); 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, 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; From 0bf4a43663fde734c0c8ee3e7e006495f6a6e03d Mon Sep 17 00:00:00 2001 From: chao wan <1013448513@qq.com> Date: Sat, 4 Mar 2023 11:52:46 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=B0=83=E8=AF=95?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs index c0af6c083e..c067a5628b 100644 --- a/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs +++ b/v2rayN/v2rayN/Views/GlobalHotkeySettingWindow.xaml.cs @@ -7,7 +7,6 @@ using v2rayN.Handler; using v2rayN.Mode; using v2rayN.Resx; -using Forms = System.Windows.Forms; namespace v2rayN.Views { @@ -29,8 +28,8 @@ public GlobalHotkeySettingWindow() txtGlobalHotkey3.KeyDown += TxtGlobalHotkey_KeyDown; txtGlobalHotkey4.KeyDown += TxtGlobalHotkey_KeyDown; - this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false; HotkeyHandler.Instance.IsPause = true; + this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false; Utils.SetDarkBorder(this, _config.uiItem.colorModeDark); InitData(); } @@ -51,7 +50,6 @@ private void InitData() private void TxtGlobalHotkey_KeyDown(object sender, KeyEventArgs e) { - Debug.WriteLine($"{e.Key}{e.SystemKey}"); e.Handled = true; var _ModifierKeys = new Key[] { Key.LeftCtrl, Key.RightCtrl, Key.LeftShift, Key.RightShift, Key.LeftAlt, Key.RightAlt, Key.LWin, Key.RWin};