win32-automation-agent/Domain/Models/KeyShortcut.cs

92 lines
2.7 KiB
C#
Raw Normal View History

2026-01-08 21:33:01 +03:00
using AutoAgent.Domain.Utils;
using Google.Protobuf;
namespace AutoAgent.Domain.Models
{
public class KeyShortcut
{
private readonly static DelayGenerator _dGen = new(new RandomNumberGenerator<int>(75, 200));
private Queue<Key> _queue = new();
public KeyShortcut(Key key) { And(key); }
public KeyShortcut(string key) { And(key); }
public KeyShortcut And(Key key)
{
_queue.Enqueue(key);
return this;
}
public KeyShortcut And(string key)
{
return And(new Key(key));
}
public IEnumerable<InputAction> GetHoldDownActions()
{
Key key;
var actions = new InputAction[_queue.Count];
for (int i = 0, n = _queue.Count; i < n;)
{
_queue.Enqueue(key = _queue.Dequeue());
actions[i++] = new InputAction()
{
DelayMs = (uint)_dGen.Next().Value,
Type = InputType.KeyDown,
Payload = ByteString.CopyFrom(IPCHandler.Serialize(new ButtonInput { Button = key.Code }))
};
}
return actions;
}
public IEnumerable<InputAction> GetHoldUpActions()
{
Key key;
var actions = new InputAction[_queue.Count];
for (int n = _queue.Count; n > 0;)
{
_queue.Enqueue(key = _queue.Dequeue());
actions[--n] = new InputAction()
{
DelayMs = (uint)_dGen.Next().Value,
Type = InputType.KeyUp,
Payload = ByteString.CopyFrom(IPCHandler.Serialize(new ButtonInput { Button = key.Code }))
};
}
return actions;
}
public IEnumerable<InputAction> GetInputActions()
{
Key key;
int n = _queue.Count * 2;
var actions = new InputAction[n];
for (int i = 0; i < n;)
{
_queue.Enqueue(key = _queue.Dequeue());
actions[i++] = new InputAction()
{
DelayMs = (uint)_dGen.Next().Value,
Type = InputType.KeyDown,
Payload = ByteString.CopyFrom(IPCHandler.Serialize(new ButtonInput { Button = key.Code }))
};
actions[--n] = new InputAction()
{
DelayMs = (uint)_dGen.Next().Value,
Type = InputType.KeyUp,
Payload = ByteString.CopyFrom(IPCHandler.Serialize(new ButtonInput { Button = key.Code }))
};
}
return actions;
}
}
}