Skip to content

Commit 1ae77a9

Browse files
committed
Adds OnePress function
Adds OneRelease function Adds OneDown function Adds OneUp Function
1 parent 40f6f21 commit 1ae77a9

File tree

3 files changed

+208
-25
lines changed

3 files changed

+208
-25
lines changed

InputStateManager/Inputs/Key.cs

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -81,23 +81,50 @@ internal IsSub(Func<KeyboardState> mapping, Func<KeyboardState> oldMapping) : ba
8181
OldState = oldMapping;
8282
}
8383

84-
public bool Press(Keys key) => State().IsKeyDown(key) && OldState().IsKeyUp(key);
85-
public bool Release(Keys key) => OldState().IsKeyDown(key) && State().IsKeyUp(key);
84+
public bool OnePress(params Keys[] keys)
85+
{
86+
foreach (var key in keys)
87+
if (State().IsKeyDown(key) && OldState().IsKeyUp(key))
88+
return true;
89+
return false;
90+
}
8691

87-
public bool ShiftPress => Press(Keys.LeftShift) || Press(Keys.RightShift);
88-
public bool CtrlPress => Press(Keys.LeftControl) || Press(Keys.RightControl);
89-
public bool AltPress => Press(Keys.LeftAlt) || Press(Keys.RightAlt);
90-
public bool WindowsPress => Press(Keys.LeftWindows) || Press(Keys.RightWindows);
92+
public bool Press(params Keys[] keys)
93+
{
94+
foreach (var key in keys)
95+
if (State().IsKeyUp(key) || OldState().IsKeyDown(key))
96+
return false;
97+
return true;
98+
}
9199

92-
public bool ShiftRelease => Release(Keys.LeftShift) && Release(Keys.RightShift);
93-
public bool CtrlRelease => Release(Keys.LeftControl) && Release(Keys.RightControl);
94-
public bool AltRelease => Release(Keys.LeftAlt) && Release(Keys.RightAlt);
95-
public bool WindowsRelease => Release(Keys.LeftWindows) && Release(Keys.RightWindows);
100+
public bool OneRelease(params Keys[] keys)
101+
{
102+
foreach (var key in keys)
103+
if (OldState().IsKeyDown(key) && State().IsKeyUp(key))
104+
return true;
105+
return false;
106+
}
96107

97-
public bool NumLockPress => State().NumLock && !OldState().NumLock;
98-
public bool NumLockRelease => !State().NumLock && OldState().NumLock;
108+
public bool Release(params Keys[] keys)
109+
{
110+
foreach (var key in keys)
111+
if (OldState().IsKeyUp(key) || State().IsKeyDown(key))
112+
return false;
113+
return true;
114+
}
99115

116+
public bool ShiftPress => OnePress(Keys.LeftShift, Keys.RightShift);
117+
public bool CtrlPress => OnePress(Keys.LeftControl, Keys.RightControl);
118+
public bool AltPress => OnePress(Keys.LeftAlt, Keys.RightAlt);
119+
public bool WindowsPress => OnePress(Keys.LeftWindows, Keys.RightWindows);
120+
public bool NumLockPress => State().NumLock && !OldState().NumLock;
100121
public bool CapsLockPress => State().CapsLock && !OldState().CapsLock;
122+
123+
public bool ShiftRelease => Release(Keys.LeftShift, Keys.RightShift);
124+
public bool CtrlRelease => Release(Keys.LeftControl, Keys.RightControl);
125+
public bool AltRelease => Release(Keys.LeftAlt, Keys.RightAlt);
126+
public bool WindowsRelease => Release(Keys.LeftWindows, Keys.RightWindows);
127+
public bool NumLockRelease => !State().NumLock && OldState().NumLock;
101128
public bool CapsLockRelease => !State().CapsLock && OldState().CapsLock;
102129
}
103130

@@ -111,21 +138,52 @@ internal WasSub(Func<KeyboardState> mapping)
111138
State = mapping;
112139
}
113140

