Skip to content

Commit e7d1581

Browse files
The beginning of Hotkey Handler, take two
- Start the hotkey binding UI over from scratch - Add basic hotkey parsing - Remove MSI Center hotkey and settings (may be added back later if more reliable MSI Center key detection method is found) Hotkeys do not function yet and there is still much to be implemented/fixed. A debug label that shows whether a hotkey binding is in progress has been left in in this commit. Feel free to report bugs with the hotkey binding UI, however there are a few known bugs: - Some key names may not match expected names (e.g. numbers show as "D0"-"D9", many special characters show up as Oem characters...) - The "Switch to fan profile" parsing isn't properly implemented yet and results in a crash when saving the hotkey config
1 parent 864223a commit e7d1581

File tree

9 files changed

+524
-833
lines changed

9 files changed

+524
-833
lines changed

YAMDCC.HotkeyHandler/Config/Hotkey.cs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Windows.Forms;
3+
4+
namespace YAMDCC.HotkeyHandler.Config;
5+
6+
public class Hotkey
7+
{
8+
/// <summary>
9+
/// The key that must be pressed (along with its modifiers)
10+
/// to trigger this hotkey's action.
11+
/// </summary>
12+
public Keys KeyCode { get; set; }
13+
14+
/// <summary>
15+
/// The modifiers that must be pressed to trigger this hotkey's action.
16+
/// </summary>
17+
public HotkeyModifiers Modifiers { get; set; }
18+
19+
/// <summary>
20+
/// The action to take when the hotkey is pressed.
21+
/// </summary>
22+
public HotkeyAction Action { get; set; }
23+
24+
/// <summary>
25+
/// Used with <see cref="HotkeyAction.SwitchFanProf"/>, ignored with other actions.
26+
/// </summary>
27+
/// <remarks>
28+
/// The zero-indexed fan profile to switch to when the associated
29+
/// hotkey is pressed. Set to -1 to cycle through all fan profiles.
30+
/// </remarks>
31+
public int ActionData { get; set; }
32+
}
33+
34+
[Flags]
35+
public enum HotkeyModifiers
36+
{
37+
None = 0,
38+
Alt = 1,
39+
Ctrl = 2,
40+
Shift = 4,
41+
Windows = 8,
42+
}
43+
44+
public enum HotkeyAction
45+
{
46+
None,
47+
OpenConfEditor,
48+
ToggleFullBlast,
49+
ToggleWinFnSwap,
50+
KeyLightUp,
51+
KeyLightDown,
52+
SwitchFanProf,
53+
}
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Xml;
5+
using System.Xml.Serialization;
6+
using YAMDCC.Common.Configs;
7+
8+
namespace YAMDCC.HotkeyHandler.Config;
9+
10+
public class HotkeyConf
11+
{
12+
[XmlAttribute]
13+
public int Ver { get; set; } = 1;
14+
15+
[XmlIgnore]
16+
private static readonly int ExpectedVer = 1;
17+
18+
[XmlArray]
19+
public List<Hotkey> Hotkeys { get; set; } = [];
20+
21+
/// <summary>
22+
/// Parses a hotkey config XML and returns a
23+
/// <see cref="HotkeyConf"/> object.
24+
/// </summary>
25+
/// <param name="path">
26+
/// The path to an XML config file.
27+
/// </param>
28+
/// <exception cref="InvalidConfigException"/>
29+
/// <exception cref="ArgumentNullException"/>
30+
/// <exception cref="FileNotFoundException"/>
31+
/// <exception cref="InvalidOperationException"/>
32+
public static HotkeyConf Load(string path)
33+
{
34+
XmlSerializer serialiser = new(typeof(HotkeyConf));
35+
using (XmlReader reader = XmlReader.Create(path))
36+
{
37+
HotkeyConf cfg = (HotkeyConf)serialiser.Deserialize(reader);
38+
return cfg.IsValid() ? cfg : throw new InvalidConfigException();
39+
}
40+
}
41+
42+
/// <summary>
43+
/// Saves a hotkey config to the specified location.
44+
/// </summary>
45+
/// <param name="path">
46+
/// The XML file to write to.
47+
/// </param>
48+
/// <exception cref="ArgumentNullException"/>
49+
/// <exception cref="InvalidOperationException"/>
50+
public void Save(string path)
51+
{
52+
XmlSerializer serialiser = new(typeof(HotkeyConf));
53+
XmlWriterSettings settings = new()
54+
{
55+
Indent = true,
56+
IndentChars = "\t",
57+
};
58+
59+
using (XmlWriter writer = XmlWriter.Create(path, settings))
60+
{
61+
serialiser.Serialize(writer, this);
62+
}
63+
}
64+
65+
/// <summary>
66+
/// Performs some validation on the loaded config to make
67+
/// sure it is in the expected format.
68+
/// </summary>
69+
/// <remarks>
70+
/// This does NOT guarantee the loaded config is valid!
71+
/// (e.g. register values are not checked)
72+
/// </remarks>
73+
/// <returns>
74+
/// <see langword="true"/> if the config is valid, otherwise <see langword="false"/>.
75+
/// </returns>
76+
private bool IsValid()
77+
{
78+
// Check the config version.
79+
// Pretty self-explanatory, if the loaded config is older/newer
80+
// than the version expected by the config library, don't bother
81+
// checking anything else as some/all of it is probably invalid.
82+
if (Ver != ExpectedVer)
83+
{
84+
return false;
85+
}
86+
87+
// All other values are considered to be valid; return true.
88+
return true;
89+
}
90+
}

YAMDCC.HotkeyHandler/KeyHook.cs

-160
This file was deleted.

YAMDCC.HotkeyHandler/KeyHookEventArgs.cs

-40
This file was deleted.

0 commit comments

Comments
 (0)