Skip to content

Commit 9d091d5

Browse files
committed
Adds tests
Minor API changes for CapsLock and NumLock Adds windows-keys accessors
1 parent b5db4e0 commit 9d091d5

File tree

2 files changed

+245
-39
lines changed

2 files changed

+245
-39
lines changed

InputStateManager/Inputs/Key.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ internal IsSub(Func<KeyboardState> mapping, Func<KeyboardState> oldMapping) : ba
8787
public bool ShiftPress => Press(Keys.LeftShift) || Press(Keys.RightShift);
8888
public bool CtrlPress => Press(Keys.LeftControl) || Press(Keys.RightControl);
8989
public bool AltPress => Press(Keys.LeftAlt) || Press(Keys.RightAlt);
90+
public bool WindowsPress => Press(Keys.LeftWindows) || Press(Keys.RightWindows);
9091

9192
public bool ShiftRelease => Release(Keys.LeftShift) && Release(Keys.RightShift);
9293
public bool CtrlRelease => Release(Keys.LeftControl) && Release(Keys.RightControl);
9394
public bool AltRelease => Release(Keys.LeftAlt) && Release(Keys.RightAlt);
95+
public bool WindowsRelease => Release(Keys.LeftWindows) && Release(Keys.RightWindows);
9496

9597
public bool NumLockPress => State().NumLock && !OldState().NumLock;
9698
public bool NumLockRelease => !State().NumLock && OldState().NumLock;
@@ -115,11 +117,15 @@ internal WasSub(Func<KeyboardState> mapping)
115117
public bool ShiftDown => Down(Keys.LeftShift) || Down(Keys.RightShift);
116118
public bool CtrlDown => Down(Keys.LeftControl) || Down(Keys.RightControl);
117119
public bool AltDown => Down(Keys.LeftAlt) || Down(Keys.RightAlt);
120+
public bool WindowsDown => Down(Keys.LeftWindows) || Down(Keys.RightWindows);
118121
public bool ShiftUp => Up(Keys.LeftShift) && Up(Keys.RightShift);
119122
public bool CtrlUp => Up(Keys.LeftControl) && Up(Keys.RightControl);
120123
public bool AltUp => Up(Keys.LeftAlt) && Up(Keys.RightAlt);
121-
public bool NumLock => State().NumLock;
122-
public bool CapsLock => State().CapsLock;
124+
public bool WindowsUp => Up(Keys.LeftWindows) && Up(Keys.RightWindows);
125+
public bool NumLockUp => !State().NumLock;
126+
public bool NumLockDown => State().NumLock;
127+
public bool CapsLockUp => !State().CapsLock;
128+
public bool CapsLockDown => State().CapsLock;
123129
public Keys[] GetPressedKeys => State().GetPressedKeys();
124130
}
125131
}

NUnitTests/KeyTests.cs

Lines changed: 237 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -48,65 +48,265 @@ public void Setup()
4848
}
4949

5050
[Test]
51-
public void KeyDownPressAndReleaseWorkAndAreInSquence()
51+
public void KeyDownTriggers()
52+
{
53+
providerMock.SetupSequence(o => o.GetState())
54+
.Returns(new KeyboardState(Keys.A));
55+
input.Update();
56+
Assert.IsTrue(input.Key.Is.Down(Keys.A));
57+
}
58+
59+
[Test]
60+
public void KeyDownIsResetProperly()
5261
{
5362
providerMock.SetupSequence(o => o.GetState())
5463
.Returns(new KeyboardState(Keys.A))
55-
.Returns(new KeyboardState(Keys.B))
56-
.Returns(new KeyboardState(Keys.C))
57-
.Returns(new KeyboardState(Keys.C));
58-
64+
.Returns(new KeyboardState(Keys.A))
65+
.Returns(new KeyboardState());
66+
input.Update();
67+
Assert.IsTrue(input.Key.Is.Down(Keys.A));
5968
input.Update();
6069
Assert.IsTrue(input.Key.Is.Down(Keys.A));
61-
Assert.IsFalse(input.Key.Is.Down(Keys.B));
62-
Assert.IsFalse(input.Key.Is.Down(Keys.C));
70+
input.Update();
71+
Assert.IsFalse(input.Key.Is.Down(Keys.A));
72+
}
6373

