diff --git a/RELEASE.md b/RELEASE.md index 2cb85f0..e4a196f 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -11,6 +11,7 @@ - Hide `Use Lizard Mouse/Buttons` as it does something different than people are used to - Fix `LT/RT` to trigger up to 50%, instead of 100% - Add mapping for `STEAM+DPadUp` +- Usage of `KeyboardController` will now generate key repeats ## 0.4.x diff --git a/SteamController/Devices/KeyboardController.cs b/SteamController/Devices/KeyboardController.cs index f0629e1..2d5a3ba 100644 --- a/SteamController/Devices/KeyboardController.cs +++ b/SteamController/Devices/KeyboardController.cs @@ -1,13 +1,17 @@ using WindowsInput; +using KeyRepeats = System.Tuple; namespace SteamController.Devices { public class KeyboardController : IDisposable { + public static readonly TimeSpan FirstRepeat = TimeSpan.FromMilliseconds(75); + public static readonly TimeSpan NextRepeats = TimeSpan.FromMilliseconds(150); + InputSimulator simulator = new InputSimulator(); - HashSet keyCodes = new HashSet(); - HashSet lastKeyCodes = new HashSet(); + Dictionary keyCodes = new Dictionary(); + Dictionary lastKeyCodes = new Dictionary(); public KeyboardController() { @@ -19,77 +23,89 @@ namespace SteamController.Devices public bool this[VirtualKeyCode button] { - get { return keyCodes.Contains(button); } + get { return keyCodes.ContainsKey(button); } set { if (value) - keyCodes.Add(button); + { + if (keyCodes.ContainsKey(button)) + return; + var keyRepeat = lastKeyCodes.GetValueOrDefault(button) ?? new KeyRepeats(DateTime.Now.Add(FirstRepeat), 0); + keyCodes.Add(button, keyRepeat); + } else + { keyCodes.Remove(button); + } } } public VirtualKeyCode[] DownKeys { - get { return keyCodes.ToArray(); } + get { return keyCodes.Keys.ToArray(); } } internal void BeforeUpdate() { lastKeyCodes = keyCodes; - keyCodes = new HashSet(); + keyCodes = new Dictionary(); } internal void Update() { - try + // Key Up: it is missing now + foreach (var keyUp in lastKeyCodes.Except(keyCodes)) { - // Key Up: it is missing now - var keyUp = lastKeyCodes.Except(keyCodes).ToArray(); - if (keyUp.Any()) - simulator.Keyboard.KeyUp(keyUp); - - // Key Down: new keys being down - var keyDown = keyCodes.Except(lastKeyCodes).ToArray(); - if (keyDown.Any()) - simulator.Keyboard.KeyUp(keyDown); + try + { + // if key was not yet pressed, send it + if (keyUp.Value.Item2 < 0) + simulator.Keyboard.KeyPress(keyUp.Key); + simulator.Keyboard.KeyUp(keyUp.Key); + } + catch (InvalidOperationException) { } } - catch (InvalidOperationException) + + // Key Down: new keys being down + foreach (var keyUp in keyCodes.Except(lastKeyCodes)) { + try { simulator.Keyboard.KeyDown(keyUp.Key); } + catch (InvalidOperationException) { } + } + + // Key Repeats + var now = DateTime.Now; + foreach (var keyPress in keyCodes) + { + if (keyPress.Value.Item1 > now) + continue; + + try { simulator.Keyboard.KeyPress(keyPress.Key); } + catch (InvalidOperationException) { } + + keyCodes[keyPress.Key] = new KeyRepeats( + DateTime.Now.Add(NextRepeats), + keyPress.Value.Item2 + 1 + ); } } public void KeyPress(params VirtualKeyCode[] keyCodes) { - try - { - simulator.Keyboard.KeyPress(keyCodes); - } - catch (InvalidOperationException) - { - } + try { simulator.Keyboard.KeyPress(keyCodes); } + catch (InvalidOperationException) { } } public void KeyPress(VirtualKeyCode modifierKey, params VirtualKeyCode[] keyCodes) { - try - { - simulator.Keyboard.ModifiedKeyStroke(modifierKey, keyCodes); - } - catch (InvalidOperationException) - { - } + try { simulator.Keyboard.ModifiedKeyStroke(modifierKey, keyCodes); } + catch (InvalidOperationException) { } } public void KeyPress(IEnumerable modifierKeys, params VirtualKeyCode[] keyCodes) { - try - { - simulator.Keyboard.ModifiedKeyStroke(modifierKeys, keyCodes); - } - catch (InvalidOperationException) - { - } + try { simulator.Keyboard.ModifiedKeyStroke(modifierKeys, keyCodes); } + catch (InvalidOperationException) { } } } } diff --git a/SteamController/Profiles/DesktopProfile.cs b/SteamController/Profiles/DesktopProfile.cs index ab912d0..c83048f 100644 --- a/SteamController/Profiles/DesktopProfile.cs +++ b/SteamController/Profiles/DesktopProfile.cs @@ -75,22 +75,11 @@ namespace SteamController.Profiles private void EmulateDPadArrows(Context c) { -#if true - if (c.Steam.BtnDpadLeft.HoldRepeat(Consumed)) - c.Keyboard.KeyPress(VirtualKeyCode.LEFT); - if (c.Steam.BtnDpadRight.HoldRepeat(Consumed)) - c.Keyboard.KeyPress(VirtualKeyCode.RIGHT); - if (c.Steam.BtnDpadUp.HoldRepeat(Consumed)) - c.Keyboard.KeyPress(VirtualKeyCode.UP); - if (c.Steam.BtnDpadDown.HoldRepeat(Consumed)) - c.Keyboard.KeyPress(VirtualKeyCode.DOWN); -#else c.Keyboard[VirtualKeyCode.RETURN] = c.Steam.BtnA; c.Keyboard[VirtualKeyCode.LEFT] = c.Steam.BtnDpadLeft; c.Keyboard[VirtualKeyCode.RIGHT] = c.Steam.BtnDpadRight; c.Keyboard[VirtualKeyCode.UP] = c.Steam.BtnDpadUp; c.Keyboard[VirtualKeyCode.DOWN] = c.Steam.BtnDpadDown; -#endif } } }