114-
public bool Down(Keys key) => State().IsKeyDown(key);
115-
public bool Up(Keys key) => State().IsKeyUp(key);
116-
117-
public bool ShiftDown => Down(Keys.LeftShift) || Down(Keys.RightShift);
118-
public bool CtrlDown => Down(Keys.LeftControl) || Down(Keys.RightControl);
119-
public bool AltDown => Down(Keys.LeftAlt) || Down(Keys.RightAlt);
120-
public bool WindowsDown => Down(Keys.LeftWindows) || Down(Keys.RightWindows);
121-
public bool ShiftUp => Up(Keys.LeftShift) && Up(Keys.RightShift);
122-
public bool CtrlUp => Up(Keys.LeftControl) && Up(Keys.RightControl);
123-
public bool AltUp => Up(Keys.LeftAlt) && Up(Keys.RightAlt);
124-
public bool WindowsUp => Up(Keys.LeftWindows) && Up(Keys.RightWindows);
125-
public bool NumLockUp => !State().NumLock;
141+
public bool OneDown(params Keys[] keys)
142+
{
143+
foreach (var key in keys)
144+
if (State().IsKeyDown(key))
145+
return true;
146+
return false;
147+
}
148+
149+
public bool Down(params Keys[] keys)
150+
{
151+
foreach (var key in keys)
152+
if (State().IsKeyUp(key))
153+
return false;
154+
return true;
155+
}
156+
157+
public bool OneUp(params Keys[] keys)
158+
{
159+
foreach (var key in keys)
160+
if (State().IsKeyUp(key))
161+
return true;
162+
return false;
163+
}
164+
165+
public bool Up(params Keys[] keys)
166+
{
167+
foreach (var key in keys)
168+
if (State().IsKeyDown(key))
169+
return false;
170+
return true;
171+
}
172+
173+
public bool ShiftDown => OneDown(Keys.LeftShift, Keys.RightShift);
174+
public bool CtrlDown => OneDown(Keys.LeftControl, Keys.RightControl);
175+
public bool AltDown => OneDown(Keys.LeftAlt, Keys.RightAlt);
176+
public bool WindowsDown => OneDown(Keys.LeftWindows, Keys.RightWindows);
126177
public bool NumLockDown => State().NumLock;
127-
public bool CapsLockUp => !State().CapsLock;
128178
public bool CapsLockDown => State().CapsLock;
179+
180+
public bool ShiftUp => Up(Keys.LeftShift, Keys.RightShift);
181+
public bool CtrlUp => Up(Keys.LeftControl, Keys.RightControl);
182+
public bool AltUp => Up(Keys.LeftAlt, Keys.RightAlt);
183+
public bool WindowsUp => Up(Keys.LeftWindows, Keys.RightWindows);
184+
public bool NumLockUp => !State().NumLock;
185+
public bool CapsLockUp => !State().CapsLock;
186+
129187
public Keys[] GetPressedKeys => State().GetPressedKeys();
130188
}
131189
}

NUnitTests/KeyTests.cs

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,24 @@ public void KeyDownTriggers()
5656
Assert.IsTrue(input.Key.Is.Down(Keys.A));
5757
}
5858

59+
[Test]
60+
public void KeyDownMultipleInputsTriggers()
61+
{
62+
providerMock.SetupSequence(o => o.GetState())
63+
.Returns(new KeyboardState(Keys.A, Keys.B));
64+
input.Update();
65+
Assert.IsTrue(input.Key.Is.Down(Keys.A, Keys.B));
66+
}
67+
68+
[Test]
69+
public void KeyOneDownTriggers()
70+
{
71+
providerMock.SetupSequence(o => o.GetState())
72+
.Returns(new KeyboardState(Keys.A));
73+
input.Update();
74+
Assert.IsTrue(input.Key.Is.OneDown(Keys.A, Keys.B));
75+
}
76+
5977
[Test]
6078
public void KeyDownIsResetProperly()
6179
{
@@ -83,6 +101,30 @@ public void KeyUpTriggers()
83101
Assert.IsTrue(input.Key.Is.Up(Keys.A));
84102
}
85103

