private async Task PlayOneShot(int offset, ObservableCollection <ButtonAction> actions) { foreach (var action in actions) { if (action.TimeInMilliseconds > 0) { //yes this is precise only to the nearest KeyDownRepeatDelay milliseconds. repeated keys are on a 60 millisecond boundary, so the UI could be locked to 60ms increments only var timeLeft = action.TimeInMilliseconds; while (timeLeft > 0) { await Task.Delay(Keyboard.KeyDownRepeatDelay); timeLeft -= Keyboard.KeyDownRepeatDelay; } } Keyboard.SendKeyPress(action.ScanCode, action.IsKeyUp, action.IsExtended); if (action.IsKeyUp) { KeystrokeUpSent?.Invoke(this, new KeystrokeSentEventArgs(offset, offset, action.ScanCode, action.IsKeyUp, action.IsExtended)); } else { KeystrokeDownSent?.Invoke(this, new KeystrokeSentEventArgs(offset, offset, action.ScanCode, action.IsKeyUp, action.IsExtended)); } } }
private async Task PlayMacroOnce(int offset, ObservableCollection <ButtonAction> actions) { MacroStarted?.Invoke(this, new MacroStartedEventArgs(offset, (int)Win32Structures.ScanCodeShort.MACRO_STARTED)); foreach (var action in actions) { if (_activeMacros.ContainsKey(offset) == false) { break; } if (action.TimeInMilliseconds > 0) { //yes this is precise only to the nearest KeyDownRepeatDelay milliseconds. repeated keys are on a 60 millisecond boundary, so the UI could be locked to 60ms increments only var timeLeft = action.TimeInMilliseconds; while (timeLeft > 0) { await Task.Delay(Keyboard.KeyDownRepeatDelay); if (!_activeMacros.ContainsKey(offset)) { return; } timeLeft -= Keyboard.KeyDownRepeatDelay; } } Keyboard.SendKeyPress(action.ScanCode, action.IsKeyUp, action.IsExtended); if (action.IsKeyUp) { KeystrokeUpSent?.Invoke(this, new KeystrokeSentEventArgs(offset, offset, action.ScanCode, action.IsKeyUp, action.IsExtended)); } else { KeystrokeDownSent?.Invoke(this, new KeystrokeSentEventArgs(offset, offset, action.ScanCode, action.IsKeyUp, action.IsExtended)); } } _activeMacros.TryRemove(offset, out _); }
private void DequeueLoop() { var keyUpList = new List <Tuple <int, ButtonAction> >(); Logging.Log.Debug("Dequeue loop started"); foreach (var job in _actionJobs.GetConsumingEnumerable()) { if (keyUpList.Count > 0) { foreach (var keyUp in keyUpList.Where(i => i.Item1 == job.Offset).ToList()) { Keyboard.SendKeyPress(keyUp.Item2.ScanCode, keyUp.Item2.IsKeyUp, keyUp.Item2.IsExtended); keyUpList.Remove(keyUp); KeystrokeUpSent?.Invoke(this, new KeystrokeSentEventArgs(job.MapId, keyUp.Item1, keyUp.Item2.ScanCode, keyUp.Item2.IsKeyUp, keyUp.Item2.IsExtended)); } } if (job.Actions == null) { continue; } foreach (var action in job.Actions) { if (action.IsKeyUp) { keyUpList.Add(new Tuple <int, ButtonAction>(job.Offset, action)); } else { Keyboard.SendKeyPress(action.ScanCode, action.IsKeyUp, action.IsExtended); KeystrokeDownSent?.Invoke(this, new KeystrokeSentEventArgs(job.MapId, job.Offset, action.ScanCode, action.IsKeyUp, action.IsExtended)); } } } Logging.Log.Debug("Dequeue loop stopped"); }
private void Device_KeystrokeUpSent(object sender, KeystrokeSentEventArgs e) { KeystrokeUpSent?.Invoke(sender, e); }