/// <summary>Wait for any key to be released</summary> /// <param name="timeout">Time until waiting is canceled</param> /// <param name="block">Block all keys from the OS while waiting</param> public static async Task <Key> WaitKeyUp(int?timeout = null, bool block = false) { var job = new TaskCompletionSource <Key>(); void Filter(Key k, bool s) { if (!s) { InputEvent -= Filter; job.TrySetResult(k); } } if (block) { LockAllKeys(true); } InputEvent += Filter; var res = await AsyncTool.Timeout(job.Task, timeout); if (block) { LockAllKeys(false); } return(res.Success ? res.Result : Key.None); }
/// <summary>Wait for an event of one of the listed keys</summary> /// <param name="keys">Array of keys to listen for</param> /// <param name="timeout">Time until waiting is canceled</param> /// <param name="block">Block listed keys from the OS while waiting</param> public static async Task <Key> WaitKey(Key[] keys, int?timeout = null, bool block = false) { var job = new TaskCompletionSource <Key>(TaskCreationOptions.RunContinuationsAsynchronously); void Filter(Key k, bool s) { if (keys.Contains(k)) { InputEvent -= Filter; job.SetResult(k); } } if (block) { LockKey(keys); } InputEvent += Filter; var res = await AsyncTool.Timeout(job.Task, timeout); if (block) { UnlockKey(keys); } return(res.Success ? res.Result : Key.None); }
/// <summary>Wait for the <paramref name="key"/> to be in an up position</summary> /// <param name="timeout">Time until waiting is canceled</param> /// <param name="block">Block <paramref name="key"/> from the OS while waiting</param> public static async Task <bool> WaitKeyUp(Key key, int?timeout = null, bool block = false) { if (!IsDown(key)) { return(true); } var job = new TaskCompletionSource <object>(); void Filter(Key k, bool s) { if (!s && k == key) { InputEvent -= Filter; job.TrySetResult(null); } } if (block) { LockKey(key); } InputEvent += Filter; var res = await AsyncTool.Timeout(job.Task, timeout); if (block) { UnlockKey(key); } return(res.Success); }
public OnlineController(IConfiguration configuration, AsyncTool tool, IMapper mapper) { _configuration = configuration; _connectionString = "Data Source=.;Initial Catalog=Pcr;Integrated Security=True"; _tool = tool ?? throw new ArgumentException(nameof(tool)); _mapper = mapper ?? throw new ArgumentException(nameof(mapper)); }
/// <summary>Wait until the user is no longer idle.</summary> public static async Task <bool> WaitForAwake(int?timeout = null) { var job = new TaskCompletionSource <object>(); void e(Key k, bool s) => job.TrySetResult(null); InputEvent += e; var res = await AsyncTool.Timeout(job.Task, timeout); InputEvent -= e; return(res.Success); }
public static async Task <Job <KeyState> > WaitKey(Func <Key, bool, bool> predicate, int?timeout = null) { if (predicate == null) { throw new ArgumentNullException("Predicate was null"); } var job = new TaskCompletionSource <KeyState>(TaskCreationOptions.RunContinuationsAsynchronously); void Filter(Key key, bool state) { if (predicate.Invoke(key, state)) { InputEvent -= Filter; job.TrySetResult(new KeyState(key, state)); } } InputEvent += Filter; var res = await AsyncTool.Timeout(job.Task, timeout); return(res); }