diff --git a/host-app/MacroPad.Host/ActionFactory.cs b/host-app/MacroPad.Host/ActionFactory.cs index 8519c8b..72c045e 100644 --- a/host-app/MacroPad.Host/ActionFactory.cs +++ b/host-app/MacroPad.Host/ActionFactory.cs @@ -6,6 +6,7 @@ public static class ActionFactory { "launch" => new LaunchAction(config.Target), "url" => new UrlAction(config.Target), + "keystroke" => new KeystrokeAction(config.Target), _ => null }; } diff --git a/host-app/MacroPad.Host/KeystrokeAction.cs b/host-app/MacroPad.Host/KeystrokeAction.cs new file mode 100644 index 0000000..aa9b787 --- /dev/null +++ b/host-app/MacroPad.Host/KeystrokeAction.cs @@ -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($"Vc{char.ToUpperInvariant(name[0])}"), + _ => throw new ArgumentException($"Touche inconnue : {name}") + }; +} \ No newline at end of file diff --git a/host-app/MacroPad.Host/MacroPad.Host.csproj b/host-app/MacroPad.Host/MacroPad.Host.csproj index 7900485..143a96b 100644 --- a/host-app/MacroPad.Host/MacroPad.Host.csproj +++ b/host-app/MacroPad.Host/MacroPad.Host.csproj @@ -9,6 +9,7 @@ + diff --git a/host-app/MacroPad.Host/config.json b/host-app/MacroPad.Host/config.json index 7a9b5f7..71fc7ef 100644 --- a/host-app/MacroPad.Host/config.json +++ b/host-app/MacroPad.Host/config.json @@ -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" } } } \ No newline at end of file