Skip to content

Commit

Permalink
The beginning of Hotkey Handler, take two
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
Sparronator9999 committed Feb 24, 2025
1 parent 864223a commit e7d1581
Show file tree
Hide file tree
Showing 9 changed files with 524 additions and 833 deletions.
53 changes: 53 additions & 0 deletions YAMDCC.HotkeyHandler/Config/Hotkey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Windows.Forms;

namespace YAMDCC.HotkeyHandler.Config;

public class Hotkey
{
/// <summary>
/// The key that must be pressed (along with its modifiers)
/// to trigger this hotkey's action.
/// </summary>
public Keys KeyCode { get; set; }

/// <summary>
/// The modifiers that must be pressed to trigger this hotkey's action.
/// </summary>
public HotkeyModifiers Modifiers { get; set; }

/// <summary>
/// The action to take when the hotkey is pressed.
/// </summary>
public HotkeyAction Action { get; set; }

/// <summary>
/// Used with <see cref="HotkeyAction.SwitchFanProf"/>, ignored with other actions.
/// </summary>
/// <remarks>
/// The zero-indexed fan profile to switch to when the associated
/// hotkey is pressed. Set to -1 to cycle through all fan profiles.
/// </remarks>
public int ActionData { get; set; }
}

[Flags]
public enum HotkeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
Windows = 8,
}

public enum HotkeyAction
{
None,
OpenConfEditor,
ToggleFullBlast,
ToggleWinFnSwap,
KeyLightUp,
KeyLightDown,
SwitchFanProf,
}
90 changes: 90 additions & 0 deletions YAMDCC.HotkeyHandler/Config/HotkeyConf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using YAMDCC.Common.Configs;

namespace YAMDCC.HotkeyHandler.Config;

public class HotkeyConf
{
[XmlAttribute]
public int Ver { get; set; } = 1;

[XmlIgnore]
private static readonly int ExpectedVer = 1;

[XmlArray]
public List<Hotkey> Hotkeys { get; set; } = [];

/// <summary>
/// Parses a hotkey config XML and returns a
/// <see cref="HotkeyConf"/> object.
/// </summary>
/// <param name="path">
/// The path to an XML config file.
/// </param>
/// <exception cref="InvalidConfigException"/>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="FileNotFoundException"/>
/// <exception cref="InvalidOperationException"/>
public static HotkeyConf Load(string path)
{
XmlSerializer serialiser = new(typeof(HotkeyConf));
using (XmlReader reader = XmlReader.Create(path))
{
HotkeyConf cfg = (HotkeyConf)serialiser.Deserialize(reader);
return cfg.IsValid() ? cfg : throw new InvalidConfigException();
}
}

/// <summary>
/// Saves a hotkey config to the specified location.
/// </summary>
/// <param name="path">
/// The XML file to write to.
/// </param>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="InvalidOperationException"/>
public void Save(string path)
{
XmlSerializer serialiser = new(typeof(HotkeyConf));
XmlWriterSettings settings = new()
{
Indent = true,
IndentChars = "\t",
};

using (XmlWriter writer = XmlWriter.Create(path, settings))
{
serialiser.Serialize(writer, this);
}
}

/// <summary>
/// Performs some validation on the loaded config to make
/// sure it is in the expected format.
/// </summary>
/// <remarks>
/// This does NOT guarantee the loaded config is valid!
/// (e.g. register values are not checked)
/// </remarks>
/// <returns>
/// <see langword="true"/> if the config is valid, otherwise <see langword="false"/>.
/// </returns>
private bool IsValid()
{
// Check the config version.
// Pretty self-explanatory, if the loaded config is older/newer
// than the version expected by the config library, don't bother
// checking anything else as some/all of it is probably invalid.
if (Ver != ExpectedVer)
{
return false;
}

// All other values are considered to be valid; return true.
return true;
}
}
160 changes: 0 additions & 160 deletions YAMDCC.HotkeyHandler/KeyHook.cs

This file was deleted.

40 changes: 0 additions & 40 deletions YAMDCC.HotkeyHandler/KeyHookEventArgs.cs

This file was deleted.

Loading

0 comments on commit e7d1581

Please sign in to comment.