Skip to content

Commit b905a9f

Browse files
committed
Helper to raise event for special keyboard keys
1 parent 64dee51 commit b905a9f

4 files changed

Lines changed: 394 additions & 6 deletions

File tree

Lines changed: 253 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
namespace Bunit
2+
{
3+
/// <summary>
4+
/// Special keys that can be dispatched in keyboard events.
5+
/// </summary>
6+
public enum Key
7+
{
8+
/// <summary>
9+
/// No keyboard key pressed. This option raises no event.
10+
/// </summary>
11+
Null = 0,
12+
13+
/// <summary>
14+
/// Represents the Backspace key.
15+
/// </summary>
16+
Backspace = 57347,
17+
18+
/// <summary>
19+
/// Represents the Tab key.
20+
/// </summary>
21+
Tab = 57348,
22+
23+
/// <summary>
24+
/// Represents the Enter key.
25+
/// </summary>
26+
Enter = 57351,
27+
28+
/// <summary>
29+
/// Represents the Pause key.
30+
/// </summary>
31+
Pause = 57355,
32+
33+
/// <summary>
34+
/// Represents the Escape key.
35+
/// </summary>
36+
Escape = 57356,
37+
38+
/// <summary>
39+
/// Represents the Spacebar key.
40+
/// </summary>
41+
Space = 57357,
42+
43+
/// <summary>
44+
/// Represents the Page Up key.
45+
/// </summary>
46+
PageUp = 57358,
47+
48+
/// <summary>
49+
/// Represents the Page Down key.
50+
/// </summary>
51+
PageDown = 57359,
52+
53+
/// <summary>
54+
/// Represents the End key.
55+
/// </summary>
56+
End = 57360,
57+
58+
/// <summary>
59+
/// Represents the Home key.
60+
/// </summary>
61+
Home = 57361,
62+
63+
/// <summary>
64+
/// Represents the left arrow key.
65+
/// </summary>
66+
Left = 57362,
67+
68+
/// <summary>
69+
/// Represents the up arrow key.
70+
/// </summary>
71+
Up = 57363,
72+
73+
/// <summary>
74+
/// Represents the right arrow key.
75+
/// </summary>
76+
Right = 57364,
77+
78+
/// <summary>
79+
/// Represents the down arrow key.
80+
/// </summary>
81+
Down = 57365,
82+
83+
/// <summary>
84+
/// Represents the Insert key.
85+
/// </summary>
86+
Insert = 57366,
87+
88+
/// <summary>
89+
/// Represents the Delete key.
90+
/// </summary>
91+
Delete = 57367,
92+
93+
/// <summary>
94+
/// Represents the equal sign key.
95+
/// </summary>
96+
Equal = 57369,
97+
98+
/// <summary>
99+
/// Represents the number pad 0 key.
100+
/// </summary>
101+
NumberPad0 = 57370,
102+
103+
/// <summary>
104+
/// Represents the number pad 1 key.
105+
/// </summary>
106+
NumberPad1 = 57371,
107+
108+
/// <summary>
109+
/// Represents the number pad 2 key.
110+
/// </summary>
111+
NumberPad2 = 57372,
112+
113+
/// <summary>
114+
/// Represents the number pad 3 key.
115+
/// </summary>
116+
NumberPad3 = 57373,
117+
118+
/// <summary>
119+
/// Represents the number pad 4 key.
120+
/// </summary>
121+
NumberPad4 = 57374,
122+
123+
/// <summary>
124+
/// Represents the number pad 5 key.
125+
/// </summary>
126+
NumberPad5 = 57375,
127+
128+
/// <summary>
129+
/// Represents the number pad 6 key.
130+
/// </summary>
131+
NumberPad6 = 57376,
132+
133+
/// <summary>
134+
/// Represents the number pad 7 key.
135+
/// </summary>
136+
NumberPad7 = 57377,
137+
138+
/// <summary>
139+
/// Represents the number pad 8 key.
140+
/// </summary>
141+
NumberPad8 = 57378,
142+
143+
/// <summary>
144+
/// Represents the number pad 9 key.
145+
/// </summary>
146+
NumberPad9 = 57379,
147+
148+
/// <summary>
149+
/// Represents the number pad multiplication key.
150+
/// </summary>
151+
Multiply = 57380,
152+
153+
/// <summary>
154+
/// Represents the number pad addition key.
155+
/// </summary>
156+
Add = 57381,
157+
158+
/// <summary>
159+
/// Represents the number pad subtraction key.
160+
/// </summary>
161+
Subtract = 57383,
162+
163+
/// <summary>
164+
/// Represents the number pad decimal separator key.
165+
/// </summary>
166+
NumberPadDecimal = 57384,
167+
168+
/// <summary>
169+
/// Represents the number pad division key.
170+
/// </summary>
171+
Divide = 57385,
172+
173+
/// <summary>
174+
/// Represents the function key F1.
175+
/// </summary>
176+
F1 = 57393,
177+
178+
/// <summary>
179+
/// Represents the function key F2.
180+
/// </summary>
181+
F2 = 57394,
182+
183+
/// <summary>
184+
/// Represents the function key F3.
185+
/// </summary>
186+
F3 = 57395,
187+
188+
/// <summary>
189+
/// Represents the function key F4.
190+
/// </summary>
191+
F4 = 57396,
192+
193+
/// <summary>
194+
/// Represents the function key F5.
195+
/// </summary>
196+
F5 = 57397,
197+
198+
/// <summary>
199+
/// Represents the function key F6.
200+
/// </summary>
201+
F6 = 57398,
202+
203+
/// <summary>
204+
/// Represents the function key F7.
205+
/// </summary>
206+
F7 = 57399,
207+
208+
/// <summary>
209+
/// Represents the function key F8.
210+
/// </summary>
211+
F8 = 57400,
212+
213+
/// <summary>
214+
/// Represents the function key F9.
215+
/// </summary>
216+
F9 = 57401,
217+
218+
/// <summary>
219+
/// Represents the function key F10.
220+
/// </summary>
221+
F10 = 57402,
222+
223+
/// <summary>
224+
/// Represents the function key F11.
225+
/// </summary>
226+
F11 = 57403,
227+
228+
/// <summary>
229+
/// Represents the function key F12.
230+
/// </summary>
231+
F12 = 57404,
232+
233+
/// <summary>
234+
/// Represents the Shift key. This is a control key and it can be combined with other keys. E.g. Keys.Enter | Keys.Shift
235+
/// </summary>
236+
Shift = 1 << 16,
237+
238+
/// <summary>
239+
/// Represents the Control key. This is a control key and it can be combined with other keys. E.g. Keys.Enter | Keys.Control
240+
/// </summary>
241+
Control = 1 << 17,
242+
243+
/// <summary>
244+
/// Represents the Alt key. This is a control key and it can be combined with other keys. E.g. Keys.Enter | Keys.Alt
245+
/// </summary>
246+
Alt = 1 << 18,
247+
248+
/// <summary>
249+
/// Represents the function key Command. This is a control key and it can be combined with other keys. E.g. Keys.Enter | Keys.Command
250+
/// </summary>
251+
Command = 1 << 19
252+
}
253+
}

