public async Task Start() { if (Hotkey == null) { throw new Exception("This action object's Hotkey is null"); } else if (Running) { throw new Exception("This action object is already running"); } Running = true; StateID = KeyHandler.GetStateID(Hotkey.MainKey); await Body(); IsStopping = false; Running = false; }
public static async Task <Job <List <Key> > > Record(int?timeout = null, int?maxLength = null, HashSet <Key> endKeys = null, HashSet <Key> cancelKeys = null, CommandContainer container = null) { if (IsNullOrEmpty(endKeys) && container == null) { throw new Exception($"{nameof(container).ToUpperFirst()} should not be null if no end keys are given"); } KeyHandler.LockKeyboard(true); var watch = Stopwatch.StartNew(); var inputs = new List <Key>(); var success = false; Input.SendUp(KeyHandler.DownKeysVirtual.ToArray()); while (watch.ElapsedMilliseconds < timeout) { var res = await KeyHandler.WaitKeyDown(timeout - (int)watch.ElapsedMilliseconds, true); if (res == Key.None) { break; } if (res.IsMouse()) { continue; } if (!IsNullOrEmpty(cancelKeys) && cancelKeys.Contains(res)) { break; } else if (!IsNullOrEmpty(endKeys) && endKeys.Contains(res)) { success = true; break; } else if (res == Key.Backspace) { if (inputs.Count > 0) { inputs.RemoveAt(inputs.Count - 1); } } else { inputs.Add(res); if (maxLength != null && inputs.Count >= maxLength) { success = true; break; } else if (IsNullOrEmpty(endKeys) && container.Commands.ContainsKey(inputs)) { success = true; break; } } } KeyHandler.LockKeyboard(false); if (success) { return(Job.Completed(inputs)); } else { return(Job.Failed()); } bool IsNullOrEmpty(HashSet <Key> keyset) { if (keyset == null || keyset.Count == 0) { return(true); } return(false); } }