mirror of
https://github.com/ayufan/steam-deck-tools.git
synced 2026-01-03 07:09:58 +01:00
Make KeyboardController to generate KeyRepeats
This commit is contained in:
parent
6da5a9796c
commit
aafe040e12
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -1,13 +1,17 @@
|
|||
using WindowsInput;
|
||||
using KeyRepeats = System.Tuple<System.DateTime, int>;
|
||||
|
||||
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<VirtualKeyCode> keyCodes = new HashSet<VirtualKeyCode>();
|
||||
HashSet<VirtualKeyCode> lastKeyCodes = new HashSet<VirtualKeyCode>();
|
||||
Dictionary<VirtualKeyCode, KeyRepeats> keyCodes = new Dictionary<VirtualKeyCode, KeyRepeats>();
|
||||
Dictionary<VirtualKeyCode, KeyRepeats> lastKeyCodes = new Dictionary<VirtualKeyCode, KeyRepeats>();
|
||||
|
||||
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<VirtualKeyCode>();
|
||||
keyCodes = new Dictionary<VirtualKeyCode, KeyRepeats>();
|
||||
}
|
||||
|
||||
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<VirtualKeyCode> modifierKeys, params VirtualKeyCode[] keyCodes)
|
||||
{
|
||||
try
|
||||
{
|
||||
simulator.Keyboard.ModifiedKeyStroke(modifierKeys, keyCodes);
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
}
|
||||
try { simulator.Keyboard.ModifiedKeyStroke(modifierKeys, keyCodes); }
|
||||
catch (InvalidOperationException) { }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue