解决当前已注册热键无法触发UI线程控件的KeyDown事件的问题

This commit is contained in:
chao wan 2023-03-04 11:46:36 +08:00
parent ba702ba041
commit 5e9f4ad926
2 changed files with 40 additions and 19 deletions

View file

@ -59,26 +59,26 @@ namespace v2rayN.Handler
} }
public void Load() 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; bool isSuccess = false;
string msg; string msg;
Application.Current.Dispatcher.Invoke(() => 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) foreach (var name in hotkeyInfo.Names)
{ {
if (isSuccess) if (isSuccess)
{ {
msg = string.Format(ResUI.RegisterGlobalHotkeySuccessfully, $"{name}({hotkeyInfo.HotkeyStr})"); msg = string.Format(ResUI.RegisterGlobalHotkeySuccessfully, $"{name}({hotkeyInfo.hotkeyStr})");
} }
else else
{ {
var errInfo = new Win32Exception(Marshal.GetLastWin32Error()).Message; 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); UpdateViewEvent?.Invoke(false, msg);
} }
@ -98,10 +98,10 @@ namespace v2rayN.Handler
Init(); Init();
Load(); Load();
} }
private (int fsModifiers, int vKey, string HotkeyStr, List<string> Names) GetHotkeyInfo(int hotkey) private (int fsModifiers, int vKey, string hotkeyStr, List<string> Names) GetHotkeyInfo(int hotkeycode)
{ {
var _fsModifiers = hotkey & 0xffff; var _fsModifiers = hotkeycode & 0xffff;
var _vkey = (hotkey >> 16) & 0xffff; var _vkey = (hotkeycode >> 16) & 0xffff;
var _hotkeyStr = new StringBuilder(); var _hotkeyStr = new StringBuilder();
var _names = new List<string>(); var _names = new List<string>();
@ -112,9 +112,9 @@ namespace v2rayN.Handler
if ((mdif | KeyModifiers.Shift) == KeyModifiers.Shift) _hotkeyStr.Append($"{KeyModifiers.Shift}+"); if ((mdif | KeyModifiers.Shift) == KeyModifiers.Shift) _hotkeyStr.Append($"{KeyModifiers.Shift}+");
_hotkeyStr.Append(key.ToString()); _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 @@ namespace v2rayN.Handler
return; return;
} }
handled = true; handled = true;
foreach (var keyEvent in _hotkeyTriggerDic[(int)msg.lParam]) var _hotKeyCode = (int)msg.lParam;
if (IsPause)
{ {
if (IsPause) return; Application.Current.Dispatcher.Invoke(() =>
HotkeyTriggerEvent?.Invoke(keyEvent); {
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)] [DllImport("user32.dll", SetLastError = true)]

View file

@ -23,11 +23,11 @@ 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; txtGlobalHotkey0.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey1.PreviewKeyDown += TxtGlobalHotkey_KeyDown; txtGlobalHotkey1.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey2.PreviewKeyDown += TxtGlobalHotkey_KeyDown; txtGlobalHotkey2.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey3.PreviewKeyDown += TxtGlobalHotkey_KeyDown; txtGlobalHotkey3.KeyDown += TxtGlobalHotkey_KeyDown;
txtGlobalHotkey4.PreviewKeyDown += TxtGlobalHotkey_KeyDown; txtGlobalHotkey4.KeyDown += TxtGlobalHotkey_KeyDown;
this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false; this.Closing += (s, e) => HotkeyHandler.Instance.IsPause = false;
HotkeyHandler.Instance.IsPause = true; HotkeyHandler.Instance.IsPause = true;
@ -53,7 +53,8 @@ namespace v2rayN.Views
{ {
Debug.WriteLine($"{e.Key}{e.SystemKey}"); 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, 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].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].Alt = (Keyboard.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt;
_TextBoxKeyEventItem[sender].Control = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control; _TextBoxKeyEventItem[sender].Control = (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control;