Add keystroke action type using SharphHook EventSimulator

This commit is contained in:
2026-08-25 01:37:47 +02:00
parent 56bf3ff5bf
commit 9797cd7e79
4 changed files with 40 additions and 1 deletions
+37
View File
@@ -0,0 +1,37 @@
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}")
};
}