diff --git a/Source/GrasscutterTools/Forms/FormMain.cs b/Source/GrasscutterTools/Forms/FormMain.cs index 69c36db..512b34d 100644 --- a/Source/GrasscutterTools/Forms/FormMain.cs +++ b/Source/GrasscutterTools/Forms/FormMain.cs @@ -44,6 +44,9 @@ namespace GrasscutterTools.Forms if (DesignMode) return; + Common.KeyGo = new KeyGo(Handle); + Common.KeyGo.HotKeyTriggerEvent += OnHotKeyTrigger; + try { var location = Settings.Default.MainFormLocation; @@ -94,7 +97,21 @@ namespace GrasscutterTools.Forms ph.OnLanguageChanged = () => FormMain_Load(this, EventArgs.Empty); var poc = CreatePage(); poc.ShowTipInRunButton = msg => ShowTip(msg, BtnInvokeOpenCommand); - CreatePage(); + var pcc = CreatePage(); + var phk = CreatePage(); + pcc.OnAddHotKey = tag => + { + phk.AddNewHotKey(tag); + // 跳转到快捷键界面 + for (var i = 0; i < TCMain.Controls.Count; i++) + { + if (TCMain.Controls[i].Controls[0] == phk) + { + ListPages.SelectedIndex = i; + break; + } + } + }; CreatePage(); CreatePage(); CreatePage(); @@ -126,6 +143,7 @@ namespace GrasscutterTools.Forms Resources.PageHomeTitle, Resources.PageOpenCommandTitle, Resources.PageCustomCommandsTitle, + Resources.PageHotKey, Resources.PageGetArtifactTitle, Resources.PageSpawnTitle, Resources.PageGiveItemTitle, @@ -274,6 +292,33 @@ namespace GrasscutterTools.Forms #endregion - 初始化 Init - + + #region - 快捷键执行 HotKey - + + /// + /// 快捷键触发时执行 + /// + private void OnHotKeyTrigger(object sender, HotKeyTriggerEventArgs e) + { + BeginInvoke(new Func(() => RunRawCommands(e.HotKeyItem.Commands))); + e.Handle = true; + } + + private const int WM_HOTKEY = 0x312; + + protected override void WndProc(ref Message m) + { + base.WndProc(ref m); + switch (m.Msg) + { + case WM_HOTKEY: + Common.KeyGo.ProcessHotKey(m.WParam.ToInt32()); + break; + } + } + + #endregion - 快捷键执行 HotKey - + #region - 命令 Command - /// @@ -375,10 +420,20 @@ namespace GrasscutterTools.Forms ShowTip(Resources.CommandContentCannotBeEmpty, CmbCommand); return; } - if (cmd.IndexOf('|') == -1) - await RunCommands(FormatCommand(cmd)); - else - await RunCommands(cmd.Split('|').Select(FormatCommand).ToArray()); + + await RunRawCommands(cmd); + } + + /// + /// 运行原始命令 + /// + /// 命令字符串 + /// 是否执行成功 + private async Task RunRawCommands(string commands) + { + if (commands.IndexOf('|') == -1) + return await RunCommands(FormatCommand(commands)); + return await RunCommands(commands.Split('|').Select(FormatCommand).ToArray()); } /// diff --git a/Source/GrasscutterTools/GrasscutterTools.csproj b/Source/GrasscutterTools/GrasscutterTools.csproj index cdffa18..344b1f4 100644 --- a/Source/GrasscutterTools/GrasscutterTools.csproj +++ b/Source/GrasscutterTools/GrasscutterTools.csproj @@ -219,6 +219,12 @@ PageHome.cs + + UserControl + + + PageHotKey.cs + UserControl @@ -284,7 +290,7 @@ - + @@ -490,6 +496,18 @@ PageHome.cs + + PageHotKey.cs + + + PageHotKey.cs + + + PageHotKey.cs + + + PageHotKey.cs + PageMail.cs diff --git a/Source/GrasscutterTools/Pages/PageCustomCommands.Designer.cs b/Source/GrasscutterTools/Pages/PageCustomCommands.Designer.cs index 4f701d9..da02cc8 100644 --- a/Source/GrasscutterTools/Pages/PageCustomCommands.Designer.cs +++ b/Source/GrasscutterTools/Pages/PageCustomCommands.Designer.cs @@ -38,6 +38,7 @@ this.BtnRemoveCustomCommand = new System.Windows.Forms.Button(); this.BtnSaveCustomCommand = new System.Windows.Forms.Button(); this.TxtCustomName = new System.Windows.Forms.TextBox(); + this.BtnAddHotKey = new System.Windows.Forms.Button(); this.GrpCustomCommands.SuspendLayout(); this.SuspendLayout(); // @@ -100,10 +101,18 @@ this.TxtCustomName.Name = "TxtCustomName"; this.TxtCustomName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TxtCustomName_KeyDown); // + // BtnAddHotKey + // + resources.ApplyResources(this.BtnAddHotKey, "BtnAddHotKey"); + this.BtnAddHotKey.Name = "BtnAddHotKey"; + this.BtnAddHotKey.UseVisualStyleBackColor = true; + this.BtnAddHotKey.Click += new System.EventHandler(this.BtnAddHotKey_Click); + // // PageCustomCommands // resources.ApplyResources(this, "$this"); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.BtnAddHotKey); this.Controls.Add(this.BtnExportCustomCommands); this.Controls.Add(this.BtnLoadCustomCommands); this.Controls.Add(this.LblCustomName); @@ -130,5 +139,6 @@ private System.Windows.Forms.Button BtnRemoveCustomCommand; private System.Windows.Forms.Button BtnSaveCustomCommand; private System.Windows.Forms.TextBox TxtCustomName; + private System.Windows.Forms.Button BtnAddHotKey; } } diff --git a/Source/GrasscutterTools/Pages/PageCustomCommands.cs b/Source/GrasscutterTools/Pages/PageCustomCommands.cs index ae69988..eb89211 100644 --- a/Source/GrasscutterTools/Pages/PageCustomCommands.cs +++ b/Source/GrasscutterTools/Pages/PageCustomCommands.cs @@ -248,5 +248,18 @@ namespace GrasscutterTools.Pages LoadCustomCommandControls(Resources.CustomCommands); } } + + public Action OnAddHotKey; + + private void BtnAddHotKey_Click(object sender, EventArgs e) + { + var name = TxtCustomName.Text.Trim(); + if (string.IsNullOrEmpty(name)) + { + MessageBox.Show(Resources.CommandTagCannotBeEmpty, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + OnAddHotKey?.Invoke(name); + } } } \ No newline at end of file diff --git a/Source/GrasscutterTools/Pages/PageCustomCommands.en-US.resx b/Source/GrasscutterTools/Pages/PageCustomCommands.en-US.resx index 3b8d54a..f250d00 100644 --- a/Source/GrasscutterTools/Pages/PageCustomCommands.en-US.resx +++ b/Source/GrasscutterTools/Pages/PageCustomCommands.en-US.resx @@ -119,19 +119,19 @@ - 573, 209 + 573, 206 - 70, 23 + 70, 30 Export - 497, 209 + 497, 206 - 70, 23 + 70, 30 Load @@ -155,24 +155,30 @@ Restore - 421, 209 + 421, 206 - 70, 23 + 70, 30 × Delete - 345, 209 + 345, 206 - 70, 23 + 70, 30 √ Save - 298, 23 + 217, 23 + + + 264, 206 + + + HotKey \ No newline at end of file diff --git a/Source/GrasscutterTools/Pages/PageCustomCommands.resx b/Source/GrasscutterTools/Pages/PageCustomCommands.resx index 5aba670..eac8d1c 100644 --- a/Source/GrasscutterTools/Pages/PageCustomCommands.resx +++ b/Source/GrasscutterTools/Pages/PageCustomCommands.resx @@ -117,283 +117,310 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - Bottom, Right - - - NoControl - - - - 590, 209 - - - 50, 23 - - - - 14 - - - 导出 - - - BtnExportCustomCommands - - - System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - $this - - 0 - - - Bottom, Right - - - NoControl - - - 534, 209 - - - 50, 23 - - - 13 - - - 载入 - - - BtnLoadCustomCommands - - - System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - $this - - - 1 - - - Bottom, Left - - - True - - - NoControl - - - 3, 212 - - - 32, 17 - - - 9 - - - 标签 - - - LblCustomName - - - System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - $this - - - 2 - - - Top, Bottom, Left, Right - - - Top, Right - - - True - - - NoControl - - - 602, -1 - - - 32, 17 - - - 8 + + 快捷键 重置 - - LnkResetCustomCommands - - - System.Windows.Forms.LinkLabel, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - GrpCustomCommands - - - 0 - - - True - - - Fill - - - 3, 19 - - - 634, 174 - - - 0 - - - FLPCustomCommands - - - System.Windows.Forms.FlowLayoutPanel, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - GrpCustomCommands - - - 1 - - - 3, 7 - - - 640, 196 - - - 8 - - - 列表 - - - GrpCustomCommands - - - System.Windows.Forms.GroupBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - $this - - - 3 - - - Bottom, Right - - - NoControl - - - 468, 209 - - - 60, 23 - - - 12 - - - × 删除 - - - BtnRemoveCustomCommand - - - System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - $this - - 4 - - - Bottom, Right - - - NoControl - - - 402, 209 - - - 60, 23 - - - 11 - - - √ 保存 - - - BtnSaveCustomCommand - - - System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - $this - - 5 - - Bottom, Left, Right - - - 41, 209 - - - 355, 23 - - - 10 - - - TxtCustomName + + GrasscutterTools.Pages.BasePage, GrasscutterTools, Version=1.11.0.0, Culture=neutral, PublicKeyToken=de2b1c089621e923 System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 405, 206 + + + 471, 206 + + + + Bottom, Right + + + 32, 17 + + + BtnExportCustomCommands + + + 537, 206 + + + NoControl + + + 3, 3 + + + 60, 30 + + + 41, 210 + + + 1 + + + Top, Right + + + System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + NoControl + + + BtnLoadCustomCommands + + + NoControl + + + Fill + + + Bottom, Left + + + 0 + + + + 15 + + + System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + $this + + + Bottom, Left, Right + + + 导出 + + + LnkResetCustomCommands + + + True + + + 8 + + + System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 32, 17 + + + 50, 30 + + + $this + + + 13 + + + 标签 + $this - + + 60, 30 + + + GrpCustomCommands + + + 0 + + + BtnRemoveCustomCommand + + + Bottom, Right + + + 4 + + + 50, 30 + + + FLPCustomCommands + + + 14 + + + 593, 206 + + + 2 + + + $this + + + 0 + + 6 - - True - + + NoControl + + + System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 3, 19 + + + 277, 23 + + + 602, -1 + + + Top, Bottom, Left, Right + + + √ 保存 + + + GrpCustomCommands + + + System.Windows.Forms.FlowLayoutPanel, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + 7, 17 + + 75, 30 + + + 3 + + + 7 + + + $this + + + $this + + + 324, 206 + + + GrpCustomCommands + + + System.Windows.Forms.LinkLabel, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + True + + + Bottom, Right + + + 634, 178 + + + 640, 200 + PageCustomCommands - - GrasscutterTools.Pages.BasePage, GrasscutterTools, Version=1.7.4.0, Culture=neutral, PublicKeyToken=de2b1c089621e923 + + 列表 + + × 删除 + + + System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 1 + + + TxtCustomName + + + 10 + + + True + + + System.Windows.Forms.GroupBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 11 + + + Bottom, Right + + + Bottom, Right + + + System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 3, 213 + + + $this + + + BtnSaveCustomCommand + + + 12 + + + 载入 + + + 8 + + + NoControl + + + 9 + + + NoControl + + + LblCustomName + + + BtnAddHotKey + + + True + \ No newline at end of file diff --git a/Source/GrasscutterTools/Pages/PageCustomCommands.ru-RU.resx b/Source/GrasscutterTools/Pages/PageCustomCommands.ru-RU.resx index a9f6ba0..48a8256 100644 --- a/Source/GrasscutterTools/Pages/PageCustomCommands.ru-RU.resx +++ b/Source/GrasscutterTools/Pages/PageCustomCommands.ru-RU.resx @@ -119,19 +119,19 @@ - 545, 209 + 548, 206 - 95, 23 + 95, 30 Экспорт - 444, 209 + 447, 206 - 95, 23 + 95, 30 Загрузить @@ -155,24 +155,33 @@ Сбросить - 343, 209 + 346, 206 - 95, 23 + 95, 30 x Удалить - 242, 209 + 245, 206 - 95, 23 + 95, 30 √ Сохранить - 195, 23 + 120, 23 + + + 167, 206 + + + 72, 30 + + + Горячая. \ No newline at end of file diff --git a/Source/GrasscutterTools/Pages/PageCustomCommands.zh-TW.resx b/Source/GrasscutterTools/Pages/PageCustomCommands.zh-TW.resx index 8721981..168f616 100644 --- a/Source/GrasscutterTools/Pages/PageCustomCommands.zh-TW.resx +++ b/Source/GrasscutterTools/Pages/PageCustomCommands.zh-TW.resx @@ -129,4 +129,7 @@ × 刪除 + + 快捷鍵 + \ No newline at end of file diff --git a/Source/GrasscutterTools/Pages/PageHotKey.Designer.cs b/Source/GrasscutterTools/Pages/PageHotKey.Designer.cs new file mode 100644 index 0000000..065762a --- /dev/null +++ b/Source/GrasscutterTools/Pages/PageHotKey.Designer.cs @@ -0,0 +1,149 @@ +namespace GrasscutterTools.Pages +{ + partial class PageHotKey + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region 组件设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PageHotKey)); + this.LvHotKeyList = new System.Windows.Forms.ListView(); + this.ColTag = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.ColHotKey = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.ColCommand = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); + this.GrpHotKeyList = new System.Windows.Forms.GroupBox(); + this.BtnRemove = new System.Windows.Forms.Button(); + this.BtnAddOrUpdate = new System.Windows.Forms.Button(); + this.TxtHotKey = new System.Windows.Forms.TextBox(); + this.LblHotKeyLabel = new System.Windows.Forms.Label(); + this.TxtTag = new System.Windows.Forms.TextBox(); + this.LblTagLabel = new System.Windows.Forms.Label(); + this.GrpHotKeyList.SuspendLayout(); + this.SuspendLayout(); + // + // LvHotKeyList + // + resources.ApplyResources(this.LvHotKeyList, "LvHotKeyList"); + this.LvHotKeyList.CheckBoxes = true; + this.LvHotKeyList.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { + this.ColTag, + this.ColHotKey, + this.ColCommand}); + this.LvHotKeyList.HideSelection = false; + this.LvHotKeyList.Name = "LvHotKeyList"; + this.LvHotKeyList.UseCompatibleStateImageBehavior = false; + this.LvHotKeyList.View = System.Windows.Forms.View.Details; + this.LvHotKeyList.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.LvHotKeyList_ItemCheck); + this.LvHotKeyList.SelectedIndexChanged += new System.EventHandler(this.LvHotKeyList_SelectedIndexChanged); + // + // ColTag + // + resources.ApplyResources(this.ColTag, "ColTag"); + // + // ColHotKey + // + resources.ApplyResources(this.ColHotKey, "ColHotKey"); + // + // ColCommand + // + resources.ApplyResources(this.ColCommand, "ColCommand"); + // + // GrpHotKeyList + // + resources.ApplyResources(this.GrpHotKeyList, "GrpHotKeyList"); + this.GrpHotKeyList.Controls.Add(this.LvHotKeyList); + this.GrpHotKeyList.Name = "GrpHotKeyList"; + this.GrpHotKeyList.TabStop = false; + // + // BtnRemove + // + resources.ApplyResources(this.BtnRemove, "BtnRemove"); + this.BtnRemove.Name = "BtnRemove"; + this.BtnRemove.UseVisualStyleBackColor = true; + this.BtnRemove.Click += new System.EventHandler(this.BtnRemove_Click); + // + // BtnAddOrUpdate + // + resources.ApplyResources(this.BtnAddOrUpdate, "BtnAddOrUpdate"); + this.BtnAddOrUpdate.Name = "BtnAddOrUpdate"; + this.BtnAddOrUpdate.UseVisualStyleBackColor = true; + this.BtnAddOrUpdate.Click += new System.EventHandler(this.BtnAddOrUpdate_Click); + // + // TxtHotKey + // + resources.ApplyResources(this.TxtHotKey, "TxtHotKey"); + this.TxtHotKey.BackColor = System.Drawing.Color.White; + this.TxtHotKey.Name = "TxtHotKey"; + this.TxtHotKey.ReadOnly = true; + this.TxtHotKey.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TxtHotKey_KeyDown); + // + // LblHotKeyLabel + // + resources.ApplyResources(this.LblHotKeyLabel, "LblHotKeyLabel"); + this.LblHotKeyLabel.Name = "LblHotKeyLabel"; + // + // TxtTag + // + resources.ApplyResources(this.TxtTag, "TxtTag"); + this.TxtTag.Name = "TxtTag"; + // + // LblTagLabel + // + resources.ApplyResources(this.LblTagLabel, "LblTagLabel"); + this.LblTagLabel.Name = "LblTagLabel"; + // + // PageHotKey + // + resources.ApplyResources(this, "$this"); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.BtnRemove); + this.Controls.Add(this.BtnAddOrUpdate); + this.Controls.Add(this.GrpHotKeyList); + this.Controls.Add(this.TxtHotKey); + this.Controls.Add(this.TxtTag); + this.Controls.Add(this.LblHotKeyLabel); + this.Controls.Add(this.LblTagLabel); + this.Name = "PageHotKey"; + this.GrpHotKeyList.ResumeLayout(false); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.ListView LvHotKeyList; + private System.Windows.Forms.ColumnHeader ColHotKey; + private System.Windows.Forms.ColumnHeader ColTag; + private System.Windows.Forms.ColumnHeader ColCommand; + private System.Windows.Forms.GroupBox GrpHotKeyList; + private System.Windows.Forms.Button BtnRemove; + private System.Windows.Forms.Button BtnAddOrUpdate; + private System.Windows.Forms.TextBox TxtHotKey; + private System.Windows.Forms.Label LblHotKeyLabel; + private System.Windows.Forms.TextBox TxtTag; + private System.Windows.Forms.Label LblTagLabel; + } +} diff --git a/Source/GrasscutterTools/Pages/PageHotKey.cs b/Source/GrasscutterTools/Pages/PageHotKey.cs new file mode 100644 index 0000000..b4038c1 --- /dev/null +++ b/Source/GrasscutterTools/Pages/PageHotKey.cs @@ -0,0 +1,242 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Windows.Forms; + +using GrasscutterTools.Properties; +using GrasscutterTools.Utils; +using Newtonsoft.Json; + +namespace GrasscutterTools.Pages +{ + internal partial class PageHotKey : BasePage + { + private const string TAG = nameof(PageHotKey); + + public PageHotKey() + { + InitializeComponent(); + LvHotKeyList.FullRowSelect = true; + if (DesignMode) return; + + InitHotKeys(); + } + + /// + /// 热键保存位置 + /// + private readonly string HotKeysFilePath = Common.GetAppDataFile("HotKeys.json"); + + /// + /// 热键配置是否存在更改 + /// + private bool HotKeysChanged; + + /// + /// 初始化快捷键 + /// + private void InitHotKeys() + { + if (!File.Exists(HotKeysFilePath)) + return; + try + { + Logger.I(TAG, "Loading HotKey json file from: " + HotKeysFilePath); + Common.KeyGo.Items = JsonConvert.DeserializeObject>(File.ReadAllText(HotKeysFilePath)); + LvHotKeyList.Items.AddRange(Common.KeyGo.Items.Select(HotKeyItemToViewItem).ToArray()); + Logger.I(TAG, "Start Register All HotKeys"); + Common.KeyGo.RegAllKey(); + } + catch (Exception ex) + { + Logger.W(TAG, "Parsing HotKeys.json failed.", ex); + } + } + + /// + /// 关闭时触发,取消注册并保存更改 + /// + public override void OnClosed() + { + Logger.I(TAG, "Cancel all HotKeys"); + Common.KeyGo.UnRegAllKey(); + + if (!HotKeysChanged) return; + Logger.I(TAG, "Save all HotKeys to: " + HotKeysFilePath); + File.WriteAllText(HotKeysFilePath, JsonConvert.SerializeObject(Common.KeyGo.Items)); + } + + /// + /// 将实体转为视图对象 + /// + private static ListViewItem HotKeyItemToViewItem(HotKeyItem item) => new ListViewItem(new[] + { + item.Tag, + item.HotKey, + item.Commands + }) { Checked = item.IsEnabled }; + + /// + /// 列表选中项改变时触发 + /// + private void LvHotKeyList_SelectedIndexChanged(object sender, EventArgs e) + { + if (LvHotKeyList.SelectedIndices.Count == 0) return; + var i = LvHotKeyList.SelectedIndices[0]; + var hotKeyItem = Common.KeyGo.Items[i]; + TxtTag.Text = hotKeyItem.Tag; + TxtHotKey.Text = hotKeyItem.HotKey; + SetCommand(hotKeyItem.Commands); + } + + /// + /// 点击添加或更新按钮时触发 + /// + private void BtnAddOrUpdate_Click(object sender, EventArgs e) + { + var tag = TxtTag.Text.Trim(); + var commands = GetCommand(); + var hotKey = TxtHotKey.Text; + if (string.IsNullOrEmpty(tag) || string.IsNullOrEmpty(commands) || string.IsNullOrEmpty(hotKey)) + { + MessageBox.Show(Resources.EmptyInputTip, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + + try + { + var i = Common.KeyGo.Items.FindIndex(it => it.Tag == tag); + if (i == -1) + { + var item = new HotKeyItem + { + Tag = tag, + Commands = commands, + HotKey = hotKey + }; + Logger.I(TAG, $"New HotKey item [{hotKey}]"); + Common.KeyGo.AddHotKey(item); + LvHotKeyList.Items.Add(HotKeyItemToViewItem(item)); + } + else + { + var item = Common.KeyGo.Items[i]; + item.Commands = commands; + if (item.HotKey != hotKey) + { + Logger.I(TAG, $"Update HotKey from [{item.HotKey}] to [{hotKey}]"); + item.HotKey = hotKey; + Common.KeyGo.ChangeHotKey(item); + } + + LvHotKeyList.Items[i] = HotKeyItemToViewItem(item); + } + + HotKeysChanged = true; + } + catch (Exception ex) + { + Logger.E(TAG, "AddOrUpdate HotKey failed", ex); + MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + /// + /// 点击移除按钮时触发 + /// + private void BtnRemove_Click(object sender, EventArgs e) + { + try + { + var tag = TxtTag.Text.Trim(); + var i = Common.KeyGo.Items.FindIndex(it => it.Tag == tag); + if (i == -1) return; + var item = Common.KeyGo.Items[i]; + Logger.I(TAG, $"Remove HotKey [{item.HotKey}] \"{item.Tag}\""); + Common.KeyGo.DelHotKey(item); + LvHotKeyList.Items.RemoveAt(i); + HotKeysChanged = true; + } + catch (Exception ex) + { + Logger.E(TAG, "Remove HotKey failed", ex); + MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + /// + /// 快捷键选项卡按键按下时触发 + /// + private void TxtHotKey_KeyDown(object sender, KeyEventArgs e) + { + // ESC键清空当前快捷键 + if (e.KeyCode == Keys.Escape) + { + TxtHotKey.Text = ""; + return; + } + + // 必须带功能键 + if (e.Modifiers == Keys.None) + return; + + // 必须是组合键 + if (e.KeyCode == Keys.ControlKey || e.KeyCode == Keys.ShiftKey || e.KeyCode == Keys.Menu) + return; + + var text = e.KeyCode.ToString(); + if (e.Control) + text = "Ctrl + " + text; + if (e.Shift) + text = "Shift + " + text; + if (e.Alt) + text = "Alt + " + text; + + TxtHotKey.Text = text; + } + + /// + /// 列表中的复选框改变时触发 + /// + private void LvHotKeyList_ItemCheck(object sender, ItemCheckEventArgs e) + { + var isEnable = e.NewValue == CheckState.Checked; + try + { + var item = Common.KeyGo.Items[e.Index]; + if (isEnable) + { + // 尝试注册快捷键 + Logger.I(TAG, $"Register hotKey [{item.HotKey}] as \"{item.Tag}\""); + Common.KeyGo.RegKey(item); + } + else + { + // 尝试注销快捷键 + Logger.I(TAG, $"Cancel hotKey [{item.HotKey}]"); + Common.KeyGo.UnRegKey(item); + } + // 更新使能状态 + item.IsEnabled = isEnable; + } + catch (Exception ex) + { + // 如果操作失败,还原选项,禁止设置 + e.NewValue = e.CurrentValue; + Logger.E(TAG, (isEnable ? "Enable" : "Disable") +" HotKey failed", ex); + MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + /// + /// 添加热键快捷设置方法 + /// + /// 标签名 + public void AddNewHotKey(string tag) + { + TxtHotKey.Tag = ""; + TxtTag.Text = tag; + } + } +} diff --git a/Source/GrasscutterTools/Pages/PageHotKey.en-US.resx b/Source/GrasscutterTools/Pages/PageHotKey.en-US.resx new file mode 100644 index 0000000..3568632 --- /dev/null +++ b/Source/GrasscutterTools/Pages/PageHotKey.en-US.resx @@ -0,0 +1,151 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Tag + + + HotKey + + + Commands + + + HotKeys + + + - Remove + + + √ AddOrUpdate + + + + 50, 17 + + + HotKey + + + 30, 17 + + + Tag + + \ No newline at end of file diff --git a/Source/GrasscutterTools/Pages/PageHotKey.resx b/Source/GrasscutterTools/Pages/PageHotKey.resx new file mode 100644 index 0000000..e5bcc15 --- /dev/null +++ b/Source/GrasscutterTools/Pages/PageHotKey.resx @@ -0,0 +1,384 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 0 + + + + 100, 30 + + + + 3 + + + GrasscutterTools.Pages.BasePage, GrasscutterTools, Version=1.11.0.0, Culture=neutral, PublicKeyToken=de2b1c089621e923 + + + 212, 213 + + + BtnAddOrUpdate + + + 543, 206 + + + 1 + + + $this + + + System.Windows.Forms.ListView, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 387, 206 + + + 165, 23 + + + $this + + + 44, 17 + + + 快捷执行列表 + + + $this + + + 100 + + + 4 + + + 150, 30 + + + TxtHotKey + + + 5 + + + System.Windows.Forms.ColumnHeader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + Bottom, Left, Right + + + System.Windows.Forms.ColumnHeader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Windows.Forms.ColumnHeader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 3 + + + True + + + 0 + + + Bottom, Left + + + $this + + + TxtTag + + + 2 + + + 634, 178 + + + GrpHotKeyList + + + 6 + + + LvHotKeyList + + + ColTag + + + 262, 210 + + + 命令 + + + 119, 23 + + + System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + PageHotKey + + + 41, 210 + + + 3, 213 + + + 5 + + + Fill + + + System.Windows.Forms.GroupBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + GrpHotKeyList + + + 快捷键 + + + System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 3, 19 + + + BtnRemove + + + System.Windows.Forms.TextBox, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 标签 + + + 640, 200 + + + √ 添加或更新 + + + 7, 17 + + + 150 + + + 快捷键 + + + LblTagLabel + + + True + + + 2 + + + 4 + + + System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Windows.Forms.Label, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Bottom, Left, Right + + + 1 + + + Top, Bottom, Left, Right + + + LblHotKeyLabel + + + 标签 + + + ColCommand + + + 0 + + + 32, 17 + + + $this + + + - 删除 + + + Bottom, Right + + + Bottom, Right + + + 350 + + + $this + + + 0 + + + ColHotKey + + + 1 + + + 3, 3 + + + System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Bottom, Left, Right + + + $this + + + True + + \ No newline at end of file diff --git a/Source/GrasscutterTools/Pages/PageHotKey.ru-RU.resx b/Source/GrasscutterTools/Pages/PageHotKey.ru-RU.resx new file mode 100644 index 0000000..3568632 --- /dev/null +++ b/Source/GrasscutterTools/Pages/PageHotKey.ru-RU.resx @@ -0,0 +1,151 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Tag + + + HotKey + + + Commands + + + HotKeys + + + - Remove + + + √ AddOrUpdate + + + + 50, 17 + + + HotKey + + + 30, 17 + + + Tag + + \ No newline at end of file diff --git a/Source/GrasscutterTools/Pages/PageHotKey.zh-TW.resx b/Source/GrasscutterTools/Pages/PageHotKey.zh-TW.resx new file mode 100644 index 0000000..7d838b2 --- /dev/null +++ b/Source/GrasscutterTools/Pages/PageHotKey.zh-TW.resx @@ -0,0 +1,138 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + 標籤 + + + 快捷鍵 + + + 快捷執行列表 + + + - 刪除 + + + 快捷鍵 + + + 標籤 + + \ No newline at end of file diff --git a/Source/GrasscutterTools/Pages/PageTasks.cs b/Source/GrasscutterTools/Pages/PageTasks.cs index 141015d..252fd33 100644 --- a/Source/GrasscutterTools/Pages/PageTasks.cs +++ b/Source/GrasscutterTools/Pages/PageTasks.cs @@ -78,7 +78,7 @@ namespace GrasscutterTools.Pages try { Tasks = JsonConvert.DeserializeObject>(File.ReadAllText(TasksJsonPath)); - ListTasks.Items.AddRange(Tasks.Select(t => TaskToViewItem(t)).ToArray()); + ListTasks.Items.AddRange(Tasks.Select(TaskToViewItem).ToArray()); } catch (Exception ex) { diff --git a/Source/GrasscutterTools/Properties/Resources.Designer.cs b/Source/GrasscutterTools/Properties/Resources.Designer.cs index 87cf21d..555572b 100644 --- a/Source/GrasscutterTools/Properties/Resources.Designer.cs +++ b/Source/GrasscutterTools/Properties/Resources.Designer.cs @@ -88,45 +88,42 @@ namespace GrasscutterTools.Properties { } /// - /// 查找类似 //[Activity ids-3.7 by dplek] - ///// Activity - ///2001:1.1未归的熄星 - ///2002:1.3海灯节 - ///2003:1.4风花节 - ///2004:1.5导能原盘-诸论 - ///2005:1.6盛夏!海岛?大冒险! - ///2006:2.0谒索雷痕 - ///2007:2.1韶光抚月 - ///2008:2.2雾海悬谜境 - ///2009:2.3皑尘与雪影 - ///2010:2.4飞彩镌流年 - ///2011:2.5三界路飨祭 - ///2012:2.6堇庭华彩 - ///2013:2.7荒梦藏虞渊 - ///2014:2.8远海诗夏游纪 - ///2015:3.0雕琢童心 - ///2016:3.1杯中遥吟之歌 - ///2017:3.2智巧灵蕈大竞逐 - ///2018:3.3秋津森夜试胆会 - ///2019:3.4磬弦奏华夜 - ///2020:3.5风花的呼吸 - ///2021:3.6盛典与慧业 - ///2022:3.7决战!召唤之巅! - ///// 1.0 - ///1001:海灯节 - ///5001:元素烘炉(test) - ///5002:且试身手 - ///5003:百货奇货 - ///// 1.1 - ///5004:映天之章 - ///5005:元素烘炉 - ///5006:佳肴尚温 - ///5007:飞行挑战 - ///5009:古闻之章(钟离传说-1) - ///5010:鲸天之章(公子传说-1) - ///// 1.2 - ///3001:白垩与黑龙 - ///5 [字符串的其余部分被截断]"; 的本地化字符串。 + /// 查找类似 //[Activity ids-3.8 by dplek] + ///// Activity + ///2001:1.1未归的熄星 + ///2002:1.3海灯节 + ///2003:1.4风花节 + ///2004:1.5导能原盘-诸论 + ///2005:1.6盛夏!海岛?大冒险! + ///2006:2.0谒索雷痕 + ///2007:2.1韶光抚月 + ///2008:2.2雾海悬谜境 + ///2009:2.3皑尘与雪影 + ///2010:2.4飞彩镌流年 + ///2011:2.5三界路飨祭 + ///2012:2.6堇庭华彩 + ///2013:2.7荒梦藏虞渊 + ///2014:2.8远海诗夏游纪 + ///2015:3.0雕琢童心 + ///2016:3.1杯中遥吟之歌 + ///2017:3.2智巧灵蕈大竞逐 + ///2018:3.3秋津森夜试胆会 + ///2019:3.4磬弦奏华夜 + ///2020:3.5风花的呼吸 + ///2021:3.6盛典与慧业 + ///2022:3.7决战!召唤之巅! + ///// 1.0 + ///1001:海灯节 + ///5001:元素烘炉(test) + ///5002:且试身手 + ///5003:百货奇货 + ///// 1.1 + ///5004:映天之章 + ///5005:元素烘炉 + ///5006:佳肴尚温 + ///5007:飞行挑战 + ///5009:古闻之章(钟离传说-1) + ///50 [字符串的其余部分被截断]"; 的本地化字符串。 /// internal static string Activity { get { @@ -638,28 +635,40 @@ namespace GrasscutterTools.Properties { } /// - /// 查找类似 1:TEST_Chest_CutSence - ///2:TEST_GoddessLvUp_CutSence - ///3:Cs_Scene_GoddessUpgrade_01 - ///4:Cs_Scene_DungeonGate_01 - ///5:Cs_Scene_DungeonGate_02 - ///6:Cs_Scene_GoddessActive - ///7:Cs_Scene_GoddessUpgradeDefault - ///8:Cs_Scene_DragonNestSealGate - ///9:Cs_MDAQ501_SwordBreak01 - ///200201:Cs_MDAQ071_DvalinCombat2 - ///10:特瓦林秘境解锁1 - ///11:特瓦林秘境解锁2 - ///12:特瓦林秘境解锁3 - ///13:Cs_RecyclableDungeon_GetReward - ///14:Cs_Scene_GoddessUpgradeDefault_SFX - ///15:Cs_Scene_Goddess_Chord_Wind - ///16:Cs_Scene_Goddess_Chord_Rock + /// 查找类似 1:[测试]空画面 + ///2:[测试]空画面 + ///3:[疑似剧情]风起地七天神像激活 + ///4:未知秘境内某门开启 + ///5:未知秘境内某门开启 + ///6:无画面(UI会闪一下) + ///7:未知区域画面旋转 + ///8:进入风龙废墟秘境 + ///9:达达乌帕谷剑屏障解锁 + ///200201:[剧情]蒙德主线特瓦林最后战斗 秘境ID:20018(测试)或20025(正式) + ///10:风龙废墟解除三层封印1 + ///11:风龙废墟解除三层封印2 + ///12:风龙废墟解除三层封印3 + ///13:秘境奖励领取时 + ///14:未知区域画面旋转 + ///15:不明地下视角 + ///16:不明地下视角 ///17:忍冬之树普通 ///18:忍冬之树形态1 ///19:忍冬之树1升级2 ///20:忍冬之树形态2 - /// [字符串的其余部分被截断]"; 的本地化字符串。 + ///21:忍冬之树2升级3 + ///22:雪山冰本进入时 + ///23:不明地下视角 + ///24:不明地下视角 + ///25:稻妻神樱树远景 + ///26:[世界任务]神樱大祓净化进度1 + ///27:[世界任务]神樱大祓净化进度2 + ///28:[世界任务]神樱大祓净化进度3 + ///29:[世界任务]神樱大祓净化进度4 + ///30:[世界任务]神樱大祓净化进度5 + ///31:稻妻荒海机关解锁 + ///32:稻妻八酝岛阵代屋敷秘境解锁 + ///33:稻妻清籁岛木 [字符串的其余部分被截断]"; 的本地化字符串。 /// internal static string Cutscene { get { @@ -1151,6 +1160,15 @@ namespace GrasscutterTools.Properties { } } + /// + /// 查找类似 快捷键 的本地化字符串。 + /// + internal static string PageHotKey { + get { + return ResourceManager.GetString("PageHotKey", resourceCulture); + } + } + /// /// 查找类似 邮件 的本地化字符串。 /// diff --git a/Source/GrasscutterTools/Properties/Resources.en-us.resx b/Source/GrasscutterTools/Properties/Resources.en-us.resx index b1f2a7e..d598c99 100644 --- a/Source/GrasscutterTools/Properties/Resources.en-us.resx +++ b/Source/GrasscutterTools/Properties/Resources.en-us.resx @@ -360,4 +360,7 @@ Improvement suggestions have been submitted, please use caution to send emails t ..\Resources\en-us\Activity.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 + + HotKey + \ No newline at end of file diff --git a/Source/GrasscutterTools/Properties/Resources.resx b/Source/GrasscutterTools/Properties/Resources.resx index 791b42b..bcdd9fb 100644 --- a/Source/GrasscutterTools/Properties/Resources.resx +++ b/Source/GrasscutterTools/Properties/Resources.resx @@ -372,4 +372,7 @@ ..\Resources\zh-cn\Cutscene.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 + + 快捷键 + \ No newline at end of file diff --git a/Source/GrasscutterTools/Properties/Resources.ru-ru.resx b/Source/GrasscutterTools/Properties/Resources.ru-ru.resx index fd3133d..42f1188 100644 --- a/Source/GrasscutterTools/Properties/Resources.ru-ru.resx +++ b/Source/GrasscutterTools/Properties/Resources.ru-ru.resx @@ -348,4 +348,7 @@ ..\Resources\ru-ru\Activity.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 + + Горячая клавиша + \ No newline at end of file diff --git a/Source/GrasscutterTools/Properties/Resources.zh-TW.resx b/Source/GrasscutterTools/Properties/Resources.zh-TW.resx index 4d8e844..cf92e9f 100644 --- a/Source/GrasscutterTools/Properties/Resources.zh-TW.resx +++ b/Source/GrasscutterTools/Properties/Resources.zh-TW.resx @@ -354,4 +354,7 @@ ..\Resources\zh-tw\Activity.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8 + + 快捷鍵 + \ No newline at end of file diff --git a/Source/GrasscutterTools/Utils/Common.cs b/Source/GrasscutterTools/Utils/Common.cs index 7e43dac..b15491a 100644 --- a/Source/GrasscutterTools/Utils/Common.cs +++ b/Source/GrasscutterTools/Utils/Common.cs @@ -41,5 +41,8 @@ namespace GrasscutterTools.Utils { return Path.Combine(AppDataFolder, filename); } + + + public static KeyGo KeyGo { get; set; } } } \ No newline at end of file diff --git a/Source/GrasscutterTools/Utils/HotKeyItem.cs b/Source/GrasscutterTools/Utils/HotKeyItem.cs index 5781409..c9777ca 100644 --- a/Source/GrasscutterTools/Utils/HotKeyItem.cs +++ b/Source/GrasscutterTools/Utils/HotKeyItem.cs @@ -1,5 +1,5 @@ using System; -using System.Xml.Serialization; +using Newtonsoft.Json; namespace GrasscutterTools.Utils { @@ -14,8 +14,8 @@ namespace GrasscutterTools.Utils /// /// The hot key identifier. /// - [XmlIgnore] - public int HotKeyID { get; set; } + [JsonIgnore] + public int HotKeyId { get; set; } /// /// Gets or sets the name of the Tag. @@ -48,30 +48,6 @@ namespace GrasscutterTools.Utils /// /// true if enabled; otherwise, false. /// - public bool Enabled { get; set; } = true; - - /// - /// Gets or sets the trigger counter. - /// - /// - /// The trigger counter. - /// - public int TriggerCounter { get; set; } - - /// - /// Gets or sets the creation time. - /// - /// - /// The creation time. - /// - public DateTime CreationTime { get; set; } = DateTime.Now; - - /// - /// Gets or sets the last modified time. - /// - /// - /// The last modified time. - /// - public DateTime LastModifiedTime { get; set; } = DateTime.Now; + public bool IsEnabled { get; set; } = true; } } \ No newline at end of file diff --git a/Source/GrasscutterTools/Utils/HotKey.cs b/Source/GrasscutterTools/Utils/KeyGo.cs similarity index 70% rename from Source/GrasscutterTools/Utils/HotKey.cs rename to Source/GrasscutterTools/Utils/KeyGo.cs index af68967..8be34e1 100644 --- a/Source/GrasscutterTools/Utils/HotKey.cs +++ b/Source/GrasscutterTools/Utils/KeyGo.cs @@ -1,82 +1,40 @@ using System; using System.Collections.Generic; -using System.IO; +using System.Linq; using System.Threading; using System.Windows.Forms; -using System.Xml.Serialization; namespace GrasscutterTools.Utils { /// /// KeyGo 核心功能类 /// - public class KeyGo + internal class KeyGo { + public KeyGo(IntPtr formHandle) + { + FormHandle = formHandle; + } + #region Member - private static int _RegMaxID; + private static int _regMaxId; - [XmlIgnore] - public IntPtr FormHandle { get; set; } + private readonly IntPtr FormHandle; public List Items { get; set; } = new List(); #endregion Member - #region FILE IO - - /// - /// Loads the XML. - /// - /// The file path. - /// - 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; - } - - /// - /// Saves the XML. - /// - /// The file path. - 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 /// - /// Regs all key. + /// 注册所有启用的快捷键 /// public void RegAllKey() { - foreach (var item in Items) + foreach (var item in Items.Where(item => item.IsEnabled)) { - if (!item.Enabled) - continue; - try { RegKey(item); @@ -89,7 +47,7 @@ namespace GrasscutterTools.Utils } /// - /// Uns the reg all key. + /// 取消所有快捷键 /// public void UnRegAllKey() { @@ -107,7 +65,7 @@ namespace GrasscutterTools.Utils } /// - /// 注册热键 - 成功后,会设置 HotKeyID + /// 注册热键 - 成功后,会设置 HotKeyId /// /// The item. /// @@ -128,17 +86,17 @@ namespace GrasscutterTools.Utils throw new ArgumentNullException(nameof(item.HotKey), "热键不能为空!"); // 如果注册过该热键,ID不为0。卸载热键会将ID置零。 - if (item.HotKeyID != 0) + if (item.HotKeyId != 0) return; - int id = Interlocked.Increment(ref _RegMaxID); + var id = Interlocked.Increment(ref _regMaxId); var keys = item.HotKey.Split('+'); - Keys keyCode = Keys.None; - AppHotKey.KeyModifiers keyModifiers = AppHotKey.KeyModifiers.None; + var keyCode = Keys.None; + var keyModifiers = AppHotKey.KeyModifiers.None; foreach (var key in keys) { - switch (key.ToLower()) + switch (key.Trim().ToLower()) { case "ctrl": keyModifiers |= AppHotKey.KeyModifiers.Ctrl; @@ -168,11 +126,11 @@ namespace GrasscutterTools.Utils throw new InvalidOperationException("快捷键不能为空!"); AppHotKey.RegKey(FormHandle, id, keyModifiers, keyCode); - item.HotKeyID = id; + item.HotKeyId = id; } /// - /// 注销热键 - 完成后,会清零 HotKeyID + /// 注销热键 - 完成后,会清零 HotKeyId /// /// The item. /// item @@ -180,11 +138,11 @@ namespace GrasscutterTools.Utils { if (item is null) throw new ArgumentNullException(nameof(item)); - if (item.HotKeyID == 0) + if (item.HotKeyId == 0) return; - AppHotKey.UnRegKey(FormHandle, item.HotKeyID); - item.HotKeyID = 0; + AppHotKey.UnRegKey(FormHandle, item.HotKeyId); + item.HotKeyId = 0; } #endregion HotKey Register @@ -207,15 +165,15 @@ namespace GrasscutterTools.Utils /// Processes the hotkey. /// /// The hot key identifier. - public void ProcessHotkey(int hotKey_id) + public void ProcessHotKey(int hotKeyId) { - var hotkey = Items.Find(k => k.HotKeyID == hotKey_id); - if (hotkey != null) + var hotKey = Items.Find(k => k.HotKeyId == hotKeyId); + if (hotKey != null) { - //Console.WriteLine($"ID:{hotkey.HotKeyID} Keys:{hotkey.HotKey} ProcessName:{hotkey.ProcessName}\nStartupPath:{hotkey.StartupPath}"); - ++hotkey.TriggerCounter; + //Console.WriteLine($"ID:{hotKey.HotKeyId} Keys:{hotKey.HotKey} ProcessName:{hotKey.ProcessName}\nStartupPath:{hotKey.StartupPath}"); + //++hotKey.TriggerCounter; // 触发事件,若被外部处理,则内部不再执行 - OnHotKeyTrigger(hotkey); + OnHotKeyTrigger(hotKey); } } @@ -242,7 +200,7 @@ namespace GrasscutterTools.Utils if (item is null) throw new ArgumentNullException(nameof(item)); - if (item.Enabled) + if (item.IsEnabled) RegKey(item); Items.Add(item); } @@ -256,7 +214,7 @@ namespace GrasscutterTools.Utils if (item is null) throw new ArgumentNullException(nameof(item)); - if (item.HotKeyID != 0) + if (item.HotKeyId != 0) UnRegKey(item); Items.Remove(item); } @@ -271,9 +229,9 @@ namespace GrasscutterTools.Utils throw new ArgumentNullException(nameof(item)); // 重新注册 - if (item.HotKeyID != 0) + if (item.HotKeyId != 0) UnRegKey(item); - if (item.Enabled) + if (item.IsEnabled) RegKey(item); }