64-
Assert.IsTrue(input.Key.Is.Press(Keys.A));
65-
Assert.IsFalse(input.Key.Is.Press(Keys.B));
66-
Assert.IsFalse(input.Key.Is.Press(Keys.C));
74+
[Test]
75+
public void KeyUpTriggers()
76+
{
77+
providerMock.SetupSequence(o => o.GetState())
78+
.Returns(new KeyboardState(Keys.A))
79+
.Returns(new KeyboardState());
80+
input.Update();
81+
Assert.IsFalse(input.Key.Is.Up(Keys.A));
82+
input.Update();
83+
Assert.IsTrue(input.Key.Is.Up(Keys.A));
84+
}
6785

68-
Assert.IsFalse(input.Key.Is.Release(Keys.A));
69-
Assert.IsFalse(input.Key.Is.Release(Keys.B));
70-
Assert.IsFalse(input.Key.Is.Release(Keys.C));
86+
[Test]
87+
public void KeyUpIsResetProperly()
88+
{
89+
providerMock.SetupSequence(o => o.GetState())
90+
.Returns(new KeyboardState(Keys.A))
91+
.Returns(new KeyboardState())
92+
.Returns(new KeyboardState(Keys.A));
93+
input.Update();
94+
Assert.IsFalse(input.Key.Is.Up(Keys.A));
95+
input.Update();
96+
Assert.IsTrue(input.Key.Is.Up(Keys.A));
97+
input.Update();
98+
Assert.IsFalse(input.Key.Is.Up(Keys.A));
99+
}
71100

101+
[Test]
102+
public void KeyPressTriggers()
103+
{
104+
providerMock.SetupSequence(o => o.GetState())
105+
.Returns(new KeyboardState(Keys.A));
72106
input.Update();
73-
Assert.IsFalse(input.Key.Is.Down(Keys.A));
74-
Assert.IsTrue(input.Key.Is.Down(Keys.B));
75-
Assert.IsFalse(input.Key.Is.Down(Keys.C));
107+
Assert.IsTrue(input.Key.Is.Press(Keys.A));
108+
}
76109

110+
[Test]
111+
public void KeyPressIsResetWhenKeyIsReleased()
112+
{
113+
providerMock.SetupSequence(o => o.GetState())
114+
.Returns(new KeyboardState(Keys.A))
115+
.Returns(new KeyboardState());
116+
input.Update();
117+
Assert.IsTrue(input.Key.Is.Press(Keys.A));
118+
input.Update();
119+
Assert.IsFalse(input.Key.Is.Press(Keys.A));
120+
}
121+
122+
[Test]
123+
public void KeyPressIsResetWhenKeyIsStillDown()
124+
{
125+
providerMock.SetupSequence(o => o.GetState())
126+
.Returns(new KeyboardState(Keys.A))
127+
.Returns(new KeyboardState(Keys.A));
128+
input.Update();
129+
Assert.IsTrue(input.Key.Is.Press(Keys.A));
130+
input.Update();
77131
Assert.IsFalse(input.Key.Is.Press(Keys.A));
78-
Assert.IsTrue(input.Key.Is.Press(Keys.B));
79-
Assert.IsFalse(input.Key.Is.Press(Keys.C));
132+
}
80133

134+
[Test]
135+
public void KeyReleaseTriggers()
136+
{
137+
providerMock.SetupSequence(o => o.GetState())
138+
.Returns(new KeyboardState(Keys.A))
139+
.Returns(new KeyboardState(Keys.A))
140+
.Returns(new KeyboardState());
141+
input.Update();
142+
Assert.IsFalse(input.Key.Is.Release(Keys.A));
143+
input.Update();
144+
Assert.IsFalse(input.Key.Is.Release(Keys.A));
145+
input.Update();
146+
Assert.IsTrue(input.Key.Is.Release(Keys.A));
147+
}
148+
149+
[Test]
150+
public void KeyReleaseIsResetProperly()
151+
{
152+
providerMock.SetupSequence(o => o.GetState())
153+
.Returns(new KeyboardState(Keys.A))
154+
.Returns(new KeyboardState())
155+
.Returns(new KeyboardState());
156+
input.Update();
157+
Assert.IsFalse(input.Key.Is.Release(Keys.A));
158+
input.Update();
81159
Assert.IsTrue(input.Key.Is.Release(Keys.A));
82-
Assert.IsFalse(input.Key.Is.Release(Keys.B));
83-
Assert.IsFalse(input.Key.Is.Release(Keys.C));
160+
input.Update();
161+
Assert.IsFalse(input.Key.Is.Press(Keys.A));
162+
}
84163