104+
[Test]
105+
public void KeyUpMultipleInputsTriggers()
106+
{
107+
providerMock.SetupSequence(o => o.GetState())
108+
.Returns(new KeyboardState(Keys.A, Keys.B))
109+
.Returns(new KeyboardState());
110+
input.Update();
111+
Assert.IsFalse(input.Key.Is.Up(Keys.A, Keys.B));
112+
input.Update();
113+
Assert.IsTrue(input.Key.Is.Up(Keys.A, Keys.B));
114+
}
115+
116+
[Test]
117+
public void KeyOneUpTriggers()
118+
{
119+
providerMock.SetupSequence(o => o.GetState())
120+
.Returns(new KeyboardState(Keys.A))
121+
.Returns(new KeyboardState());
122+
input.Update();
123+
Assert.IsTrue(input.Key.Is.OneUp(Keys.A, Keys.B));
124+
input.Update();
125+
Assert.IsTrue(input.Key.Is.OneUp(Keys.A, Keys.B));
126+
}
127+
86128
[Test]
87129
public void KeyUpIsResetProperly()
88130
{
@@ -189,6 +231,34 @@ public void WasUpGivesOldState()
189231
Assert.IsFalse(input.Key.Was.Up(Keys.A));
190232
}
191233

234+
[Test]
235+
public void WasDownGivesOldStateMultipleInputs()
236+
{
237+
providerMock.SetupSequence(o => o.GetState())
238+
.Returns(new KeyboardState(Keys.A, Keys.B))
239+
.Returns(new KeyboardState());
240+
input.Update();
241+
Assert.IsTrue(input.Key.Is.Down(Keys.A, Keys.B));
242+
Assert.IsFalse(input.Key.Was.Down(Keys.A, Keys.B));
243+
input.Update();
244+
Assert.IsFalse(input.Key.Is.Down(Keys.A, Keys.B));
245+
Assert.IsTrue(input.Key.Was.Down(Keys.A, Keys.B));
246+
}
247+
248+
[Test]
249+
public void WasUpGivesOldStateMultipleInputs()
250+
{
251+
providerMock.SetupSequence(o => o.GetState())
252+
.Returns(new KeyboardState(Keys.A, Keys.B))
253+
.Returns(new KeyboardState());
254+
input.Update();
255+
Assert.IsFalse(input.Key.Is.Up(Keys.A, Keys.B));
256+
Assert.IsTrue(input.Key.Was.Up(Keys.A, Keys.B));
257+
input.Update();
258+
Assert.IsTrue(input.Key.Is.Up(Keys.A, Keys.B));
259+
Assert.IsFalse(input.Key.Was.Up(Keys.A, Keys.B));
260+
}
261+
192262
[Test]
193263
public void AltPressIsTriggeredAndReleasedWithLeftAndRightAlt()
194264
{
@@ -288,6 +358,7 @@ public void CapslockPressIsTriggeredAndReleased()
288358
Assert.IsTrue(input.Key.Is.CapsLockDown);
289359
input.Update();
290360
Assert.IsFalse(input.Key.Is.CapsLockPress);
361+
Assert.IsTrue(input.Key.Is.CapsLockRelease);
291362
Assert.IsTrue(input.Key.Is.CapsLockUp);
292363
}
293364

