Class InputInjector
Provides an API for injecting mouse and keyboard input into Terminal.Gui applications.
public class InputInjector : IInputInjector
- Inheritance
-
InputInjector
- Implements
- Inherited Members
Examples
Example 1: Basic key injection (using extension method)
using IApplication app = Application.Create();
app.Init(DriverRegistry.Names.ANSI);
app.Keyboard.KeyDown += (s, e) => {
Console.WriteLine($"Key pressed: {e}");
};
// Simple injection using extension method
app.InjectKey(Key.A);
app.InjectKey(Key.Enter);
Example 2: Mouse injection with Direct mode (preserving timestamps)
using IApplication app = Application.Create();
app.Init(DriverRegistry.Names.ANSI);
IInputInjector injector = app.GetInputInjector();
InputInjectionOptions options = new() { Mode = InputInjectionMode.Direct };
DateTime baseTime = new(2025, 1, 1, 12, 0, 0);
// First click
injector.InjectMouse(new() {
ScreenPosition = new(10, 10),
Flags = MouseFlags.LeftButtonPressed,
Timestamp = baseTime
}, options);
injector.InjectMouse(new() {
ScreenPosition = new(10, 10),
Flags = MouseFlags.LeftButtonReleased,
Timestamp = baseTime.AddMilliseconds(50)
}, options);
// Second click 600ms later (prevents double-click detection)
injector.InjectMouse(new() {
ScreenPosition = new(10, 10),
Flags = MouseFlags.LeftButtonPressed,
Timestamp = baseTime.AddMilliseconds(600)
}, options);
injector.InjectMouse(new() {
ScreenPosition = new(10, 10),
Flags = MouseFlags.LeftButtonReleased,
Timestamp = baseTime.AddMilliseconds(650)
}, options);
Example 3: Event sequence with virtual time
VirtualTimeProvider timeProvider = new();
using IApplication app = Application.Create(timeProvider);
app.Init(DriverRegistry.Names.ANSI);
// Inject sequence with delays (virtual time advances instantly)
InputEvent[] sequence = [
new KeyInjectionEvent(Key.H),
new KeyInjectionEvent(Key.E) { Delay = TimeSpan.FromMilliseconds(100) },
new KeyInjectionEvent(Key.L) { Delay = TimeSpan.FromMilliseconds(100) },
new KeyInjectionEvent(Key.L) { Delay = TimeSpan.FromMilliseconds(100) },
new KeyInjectionEvent(Key.O)
];
app.InjectSequence(sequence);
// Virtual time has advanced by 300ms, but test executes instantly
Example 4: Testing ANSI encoding with Pipeline mode
using IApplication app = Application.Create();
app.Init(DriverRegistry.Names.ANSI);
IInputInjector injector = app.GetInputInjector();
// Use Pipeline mode to test encoding/decoding
InputInjectionOptions options = new() { Mode = InputInjectionMode.Pipeline };
// Key will go through: Key → ANSI sequence → Parser → Key event
injector.InjectKey(Key.F1, options);
injector.InjectKey(Key.CursorUp.WithCtrl, options);
Remarks
InputInjector offers two injection modes to support different testing scenarios:
- Direct Mode (default): Bypasses ANSI encoding/decoding and directly raises events. Ideal for fast, deterministic tests that don't need to verify encoding/parsing behavior. Use this mode when you want precise control over timestamps and event timing.
- Pipeline Mode: Routes input through the complete processing pipeline including ANSI encoding/decoding. Use this mode when testing driver-level behavior or verifying that escape sequences are correctly encoded and parsed.
For convenience, use the extension methods in InputInjectionExtensions: InjectKey(IApplication, Key), InjectMouse(IApplication, Mouse), and InjectSequence(IApplication, params InputInjectionEvent[]) to inject input without directly accessing the injector.
When you need to specify injection options (such as Direct mode with timestamps), use GetInputInjector() to access the injector directly.
Constructors
- InputInjector(IInputProcessor, ITimeProvider, TestInputSource?)
Initializes a new instance of the InputInjector class.
Methods
- InjectKey(Key, InputInjectionOptions?)
Injects a keyboard event. Handles encoding, queueing, and processing automatically.
- InjectMouse(Mouse, InputInjectionOptions?)
Injects a mouse event. Handles encoding, queueing, and processing automatically.
- InjectSequence(IEnumerable<InputInjectionEvent>, InputInjectionOptions?)
Injects a sequence of input events with delays between them.
- ProcessQueue()
Forces processing of the input queue (usually automatic).