Skip to content

Commit 3250572

Browse files
Add a way to simulate multiple events together
1 parent f4ebd63 commit 3250572

13 files changed

+1553
-156
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
namespace SharpHook;
2+
3+
/// <summary>
4+
/// Contains extension methods for <see cref="IEventSimulator" /> and <see cref="IEventSimulationSequenceBuilder" />.
5+
/// </summary>
6+
public static class EventSimulationExtensions
7+
{
8+
/// <summary>
9+
/// Adds a sequecne of key press and release events which represent a single key stroke to the sequence of events to
10+
/// simulate.
11+
/// </summary>
12+
/// <param name="builder">The event simulation sequence builder.</param>
13+
/// <param name="keyCodes">The codes of the keys to press and release.</param>
14+
/// <returns>The builder.</returns>
15+
/// <remarks>
16+
/// As an example, if the method is called with the following parameters:
17+
/// <code>
18+
/// builder.AddKeyStroke(KeyCode.VcLeftControl, KeyCode.VcC);
19+
/// </code>
20+
/// then this will be equivalent to calling the following method sequence:
21+
/// <code>
22+
/// builder
23+
/// .AddKeyPress(KeyCode.VcLeftControl)
24+
/// .AddKeyPress(KeyCode.VcC)
25+
/// .AddKeyRelease(KeyCode.VcC)
26+
/// .AddKeyRelease(KeyCode.VcLeftControl);
27+
/// </code>
28+
/// which means that this method will add pressing the left control, then pressing C, then releasing C, then
29+
/// releasing the left control.
30+
/// </remarks>
31+
public static IEventSimulationSequenceBuilder AddKeyStroke(
32+
this IEventSimulationSequenceBuilder builder,
33+
params KeyCode[] keyCodes)
34+
{
35+
foreach (var keyCode in keyCodes)
36+
{
37+
builder.AddKeyPress(keyCode);
38+
}
39+
40+
foreach (var keyCode in keyCodes.Reverse())
41+
{
42+
builder.AddKeyRelease(keyCode);
43+
}
44+
45+
return builder;
46+
}
47+
48+
/// <summary>
49+
/// Adds a sequence of key press and release events which represent a single key stroke to the sequence of events to
50+
/// simulate.
51+
/// </summary>
52+
/// <param name="builder">The event simulation sequence builder.</param>
53+
/// <param name="keyCodes">The codes of the keys to press and release.</param>
54+
/// <returns>The builder.</returns>
55+
/// <remarks>
56+
/// As an example, if the method is called with the following parameters:
57+
/// <code>
58+
/// builder.AddKeyStroke(KeyCode.VcLeftControl, KeyCode.VcC);
59+
/// </code>
60+
/// then this will be equivalent to calling the following method sequence:
61+
/// <code>
62+
/// builder
63+
/// .AddKeyPress(KeyCode.VcLeftControl)
64+
/// .AddKeyPress(KeyCode.VcC)
65+
/// .AddKeyRelease(KeyCode.VcC)
66+
/// .AddKeyRelease(KeyCode.VcLeftControl);
67+
/// </code>
68+
/// which means that this method will add pressing the left control, then pressing C, then releasing C, then
69+
/// releasing the left control.
70+
/// </remarks>
71+
public static IEventSimulationSequenceBuilder AddKeyStroke(
72+
this IEventSimulationSequenceBuilder builder,
73+
IEnumerable<KeyCode> keyCodes) =>
74+
builder.AddKeyStroke([.. keyCodes]);
75+
76+
/// <summary>
77+
/// Simulates a sequence of key press and release events which represent a single key stroke.
78+
/// </summary>
79+
/// <param name="simulator">The event simulator.</param>
80+
/// <param name="keyCodes">The codes of the keys to press and release.</param>
81+
/// <returns>The result of the operation.</returns>
82+
/// <remarks>
83+
/// As an example, if the method is called with the following parameters:
84+
/// <code>
85+
/// simulator.SimulateKeyStroke(KeyCode.VcLeftControl, KeyCode.VcC);
86+
/// </code>
87+
/// then this will be equivalent to calling the following method sequence:
88+
/// <code>
89+
/// simulator.Sequence()
90+
/// .AddKeyPress(KeyCode.VcLeftControl)
91+
/// .AddKeyPress(KeyCode.VcC)
92+
/// .AddKeyRelease(KeyCode.VcC)
93+
/// .AddKeyRelease(KeyCode.VcLeftControl)
94+
/// .Simulate();
95+
/// </code>
96+
/// which means that this method will simualte pressing the left control, then pressing C, then releasing C, then
97+
/// releasing the left control.
98+
/// </remarks>
99+
public static UioHookResult SimulateKeyStroke(this IEventSimulator simulator, params KeyCode[] keyCodes) =>
100+
simulator.Sequence()
101+
.AddKeyStroke(keyCodes)
102+
.Simulate();
103+
104+
/// <summary>
105+
/// Simulates a sequence of key press and release events which represent a single key stroke.
106+
/// </summary>
107+
/// <param name="simulator">The event simulator.</param>
108+
/// <param name="keyCodes">The codes of the keys to press and release.</param>
109+
/// <returns>The result of the operation.</returns>
110+
/// <remarks>
111+
/// As an example, if the method is called with the following parameters:
112+
/// <code>
113+
/// simulator.SimulateKeyStroke(KeyCode.VcLeftControl, KeyCode.VcC);
114+
/// </code>
115+
/// then this will be equivalent to calling the following method sequence:
116+
/// <code>
117+
/// simulator.Sequence()
118+
/// .AddKeyPress(KeyCode.VcLeftControl)
119+
/// .AddKeyPress(KeyCode.VcC)
120+
/// .AddKeyRelease(KeyCode.VcC)
121+
/// .AddKeyRelease(KeyCode.VcLeftControl)
122+
/// .Simulate();
123+
/// </code>
124+
/// which means that this method will simualte pressing the left control, then pressing C, then releasing C, then
125+
/// releasing the left control.
126+
/// </remarks>
127+
public static UioHookResult SimulateKeyStroke(this IEventSimulator simulator, IEnumerable<KeyCode> keyCodes) =>
128+
simulator.Sequence()
129+
.AddKeyStroke(keyCodes)
130+
.Simulate();
131+
}

0 commit comments

Comments
 (0)