@@ -306,7 +377,47 @@ public void NumlockPressIsTriggeredAndReleased()
306377
Assert.IsTrue(input.Key.Is.NumLockDown);
307378
input.Update();
308379
Assert.IsFalse(input.Key.Is.NumLockPress);
380+
Assert.IsTrue(input.Key.Is.NumLockRelease);
309381
Assert.IsTrue(input.Key.Is.NumLockUp);
310382
}
383+
384+
[Test]
385+
public void PressAndReleaseWorksWithSeveralKeys()
386+
{
387+
providerMock.SetupSequence(o => o.GetState())
388+
.Returns(new KeyboardState(Keys.A, Keys.B))
389+
.Returns(new KeyboardState(Keys.A));
390+
input.Update();
391+
Assert.IsTrue(input.Key.Is.Press(Keys.A, Keys.B));
392+
Assert.IsFalse(input.Key.Is.Release(Keys.A, Keys.B));
393+
input.Update();
394+
Assert.IsFalse(input.Key.Is.Press(Keys.A, Keys.B));
395+
Assert.IsFalse(input.Key.Is.Release(Keys.A, Keys.B));
396+
input.Update();
397+
Assert.IsFalse(input.Key.Is.Press(Keys.A, Keys.B));
398+
// A and B were not released at once.
399+
Assert.IsFalse(input.Key.Is.Release(Keys.A, Keys.B));
400+
}
401+
402+
[Test]
403+
public void OnePressAndOneReleaseWorksWithSeveralKeys()
404+
{
405+
providerMock.SetupSequence(o => o.GetState())
406+
.Returns(new KeyboardState(Keys.A))
407+
.Returns(new KeyboardState(Keys.A, Keys.B))
408+
.Returns(new KeyboardState(Keys.A));
409+
input.Update();
410+
Assert.IsTrue(input.Key.Is.OnePress(Keys.A, Keys.B));
411+
Assert.IsFalse(input.Key.Is.OneRelease(Keys.A, Keys.B));
412+
input.Update();
413+
Assert.IsTrue(input.Key.Is.OnePress(Keys.A, Keys.B));
414+
Assert.IsFalse(input.Key.Is.OneRelease(Keys.A, Keys.B));
415+
input.Update();
416+
Assert.IsFalse(input.Key.Is.OnePress(Keys.A, Keys.B));
417+
Assert.IsTrue(input.Key.Is.OneRelease(Keys.A, Keys.B));
418+
input.Update();
419+
Assert.IsFalse(input.Key.Is.OnePress(Keys.A, Keys.B));
420+
Assert.IsTrue(input.Key.Is.OneRelease(Keys.A, Keys.B));
421+
}
311422
}
312423
}

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,27 @@ if (input.Mouse.Was.Up(Mouse.Button.MIDDLE)) {...}
9494
// Somewhere in your code:
9595
9696
if (input.Key.Is.Press(Keys.Escape)) {...}
97+
// Register keys that are currently held down:
98+
if (input.Key.Is.Down(Keys.A) && input.Key.Is.Down(Keys.B)) {...}
99+
// Same as:
100+
if (input.Key.Is.Down(Keys.A, Keys.B)) {...}
101+
// Or:
102+
if (input.Key.Is.Down(new []{Keys.A, Keys.B})) {...}
103+
// Register key-presses (down -> up):
104+
if (input.Key.Is.Press(Keys.A) || input.Key.Is.Press(Keys.B)) {...}
105+
// Same as:
106+
if (input.Key.Is.OnePress(Keys.A, Keys.B)) {...}
107+
// Or:
108+
if (input.Key.Is.OnePress(new []{Keys.A, Keys.B})) {...}
97109
// Query for any SHIFT-key pressed:
98110
if (input.Key.Is.Down(Keys.A) && input.Key.Is.ShiftDown) {...}
99111
// Same for CTRL and ALT:
100112
if (input.Key.Is.CtrlRelease() || input.Key.Is.AltPress()) {...}
101113
// And NumLock and CapsLock:
102114
if (input.Key.Is.NumLockRelease() || input.Key.Is.CapsLockPress()) {..}
103115
...
116+
117+
104118
```
105119

106120
### GamePad

0 commit comments

Comments
 (0)