Add config-driven action system (launch, url) with factory pattern
This commit is contained in:
@@ -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
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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();
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
public interface IAction
|
||||||
|
{
|
||||||
|
void Execute();
|
||||||
|
}
|
||||||
@@ -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" />
|
<PackageReference Include="HidSharp" Version="2.6.4" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Update="config.json">
|
||||||
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
</None>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -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");
|
var actions = new Dictionary<int, IAction>();
|
||||||
foreach (var d in candidates)
|
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)
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine($"Ouverture de : {device.DevicePath}\n");
|
|
||||||
|
|
||||||
using var stream = device.Open();
|
using var stream = device.Open();
|
||||||
stream.ReadTimeout = Timeout.Infinite;
|
stream.ReadTimeout = Timeout.Infinite;
|
||||||
|
|
||||||
Console.WriteLine($"MaxInputReportLength: {device.GetMaxInputReportLength()}");
|
|
||||||
Console.WriteLine($"MaxOutputReportLength: {device.GetMaxOutputReportLength()}");
|
|
||||||
|
|
||||||
var buffer = new byte[device.GetMaxInputReportLength()];
|
var buffer = new byte[device.GetMaxInputReportLength()];
|
||||||
ushort lastMask = 0;
|
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)
|
while (true)
|
||||||
{
|
{
|
||||||
int count;
|
int count;
|
||||||
try
|
try { count = stream.Read(buffer, 0, buffer.Length); }
|
||||||
{
|
catch (TimeoutException) { continue; }
|
||||||
count = stream.Read(buffer, 0, buffer.Length);
|
|
||||||
}
|
|
||||||
catch (TimeoutException)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int offset = count > 2 ? 1 : 0;
|
int offset = count > 2 ? 1 : 0;
|
||||||
ushort mask = (ushort)(buffer[offset] | (buffer[offset + 1] << 8));
|
ushort mask = (ushort)(buffer[offset] | (buffer[offset + 1] << 8));
|
||||||
@@ -50,10 +43,14 @@ while (true)
|
|||||||
ushort changed = (ushort)(mask ^ lastMask);
|
ushort changed = (ushort)(mask ^ lastMask);
|
||||||
for (int bit = 0; bit < 12; bit++)
|
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($"Bouton {bit} → exécution de l'action");
|
||||||
Console.WriteLine($"Bit {bit}: {(pressed ? "PRESSÉ" : "relâché")}");
|
action.Execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lastMask = mask;
|
lastMask = mask;
|
||||||
|
|||||||
@@ -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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"bindings": {
|
||||||
|
"0": { "type": "launch", "target": "notepad.exe" },
|
||||||
|
"1": { "type": "url", "target": "https://github.com" },
|
||||||
|
"2": { "type": "keystroke", "target": "^{c}" }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user