Skip to content

Commit

Permalink
Add hotkey global switch
Browse files Browse the repository at this point in the history
  • Loading branch information
jie65535 committed Sep 29, 2023
1 parent 1c49c41 commit 749f324
Show file tree
Hide file tree
Showing 13 changed files with 364 additions and 186 deletions.
3 changes: 3 additions & 0 deletions Source/GrasscutterTools/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<setting name="IsUpgraded" serializeAs="String">
<value>False</value>
</setting>
<setting name="IsHotkeyEenabled" serializeAs="String">
<value>True</value>
</setting>
</GrasscutterTools.Properties.Settings>
</userSettings>
</configuration>
24 changes: 24 additions & 0 deletions Source/GrasscutterTools/Forms/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,30 @@ private void FormMain_KeyDown(object sender, KeyEventArgs e)
if (i < ListPages.Items.Count)
ListPages.SelectedIndex = i;
}
else if (Common.KeyGo.IsEnabled == false)
{
foreach (var hotkeyItem in Common.KeyGo.Items)
{
if (!hotkeyItem.IsEnabled) continue;

var t = hotkeyItem.HotKey.LastIndexOf('+');
var key = (t >= 0) ? hotkeyItem.HotKey.Substring(t+1) : hotkeyItem.HotKey;
if (e.KeyCode != (Keys)Enum.Parse(typeof(Keys), key.Trim()))
continue;

if (t >= 0)
{
if (hotkeyItem.HotKey.Contains("Ctrl") && !e.Control)
continue;
if (hotkeyItem.HotKey.Contains("Shift") && !e.Shift)
continue;
if (hotkeyItem.HotKey.Contains("Alt") && !e.Alt)
continue;
}
BeginInvoke(new Func<Task>(() => RunRawCommands(hotkeyItem.Commands)));
break;
}
}
}

/// <summary>
Expand Down
15 changes: 14 additions & 1 deletion Source/GrasscutterTools/Pages/PageHotKey.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 57 additions & 10 deletions Source/GrasscutterTools/Pages/PageHotKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ public PageHotKey()
/// </summary>
private bool HotKeysChanged;

/// <summary>
/// 加载用户热键列表
/// </summary>
private List<HotKeyItem> LoadKeyItems()
{
List<HotKeyItem> list = null;
try
{
Logger.I(TAG, "Loading HotKey json file from: " + HotKeysFilePath);
list = JsonConvert.DeserializeObject<List<HotKeyItem>>(File.ReadAllText(HotKeysFilePath));
}
catch (Exception ex)
{
Logger.E(TAG, "Parsing HotKeys.json failed", ex);
}

if (list == null || list.Count == 0)
{
// 默认把移动命令加到列表
list = new List<HotKeyItem>
{
new("", "/tp ^ ^ ^10", "NumPad8", false),
new("", "/tp ^ ^ ^-10", "NumPad5", false),
new("", "/tp ^-10 ^ ^", "NumPad4", false),
new("", "/tp ^10 ^ ^", "NumPad6", false),
new("↑^↑", "/tp ~ ~10 ~", "NumPad0", false),
};
}

return list;
}

/// <summary>
/// 初始化快捷键
/// </summary>
Expand All @@ -62,15 +94,16 @@ private void InitHotKeys()
return;
try
{
Logger.I(TAG, "Loading HotKey json file from: " + HotKeysFilePath);
Common.KeyGo.Items = JsonConvert.DeserializeObject<List<HotKeyItem>>(File.ReadAllText(HotKeysFilePath));
// 还原设置
Common.KeyGo.IsEnabled = ChkEnableGlobal.Checked = Settings.Default.IsHotkeyEenabled;
Common.KeyGo.Items = LoadKeyItems();
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);
Logger.W(TAG, "Failed to InitHotKeys", ex);
}
}

Expand All @@ -79,6 +112,8 @@ private void InitHotKeys()
/// </summary>
public override void OnClosed()
{
Settings.Default.IsHotkeyEenabled = Common.KeyGo.IsEnabled;

Logger.I(TAG, "Cancel all HotKeys");
Common.KeyGo.UnRegAllKey();

Expand All @@ -90,7 +125,7 @@ public override void OnClosed()
/// <summary>
/// 将实体转为视图对象
/// </summary>
private static ListViewItem HotKeyItemToViewItem(HotKeyItem item) => new ListViewItem(new[]
private static ListViewItem HotKeyItemToViewItem(HotKeyItem item) => new(new[]
{
item.Tag,
item.HotKey,
Expand Down Expand Up @@ -130,12 +165,7 @@ private void BtnAddOrUpdate_Click(object sender, EventArgs e)
var i = Common.KeyGo.Items.FindIndex(it => it.Tag == tag);
if (i == -1)
{
var item = new HotKeyItem
{
Tag = tag,
Commands = commands,
HotKey = hotKey
};
var item = new HotKeyItem(tag, commands, hotKey);
Logger.I(TAG, $"New HotKey item [{hotKey}]");
Common.KeyGo.AddHotKey(item);
LvHotKeyList.Items.Add(HotKeyItemToViewItem(item));
Expand Down Expand Up @@ -260,5 +290,22 @@ public void AddNewHotKey(string tag)
TxtHotKey.Tag = "";
TxtTag.Text = tag;
}

/// <summary>
/// 切换启用全局快捷键时触发
/// </summary>
private void ChkEnableGlobal_CheckedChanged(object sender, EventArgs e)
{
try
{
Common.KeyGo.IsEnabled = ChkEnableGlobal.Checked;
}
catch (Exception ex)
{
Logger.E(TAG, "Failed to switch global hotkeys", ex);
MessageBox.Show(ex.Message, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

}
}
}
11 changes: 10 additions & 1 deletion Source/GrasscutterTools/Pages/PageHotKey.en-US.resx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@
<data name="ColCommand.Text" xml:space="preserve">
<value>Commands</value>
</data>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="ChkEnableGlobal.Location" type="System.Drawing.Point, System.Drawing">
<value>523, -2</value>
</data>
<data name="ChkEnableGlobal.Size" type="System.Drawing.Size, System.Drawing">
<value>111, 21</value>
</data>
<data name="ChkEnableGlobal.Text" xml:space="preserve">
<value>Global HotKey</value>
</data>
<data name="GrpHotKeyList.Text" xml:space="preserve">
<value>HotKeys</value>
</data>
Expand All @@ -135,7 +145,6 @@
<data name="BtnAddOrUpdate.Text" xml:space="preserve">
<value>√ AddOrUpdate</value>
</data>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="LblHotKeyLabel.Size" type="System.Drawing.Size, System.Drawing">
<value>50, 17</value>
</data>
Expand Down
Loading

0 comments on commit 749f324

Please sign in to comment.