using AutoAgent.Domain.Utils; using Google.Protobuf; namespace AutoAgent.Domain.Models { public class KeyShortcut { private readonly static DelayGenerator _dGen = new(new RandomNumberGenerator(75, 200)); private Queue _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 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 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 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; } } }