Table of Contents

Class InputStateService

Namespace
SharpConsoleUI.Core
Assembly
SharpConsoleUI.dll

Centralized service for managing input state. Provides input queue management, modifier tracking, and input history.

public class InputStateService : IDisposable
Inheritance
InputStateService
Implements
Inherited Members

Properties

HasPendingInput

Gets whether there are pending keys in the queue

public bool HasPendingInput { get; }

Property Value

bool

IsIdle

Gets whether the system is currently idle (no recent input)

public bool IsIdle { get; }

Property Value

bool

LastKeyTime

Gets the time of the last key press

public DateTime LastKeyTime { get; }

Property Value

DateTime

LastModifiers

Gets the last known modifier state

public ConsoleModifiers LastModifiers { get; }

Property Value

ConsoleModifiers

PendingInputCount

Gets the number of pending keys in the queue

public int PendingInputCount { get; }

Property Value

int

TimeSinceLastKey

Gets the time since the last key press

public TimeSpan TimeSinceLastKey { get; }

Property Value

TimeSpan

WasAltHeld

Checks if Alt modifier was held in last key press

public bool WasAltHeld { get; }

Property Value

bool

WasControlHeld

Checks if Control modifier was held in last key press

public bool WasControlHeld { get; }

Property Value

bool

WasShiftHeld

Checks if Shift modifier was held in last key press

public bool WasShiftHeld { get; }

Property Value

bool

Methods

ClearHistory()

Clears the input history

public void ClearHistory()

ClearQueue()

Clears all pending input

public void ClearQueue()

DequeueKey()

Dequeues a key from the queue

public ConsoleKeyInfo? DequeueKey()

Returns

ConsoleKeyInfo?

The key, or null if queue is empty

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

EnqueueKey(ConsoleKeyInfo)

Enqueues a key for processing

public void EnqueueKey(ConsoleKeyInfo key)

Parameters

key ConsoleKeyInfo

GetDebugInfo()

Gets a debug string representation of current input state

public string GetDebugInfo()

Returns

string

GetHistory()

Gets recent input history for debugging

public IReadOnlyList<(ConsoleKeyInfo Key, DateTime Time)> GetHistory()

Returns

IReadOnlyList<(ConsoleKeyInfo Key, DateTime Time)>

GetLastKeys(int)

Gets the last N keys from history

public IReadOnlyList<ConsoleKeyInfo> GetLastKeys(int count)

Parameters

count int

Returns

IReadOnlyList<ConsoleKeyInfo>

GetRecommendedSleepDuration(int, int)

Gets a recommended sleep duration based on current activity

public int GetRecommendedSleepDuration(int minSleep = 10, int maxSleep = 100)

Parameters

minSleep int

Minimum sleep duration (when active)

maxSleep int

Maximum sleep duration (when idle)

Returns

int

Recommended sleep duration in milliseconds

PeekKey()

Peeks at the next key without removing it

public ConsoleKeyInfo? PeekKey()

Returns

ConsoleKeyInfo?

The key, or null if queue is empty

ProcessAll(Func<ConsoleKeyInfo, bool>)

Processes all pending input with a handler function

public int ProcessAll(Func<ConsoleKeyInfo, bool> handler)

Parameters

handler Func<ConsoleKeyInfo, bool>

Function to handle each key (return true to continue, false to stop)

Returns

int

Number of keys processed

UpdateIdleState()

Updates idle state based on current time. Should be called periodically from the main loop.

public void UpdateIdleState()

Events

IdleStateChanged

Event fired when idle state changes

public event EventHandler<IdleStateEventArgs>? IdleStateChanged

Event Type

EventHandler<IdleStateEventArgs>

KeyPressed

Event fired when a key is pressed (after enqueue)

public event EventHandler<KeyPressEventArgs>? KeyPressed

Event Type

EventHandler<KeyPressEventArgs>