164+
[Test]
165+
public void WasDownGivesOldState()
166+
{
167+
providerMock.SetupSequence(o => o.GetState())
168+
.Returns(new KeyboardState(Keys.A))
169+
.Returns(new KeyboardState());
170+
input.Update();
171+
Assert.IsTrue(input.Key.Is.Down(Keys.A));
172+
Assert.IsFalse(input.Key.Was.Down(Keys.A));
85173
input.Update();
86174
Assert.IsFalse(input.Key.Is.Down(Keys.A));
87-
Assert.IsFalse(input.Key.Is.Down(Keys.B));
88-
Assert.IsTrue(input.Key.Is.Down(Keys.C));
175+
Assert.IsTrue(input.Key.Was.Down(Keys.A));
176+
}
89177

90-
Assert.IsFalse(input.Key.Is.Press(Keys.A));
91-
Assert.IsFalse(input.Key.Is.Press(Keys.B));
92-
Assert.IsTrue(input.Key.Is.Press(Keys.C));
178+
[Test]
179+
public void WasUpGivesOldState()
180+
{
181+
providerMock.SetupSequence(o => o.GetState())
182+
.Returns(new KeyboardState(Keys.A))
183+
.Returns(new KeyboardState());
184+
input.Update();
185+
Assert.IsFalse(input.Key.Is.Up(Keys.A));
186+
Assert.IsTrue(input.Key.Was.Up(Keys.A));
187+
input.Update();
188+
Assert.IsTrue(input.Key.Is.Up(Keys.A));
189+
Assert.IsFalse(input.Key.Was.Up(Keys.A));
190+
}
93191

94-
Assert.IsFalse(input.Key.Is.Release(Keys.A));
95-
Assert.IsTrue(input.Key.Is.Release(Keys.B));
96-
Assert.IsFalse(input.Key.Is.Release(Keys.C));
192+
[Test]
193+
public void AltPressIsTriggeredAndReleasedWithLeftAndRightAlt()
194+
{
195+
providerMock.SetupSequence(o => o.GetState())
196+
.Returns(new KeyboardState(Keys.LeftAlt))
197+
.Returns(new KeyboardState(Keys.RightAlt))
198+
.Returns(new KeyboardState(Keys.LeftAlt, Keys.RightAlt))
199+
.Returns(new KeyboardState());
200+
input.Update();
201+
Assert.IsTrue(input.Key.Is.AltPress);
202+
Assert.IsTrue(input.Key.Is.AltDown);
203+
input.Update();
204+
Assert.IsTrue(input.Key.Is.AltPress);
205+
input.Update();
206+
Assert.IsTrue(input.Key.Is.AltPress);
207+
input.Update();
208+
Assert.IsFalse(input.Key.Is.AltPress);
209+
Assert.IsTrue(input.Key.Is.AltRelease);
210+
Assert.IsTrue(input.Key.Is.AltUp);
211+
}
97212

213+
[Test]
214+
public void CtrlPressIsTriggeredAndReleasedWithLeftAndRightCtrl()
215+
{
216+
providerMock.SetupSequence(o => o.GetState())
217+
.Returns(new KeyboardState(Keys.LeftControl))
218+
.Returns(new KeyboardState(Keys.RightControl))
219+
.Returns(new KeyboardState(Keys.LeftControl, Keys.RightControl))
220+
.Returns(new KeyboardState());
98221
input.Update();
99-
Assert.IsFalse(input.Key.Is.Down(Keys.A));
100-
Assert.IsFalse(input.Key.Is.Down(Keys.B));
101-
Assert.IsTrue(input.Key.Is.Down(Keys.C));
222+
Assert.IsTrue(input.Key.Is.CtrlPress);
223+
Assert.IsTrue(input.Key.Is.CtrlDown);
224+
input.Update();
225+
Assert.IsTrue(input.Key.Is.CtrlPress);
226+
input.Update();
227+
Assert.IsTrue(input.Key.Is.CtrlPress);
228+
input.Update();
229+
Assert.IsFalse(input.Key.Is.CtrlPress);
230+
Assert.IsTrue(input.Key.Is.CtrlRelease);
231+
Assert.IsTrue(input.Key.Is.CtrlUp);
232+
}
102233