src/bunit.web/EventDispatchExtensions/KeyboardEventDispatchExtensions.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ public static void KeyDown(this IElement element, string key, string? code = def
4343
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
4444
public static void KeyDown(this IElement element, KeyboardEventArgs eventArgs) => _ = KeyDownAsync(element, eventArgs);
4545

46+
/// <summary>
47+
/// Raises the <c>@onkeydown</c> event on <paramref name="element"/>, passing the provided keyboard <paramref name="key"/>.
48+
/// </summary>
49+
/// <param name="element">The element to raise the event on.</param>
50+
/// <param name="key">The keyboard key to raise the event for.</param>
51+
public static void KeyDown(this IElement element, Key key)
52+
{
53+
var eventArgs = KeysEventFactory.CreateKeyboardEventArgs(key);
54+
if (eventArgs != null)
55+
{
56+
KeyDown(element, eventArgs);
57+
}
58+
}
59+
4660
/// <summary>
4761
/// Raises the <c>@onkeydown</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
4862
/// to the event handler.
@@ -86,6 +100,20 @@ public static void KeyUp(this IElement element, string key, string? code = defau
86100
/// <param name="eventArgs">The event arguments to pass to the event handler.</param>
87101
public static void KeyUp(this IElement element, KeyboardEventArgs eventArgs) => _ = KeyUpAsync(element, eventArgs);
88102

103+
/// <summary>
104+
/// Raises the <c>@onkeyup</c> event on <paramref name="element"/>, passing the provided keyboard <paramref name="key"/>.
105+
/// </summary>
106+
/// <param name="element">The element to raise the event on.</param>
107+
/// <param name="key">The keyboard key to raise the event for.</param>
108+
public static void KeyUp(this IElement element, Key key)
109+
{
110+
var eventArgs = KeysEventFactory.CreateKeyboardEventArgs(key);
111+
if (eventArgs != null)
112+
{
113+
KeyUp(element, eventArgs);
114+
}
115+
}
116+
89117
/// <summary>
90118
/// Raises the <c>@onkeyup</c> event on <paramref name="element"/>, passing the provided <paramref name="eventArgs"/>
91119
/// to the event handler.

0 commit comments

Comments
 (0)