Files
macro-pad/host-app/MacroPad.Host/KeystrokeAction.cs
T

37 lines
1.2 KiB
C#

using SharpHook.Data;
using SharpHook.Simulation;
public class KeystrokeAction : IAction, IDisposable
{
private readonly KeyCode[] _keys;
private readonly EventSimulator _simulator = EventSimulator.Create("MacroPad.Host");
public KeystrokeAction(string target)
{
_keys = target.Split('+', StringSplitOptions.TrimEntries)
.Select(ParseKey)
.ToArray();
}
public void Execute()
{
_simulator.SimulateKeyStroke(_keys);
}
public void Dispose() => _simulator.Dispose();
private static KeyCode ParseKey(string name) => name.ToLowerInvariant() switch
{
"ctrl" or "control" => KeyCode.VcLeftControl,
"shift" => KeyCode.VcLeftShift,
"alt" => KeyCode.VcLeftAlt,
"win" or "super" or "meta" => KeyCode.VcLeftMeta,
"esc" or "escape" => KeyCode.VcEscape,
"tab" => KeyCode.VcTab,
"enter" or "return" => KeyCode.VcEnter,
"space" => KeyCode.VcSpace,
"delete" or "del" => KeyCode.VcDelete,
_ when name.Length == 1 => Enum.Parse<KeyCode>($"Vc{char.ToUpperInvariant(name[0])}"),
_ => throw new ArgumentException($"Touche inconnue : {name}")
};
}