103-
Assert.IsFalse(input.Key.Is.Press(Keys.A));
104-
Assert.IsFalse(input.Key.Is.Press(Keys.B));
105-
Assert.IsFalse(input.Key.Is.Press(Keys.C));
234+
[Test]
235+
public void ShiftPressIsTriggeredAndReleasedWithLeftAndRightShift()
236+
{
237+
providerMock.SetupSequence(o => o.GetState())
238+
.Returns(new KeyboardState(Keys.LeftShift))
239+
.Returns(new KeyboardState(Keys.RightShift))
240+
.Returns(new KeyboardState(Keys.LeftShift, Keys.RightShift))
241+
.Returns(new KeyboardState());
242+
input.Update();
243+
Assert.IsTrue(input.Key.Is.ShiftPress);
244+
Assert.IsTrue(input.Key.Is.ShiftDown);
245+
input.Update();
246+
Assert.IsTrue(input.Key.Is.ShiftPress);
247+
input.Update();
248+
Assert.IsTrue(input.Key.Is.ShiftPress);
249+
input.Update();
250+
Assert.IsFalse(input.Key.Is.ShiftPress);
251+
Assert.IsTrue(input.Key.Is.ShiftRelease);
252+
Assert.IsTrue(input.Key.Is.ShiftUp);
253+
}
106254

107-
Assert.IsFalse(input.Key.Is.Release(Keys.A));
108-
Assert.IsFalse(input.Key.Is.Release(Keys.B));
109-
Assert.IsFalse(input.Key.Is.Release(Keys.C));
255+
[Test]
256+
public void WindowsPressIsTriggeredAndReleasedWithLeftAndRightWindows()
257+
{
258+
providerMock.SetupSequence(o => o.GetState())
259+
.Returns(new KeyboardState(Keys.LeftWindows))
260+
.Returns(new KeyboardState(Keys.RightWindows))
261+
.Returns(new KeyboardState(Keys.LeftWindows, Keys.RightWindows))
262+
.Returns(new KeyboardState());
263+
input.Update();
264+
Assert.IsTrue(input.Key.Is.WindowsPress);
265+
Assert.IsTrue(input.Key.Is.WindowsDown);
266+
input.Update();
267+
Assert.IsTrue(input.Key.Is.WindowsPress);
268+
input.Update();
269+
Assert.IsTrue(input.Key.Is.WindowsPress);
270+
input.Update();
271+
Assert.IsFalse(input.Key.Is.WindowsPress);
272+
Assert.IsTrue(input.Key.Is.WindowsRelease);
273+
Assert.IsTrue(input.Key.Is.WindowsUp);
274+
}
275+
276+
[Test]
277+
public void CapslockPressIsTriggeredAndReleased()
278+
{
279+
providerMock.SetupSequence(o => o.GetState())
280+
.Returns(new KeyboardState(new Keys[] { }, true))
281+
.Returns(new KeyboardState(new Keys[] { }, true))
282+
.Returns(new KeyboardState());
283+
input.Update();
284+
Assert.IsTrue(input.Key.Is.CapsLockPress);
285+
Assert.IsTrue(input.Key.Is.CapsLockDown);
286+
input.Update();
287+
Assert.IsFalse(input.Key.Is.CapsLockPress);
288+
Assert.IsTrue(input.Key.Is.CapsLockDown);
289+
input.Update();
290+
Assert.IsFalse(input.Key.Is.CapsLockPress);
291+
Assert.IsTrue(input.Key.Is.CapsLockUp);
292+
}
293+
294+
[Test]
295+
public void NumlockPressIsTriggeredAndReleased()
296+
{
297+
providerMock.SetupSequence(o => o.GetState())
298+
.Returns(new KeyboardState(new Keys[] { }, false, true))
299+
.Returns(new KeyboardState(new Keys[] { }, false, true))
300+
.Returns(new KeyboardState());
301+
input.Update();
302+
Assert.IsTrue(input.Key.Is.NumLockPress);
303+
Assert.IsTrue(input.Key.Is.NumLockDown);
304+
input.Update();
305+
Assert.IsFalse(input.Key.Is.NumLockPress);
306+
Assert.IsTrue(input.Key.Is.NumLockDown);
307+
input.Update();
308+
Assert.IsFalse(input.Key.Is.NumLockPress);
309+
Assert.IsTrue(input.Key.Is.NumLockUp);
110310
}
111311
}
112312
}

0 commit comments

Comments
 (0)