Add keystroke action type using SharphHook EventSimulator
This commit is contained in:
@@ -6,6 +6,7 @@ public static class ActionFactory
|
||||
{
|
||||
"launch" => new LaunchAction(config.Target),
|
||||
"url" => new UrlAction(config.Target),
|
||||
"keystroke" => new KeystrokeAction(config.Target),
|
||||
_ => null
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
<PackageReference Include="HidSharp" Version="2.6.4" />
|
||||
<PackageReference Include="SharpHook" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -2,6 +2,6 @@
|
||||
"bindings": {
|
||||
"0": { "type": "launch", "target": "notepad.exe" },
|
||||
"1": { "type": "url", "target": "https://github.com" },
|
||||
"2": { "type": "keystroke", "target": "^{c}" }
|
||||
"2": { "type": "keystroke", "target": "ctrl+shift+esc" }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user