Add config-driven action system (launch, url) with factory pattern

This commit is contained in:
2026-08-25 00:48:09 +02:00
parent 909d9aa13c
commit 56bf3ff5bf
8 changed files with 86 additions and 26 deletions
+12
View File
@@ -0,0 +1,12 @@
public static class ActionFactory
{
public static IAction? Create(BindingConfig config)
{
return config.Type switch
{
"launch" => new LaunchAction(config.Target),
"url" => new UrlAction(config.Target),
_ => null
};
}
}
+10
View File
@@ -0,0 +1,10 @@
public class BindingConfig
{
public string Type { get; set; } = "";
public string Target { get; set; } = "";
}
public class Config
{
public Dictionary<string, BindingConfig> Bindings { get; set; } = new();
}
+4
View File
@@ -0,0 +1,4 @@
public interface IAction
{
void Execute();
}
+12
View File
@@ -0,0 +1,12 @@
using System.Diagnostics;
public class LaunchAction : IAction
{
private readonly string _target;
public LaunchAction(string target) => _target = target;
public void Execute()
{
Process.Start(new ProcessStartInfo(_target) { UseShellExecute = true });
}
}
@@ -11,4 +11,10 @@
<PackageReference Include="HidSharp" Version="2.6.4" />
</ItemGroup>
<ItemGroup>
<None Update="config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
+23 -26
View File
@@ -1,46 +1,39 @@
using HidSharp;
using System.Text.Json;
using HidSharp;
var candidates = DeviceList.Local.GetHidDevices(vendorID: 0x2341, productID: 0x8036).ToList();
var configPath = Path.Combine(AppContext.BaseDirectory, "config.json");
var configJson = File.ReadAllText(configPath);
var config = JsonSerializer.Deserialize<Config>(configJson, new JsonSerializerOptions { PropertyNameCaseInsensitive = true })
?? new Config();
Console.WriteLine($"{candidates.Count} interface(s) trouvée(s) pour ce VID/PID :\n");
foreach (var d in candidates)
var actions = new Dictionary<int, IAction>();
foreach (var (key, binding) in config.Bindings)
{
Console.WriteLine($" {d.DevicePath}");
var action = ActionFactory.Create(binding);
if (action is not null && int.TryParse(key, out var bit))
actions[bit] = action;
}
Console.WriteLine();
var device = candidates.FirstOrDefault(d => !d.DevicePath.Contains("kbd", StringComparison.OrdinalIgnoreCase));
var device = DeviceList.Local.GetHidDevices(vendorID: 0x2341, productID: 0x8036).FirstOrDefault();
if (device is null)
{
Console.WriteLine("Interface Raw HID non trouvée (seule l'interface clavier standard a été détectée).");
Console.WriteLine("Pad non trouvé. Vérifie qu'il est branché.");
return;
}
Console.WriteLine($"Ouverture de : {device.DevicePath}\n");
using var stream = device.Open();
stream.ReadTimeout = Timeout.Infinite;
Console.WriteLine($"MaxInputReportLength: {device.GetMaxInputReportLength()}");
Console.WriteLine($"MaxOutputReportLength: {device.GetMaxOutputReportLength()}");
var buffer = new byte[device.GetMaxInputReportLength()];
ushort lastMask = 0;
Console.WriteLine("En écoute... appuie sur chaque bouton un par un (Ctrl+C pour quitter)\n");
Console.WriteLine("En écoute...\n");
while (true)
{
int count;
try
{
count = stream.Read(buffer, 0, buffer.Length);
}
catch (TimeoutException)
{
continue;
}
try { count = stream.Read(buffer, 0, buffer.Length); }
catch (TimeoutException) { continue; }
int offset = count > 2 ? 1 : 0;
ushort mask = (ushort)(buffer[offset] | (buffer[offset + 1] << 8));
@@ -50,10 +43,14 @@ while (true)
ushort changed = (ushort)(mask ^ lastMask);
for (int bit = 0; bit < 12; bit++)
{
if ((changed & (1 << bit)) != 0)
bool bitChanged = (changed & (1 << bit)) != 0;
bool nowPressed = (mask & (1 << bit)) != 0;
// On déclenche seulement au moment où le bouton est pressé, pas relâché
if (bitChanged && nowPressed && actions.TryGetValue(bit, out var action))
{
bool pressed = (mask & (1 << bit)) != 0;
Console.WriteLine($"Bit {bit}: {(pressed ? "PRESSÉ" : "relâché")}");
Console.WriteLine($"Bouton {bit} → exécution de l'action");
action.Execute();
}
}
lastMask = mask;
+12
View File
@@ -0,0 +1,12 @@
using System.Diagnostics;
public class UrlAction : IAction
{
private readonly string _target;
public UrlAction(string target) => _target = target;
public void Execute()
{
Process.Start(new ProcessStartInfo(_target) { UseShellExecute = true });
}
}
+7
View File
@@ -0,0 +1,7 @@
{
"bindings": {
"0": { "type": "launch", "target": "notepad.exe" },
"1": { "type": "url", "target": "https://github.com" },
"2": { "type": "keystroke", "target": "^{c}" }
}
}