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
+1
View File
@@ -6,6 +6,7 @@ public static class ActionFactory
{ {
"launch" => new LaunchAction(config.Target), "launch" => new LaunchAction(config.Target),
"url" => new UrlAction(config.Target), "url" => new UrlAction(config.Target),
"keystroke" => new KeystrokeAction(config.Target),
_ => null _ => null
}; };
} }
+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}")
};
}
@@ -9,6 +9,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="HidSharp" Version="2.6.4" /> <PackageReference Include="HidSharp" Version="2.6.4" />
<PackageReference Include="SharpHook" Version="8.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+1 -1
View File
@@ -2,6 +2,6 @@
"bindings": { "bindings": {
"0": { "type": "launch", "target": "notepad.exe" }, "0": { "type": "launch", "target": "notepad.exe" },
"1": { "type": "url", "target": "https://github.com" }, "1": { "type": "url", "target": "https://github.com" },
"2": { "type": "keystroke", "target": "^{c}" } "2": { "type": "keystroke", "target": "ctrl+shift+esc" }
} }
} }