mirror of
https://github.com/jie65535/GrasscutterCommandGenerator.git
synced 2025-06-08 23:19:14 +08:00
Add Kot key utils from https://github.com/jie65535/KeyGo
This commit is contained in:
parent
b77d242bd9
commit
6855b6299f
@ -280,9 +280,12 @@
|
|||||||
<AutoGen>True</AutoGen>
|
<AutoGen>True</AutoGen>
|
||||||
<DesignTime>True</DesignTime>
|
<DesignTime>True</DesignTime>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Include="Utils\AppHotKey.cs" />
|
||||||
<Compile Include="Utils\ArtifactUtils.cs" />
|
<Compile Include="Utils\ArtifactUtils.cs" />
|
||||||
<Compile Include="Utils\Common.cs" />
|
<Compile Include="Utils\Common.cs" />
|
||||||
<Compile Include="Utils\GuiRedirect.cs" />
|
<Compile Include="Utils\GuiRedirect.cs" />
|
||||||
|
<Compile Include="Utils\HotKey.cs" />
|
||||||
|
<Compile Include="Utils\HotKeyItem.cs" />
|
||||||
<Compile Include="Utils\HttpHelper.cs" />
|
<Compile Include="Utils\HttpHelper.cs" />
|
||||||
<Compile Include="Utils\Logger.cs" />
|
<Compile Include="Utils\Logger.cs" />
|
||||||
<Compile Include="Utils\GithubHelper.cs" />
|
<Compile Include="Utils\GithubHelper.cs" />
|
||||||
|
61
Source/GrasscutterTools/Utils/AppHotKey.cs
Normal file
61
Source/GrasscutterTools/Utils/AppHotKey.cs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
using System;
|
||||||
|
using System.ComponentModel;
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
|
namespace GrasscutterTools.Utils
|
||||||
|
{
|
||||||
|
public static class AppHotKey
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 注册热键
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hwnd">窗口句柄</param>
|
||||||
|
/// <param name="hotKey_id">热键ID</param>
|
||||||
|
/// <param name="keyModifiers">组合键</param>
|
||||||
|
/// <param name="key">热键</param>
|
||||||
|
public static void RegKey(IntPtr hwnd, int hotKey_id, KeyModifiers keyModifiers, Keys key)
|
||||||
|
{
|
||||||
|
if (!RegisterHotKey(hwnd, hotKey_id, keyModifiers, key))
|
||||||
|
throw new Win32Exception();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 注销热键
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hwnd">窗口句柄</param>
|
||||||
|
/// <param name="hotKey_id">热键ID</param>
|
||||||
|
public static void UnRegKey(IntPtr hwnd, int hotKey_id)
|
||||||
|
{
|
||||||
|
//注销Id号为hotKey_id的热键设定
|
||||||
|
UnregisterHotKey(hwnd, hotKey_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
//如果函数执行成功,返回值不为0。
|
||||||
|
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
|
||||||
|
[DllImport("user32.dll", SetLastError = true)]
|
||||||
|
private static extern bool RegisterHotKey(
|
||||||
|
IntPtr hWnd, //要定义热键的窗口的句柄
|
||||||
|
int id, //定义热键ID(不能与其它ID重复)
|
||||||
|
KeyModifiers fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
|
||||||
|
Keys vk //定义热键的内容
|
||||||
|
);
|
||||||
|
|
||||||
|
[DllImport("user32.dll", SetLastError = true)]
|
||||||
|
private static extern bool UnregisterHotKey(
|
||||||
|
IntPtr hWnd, //要取消热键的窗口的句柄
|
||||||
|
int id //要取消热键的ID
|
||||||
|
);
|
||||||
|
|
||||||
|
//定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
|
||||||
|
[Flags]
|
||||||
|
public enum KeyModifiers
|
||||||
|
{
|
||||||
|
None = 0,
|
||||||
|
Alt = 1,
|
||||||
|
Ctrl = 2,
|
||||||
|
Shift = 4,
|
||||||
|
WindowsKey = 8
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
298
Source/GrasscutterTools/Utils/HotKey.cs
Normal file
298
Source/GrasscutterTools/Utils/HotKey.cs
Normal file
@ -0,0 +1,298 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
|
namespace GrasscutterTools.Utils
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// KeyGo 核心功能类
|
||||||
|
/// </summary>
|
||||||
|
public class KeyGo
|
||||||
|
{
|
||||||
|
#region Member
|
||||||
|
|
||||||
|
private static int _RegMaxID;
|
||||||
|
|
||||||
|
[XmlIgnore]
|
||||||
|
public IntPtr FormHandle { get; set; }
|
||||||
|
|
||||||
|
public List<HotKeyItem> Items { get; set; } = new List<HotKeyItem>();
|
||||||
|
|
||||||
|
#endregion Member
|
||||||
|
|
||||||
|
#region FILE IO
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Loads the XML.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filePath">The file path.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static KeyGo LoadXml(string filePath)
|
||||||
|
{
|
||||||
|
KeyGo data = null;
|
||||||
|
if (File.Exists(filePath))
|
||||||
|
{
|
||||||
|
XmlSerializer formatter = new XmlSerializer(typeof(KeyGo));
|
||||||
|
using (var stream = File.OpenRead(filePath))
|
||||||
|
{
|
||||||
|
if (stream.Length > 0)
|
||||||
|
{
|
||||||
|
data = formatter.Deserialize(stream) as KeyGo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Saves the XML.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="filePath">The file path.</param>
|
||||||
|
public void SaveXml(string filePath)
|
||||||
|
{
|
||||||
|
if (!File.Exists(filePath))
|
||||||
|
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
|
||||||
|
|
||||||
|
XmlSerializer formatter = new XmlSerializer(typeof(KeyGo));
|
||||||
|
using (var stream = File.Create(filePath))
|
||||||
|
{
|
||||||
|
formatter.Serialize(stream, this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion FILE IO
|
||||||
|
|
||||||
|
#region HotKey Register
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Regs all key.
|
||||||
|
/// </summary>
|
||||||
|
public void RegAllKey()
|
||||||
|
{
|
||||||
|
foreach (var item in Items)
|
||||||
|
{
|
||||||
|
if (!item.Enabled)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
RegKey(item);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// 忽视异常,外部通过ID是否被设置判断执行结果
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Uns the reg all key.
|
||||||
|
/// </summary>
|
||||||
|
public void UnRegAllKey()
|
||||||
|
{
|
||||||
|
foreach (var item in Items)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
UnRegKey(item);
|
||||||
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
// 忽视异常,外部通过ID是否被设置判断执行结果
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 注册热键 - 成功后,会设置 HotKeyID
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">The item.</param>
|
||||||
|
/// <exception cref="ArgumentNullException">
|
||||||
|
/// item
|
||||||
|
/// or
|
||||||
|
/// HotKey - 热键不能为空!
|
||||||
|
/// </exception>
|
||||||
|
/// <exception cref="InvalidOperationException">
|
||||||
|
/// 功能键不能为空!
|
||||||
|
/// or
|
||||||
|
/// 快捷键不能为空!
|
||||||
|
/// </exception>
|
||||||
|
public void RegKey(HotKeyItem item)
|
||||||
|
{
|
||||||
|
if (item is null)
|
||||||
|
throw new ArgumentNullException(nameof(item));
|
||||||
|
if (string.IsNullOrWhiteSpace(item.HotKey))
|
||||||
|
throw new ArgumentNullException(nameof(item.HotKey), "热键不能为空!");
|
||||||
|
|
||||||
|
// 如果注册过该热键,ID不为0。卸载热键会将ID置零。
|
||||||
|
if (item.HotKeyID != 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
int id = Interlocked.Increment(ref _RegMaxID);
|
||||||
|
|
||||||
|
var keys = item.HotKey.Split('+');
|
||||||
|
Keys keyCode = Keys.None;
|
||||||
|
AppHotKey.KeyModifiers keyModifiers = AppHotKey.KeyModifiers.None;
|
||||||
|
foreach (var key in keys)
|
||||||
|
{
|
||||||
|
switch (key.ToLower())
|
||||||
|
{
|
||||||
|
case "ctrl":
|
||||||
|
keyModifiers |= AppHotKey.KeyModifiers.Ctrl;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "shift":
|
||||||
|
keyModifiers |= AppHotKey.KeyModifiers.Shift;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "alt":
|
||||||
|
keyModifiers |= AppHotKey.KeyModifiers.Alt;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case "win":
|
||||||
|
keyModifiers |= AppHotKey.KeyModifiers.WindowsKey;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
keyCode = (Keys)Enum.Parse(typeof(Keys), key);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (keyModifiers == AppHotKey.KeyModifiers.None)
|
||||||
|
throw new InvalidOperationException("功能键不能为空!");
|
||||||
|
if (keyCode == Keys.None)
|
||||||
|
throw new InvalidOperationException("快捷键不能为空!");
|
||||||
|
|
||||||
|
AppHotKey.RegKey(FormHandle, id, keyModifiers, keyCode);
|
||||||
|
item.HotKeyID = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 注销热键 - 完成后,会清零 HotKeyID
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">The item.</param>
|
||||||
|
/// <exception cref="ArgumentNullException">item</exception>
|
||||||
|
public void UnRegKey(HotKeyItem item)
|
||||||
|
{
|
||||||
|
if (item is null)
|
||||||
|
throw new ArgumentNullException(nameof(item));
|
||||||
|
if (item.HotKeyID == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
|
AppHotKey.UnRegKey(FormHandle, item.HotKeyID);
|
||||||
|
item.HotKeyID = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion HotKey Register
|
||||||
|
|
||||||
|
#region HotKey Trigger
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 热键触发时调用
|
||||||
|
/// </summary>
|
||||||
|
public event EventHandler<HotKeyTriggerEventArgs> HotKeyTriggerEvent;
|
||||||
|
|
||||||
|
private bool OnHotKeyTrigger(HotKeyItem item)
|
||||||
|
{
|
||||||
|
var args = new HotKeyTriggerEventArgs { HotKeyItem = item };
|
||||||
|
HotKeyTriggerEvent?.Invoke(this, args);
|
||||||
|
return args.Handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Processes the hotkey.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hotKey_id">The hot key identifier.</param>
|
||||||
|
public void ProcessHotkey(int hotKey_id)
|
||||||
|
{
|
||||||
|
var hotkey = Items.Find(k => k.HotKeyID == hotKey_id);
|
||||||
|
if (hotkey != null)
|
||||||
|
{
|
||||||
|
//Console.WriteLine($"ID:{hotkey.HotKeyID} Keys:{hotkey.HotKey} ProcessName:{hotkey.ProcessName}\nStartupPath:{hotkey.StartupPath}");
|
||||||
|
++hotkey.TriggerCounter;
|
||||||
|
// 触发事件,若被外部处理,则内部不再执行
|
||||||
|
OnHotKeyTrigger(hotkey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion HotKey Trigger
|
||||||
|
|
||||||
|
#region HotKey Manager
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 添加一个新热键
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">The item.</param>
|
||||||
|
/// <exception cref="ArgumentNullException">
|
||||||
|
/// item
|
||||||
|
/// or
|
||||||
|
/// HotKey - 热键不能为空!
|
||||||
|
/// </exception>
|
||||||
|
/// <exception cref="InvalidOperationException">
|
||||||
|
/// 功能键不能为空!
|
||||||
|
/// or
|
||||||
|
/// 快捷键不能为空!
|
||||||
|
/// </exception>
|
||||||
|
public void AddHotKey(HotKeyItem item)
|
||||||
|
{
|
||||||
|
if (item is null)
|
||||||
|
throw new ArgumentNullException(nameof(item));
|
||||||
|
|
||||||
|
if (item.Enabled)
|
||||||
|
RegKey(item);
|
||||||
|
Items.Add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除一个热键
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">The item.</param>
|
||||||
|
public void DelHotKey(HotKeyItem item)
|
||||||
|
{
|
||||||
|
if (item is null)
|
||||||
|
throw new ArgumentNullException(nameof(item));
|
||||||
|
|
||||||
|
if (item.HotKeyID != 0)
|
||||||
|
UnRegKey(item);
|
||||||
|
Items.Remove(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 修改热键
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item">The item.</param>
|
||||||
|
public void ChangeHotKey(HotKeyItem item)
|
||||||
|
{
|
||||||
|
if (item is null)
|
||||||
|
throw new ArgumentNullException(nameof(item));
|
||||||
|
|
||||||
|
// 重新注册
|
||||||
|
if (item.HotKeyID != 0)
|
||||||
|
UnRegKey(item);
|
||||||
|
if (item.Enabled)
|
||||||
|
RegKey(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion HotKey Manager
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 热键触发事件参数
|
||||||
|
/// </summary>
|
||||||
|
public class HotKeyTriggerEventArgs
|
||||||
|
{
|
||||||
|
public HotKeyItem HotKeyItem { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取或设置该事件是否已经被处理
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// <c>true</c> if handle; otherwise, <c>false</c>.
|
||||||
|
/// </value>
|
||||||
|
public bool Handle { get; set; }
|
||||||
|
}
|
||||||
|
}
|
77
Source/GrasscutterTools/Utils/HotKeyItem.cs
Normal file
77
Source/GrasscutterTools/Utils/HotKeyItem.cs
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
using System;
|
||||||
|
using System.Xml.Serialization;
|
||||||
|
|
||||||
|
namespace GrasscutterTools.Utils
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 热键项
|
||||||
|
/// </summary>
|
||||||
|
public class HotKeyItem
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the hot key identifier.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The hot key identifier.
|
||||||
|
/// </value>
|
||||||
|
[XmlIgnore]
|
||||||
|
public int HotKeyID { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the name of the Tag.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The name of the Tag.
|
||||||
|
/// </value>
|
||||||
|
public string Tag { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the Commands.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The Commands.
|
||||||
|
/// </value>
|
||||||
|
public string Commands { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the hot key.
|
||||||
|
/// 组合键之间使用+隔开,例如:"Ctrl+Shift+W"
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The hot key.
|
||||||
|
/// </value>
|
||||||
|
public string HotKey { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether this <see cref="HotKeyItem"/> is enabled.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// <c>true</c> if enabled; otherwise, <c>false</c>.
|
||||||
|
/// </value>
|
||||||
|
public bool Enabled { get; set; } = true;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the trigger counter.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The trigger counter.
|
||||||
|
/// </value>
|
||||||
|
public int TriggerCounter { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the creation time.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The creation time.
|
||||||
|
/// </value>
|
||||||
|
public DateTime CreationTime { get; set; } = DateTime.Now;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the last modified time.
|
||||||
|
/// </summary>
|
||||||
|
/// <value>
|
||||||
|
/// The last modified time.
|
||||||
|
/// </value>
|
||||||
|
public DateTime LastModifiedTime { get; set; } = DateTime.Now;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user