* UIForm: 增加UnRegisterHotKey,卸载全局热键

This commit is contained in:
Sunny 2022-07-14 09:29:13 +08:00
parent 74c1a47a8b
commit 8322b14cbe

View File

@ -36,9 +36,11 @@
* 2022-05-06: V3.1.8 Padding可以调整大小
* 2022-06-11: V3.1.9
* 2022-07-05: V3.2.1 PageAddedPageSelectedPageRemoved事件
* 2022-07-14: V3.2.1 UnRegisterHotKey
******************************************************************************/
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
@ -1690,20 +1692,31 @@ namespace Sunny.UI
public void RegisterHotKey(Sunny.UI.ModifierKeys modifierKey, Keys key)
{
if (hotKeys == null) hotKeys = new Dictionary<int, HotKey>();
if (hotKeys == null) hotKeys = new ConcurrentDictionary<int, HotKey>();
int id = HotKey.CalculateID(modifierKey, key);
if (!hotKeys.ContainsKey(id))
{
HotKey newHotkey = new HotKey(modifierKey, key);
this.hotKeys.Add(id, newHotkey);
hotKeys.TryAdd(id, newHotkey);
Win32.User.RegisterHotKey(Handle, id, (int)newHotkey.ModifierKey, (int)newHotkey.Key);
}
}
public void UnRegisterHotKey(Sunny.UI.ModifierKeys modifierKey, Keys key)
{
if (hotKeys == null) return;
int id = HotKey.CalculateID(modifierKey, key);
if (hotKeys.ContainsKey(id))
{
hotKeys.TryRemove(id, out _);
Win32.User.UnregisterHotKey(Handle, id);
}
}
public event HotKeyEventHandler HotKeyEventHandler;
private Dictionary<int, HotKey> hotKeys;
private ConcurrentDictionary<int, HotKey> hotKeys;
#region