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
IsIdle
Gets whether the system is currently idle (no recent input)
public bool IsIdle { get; }
Property Value
LastKeyTime
Gets the time of the last key press
public DateTime LastKeyTime { get; }
Property Value
LastModifiers
Gets the last known modifier state
public ConsoleModifiers LastModifiers { get; }
Property Value
PendingInputCount
Gets the number of pending keys in the queue
public int PendingInputCount { get; }
Property Value
TimeSinceLastKey
Gets the time since the last key press
public TimeSpan TimeSinceLastKey { get; }
Property Value
WasAltHeld
Checks if Alt modifier was held in last key press
public bool WasAltHeld { get; }
Property Value
WasControlHeld
Checks if Control modifier was held in last key press
public bool WasControlHeld { get; }
Property Value
WasShiftHeld
Checks if Shift modifier was held in last key press
public bool WasShiftHeld { get; }
Property Value
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
keyConsoleKeyInfo
GetDebugInfo()
Gets a debug string representation of current input state
public string GetDebugInfo()
Returns
GetHistory()
Gets recent input history for debugging
public IReadOnlyList<(ConsoleKeyInfo Key, DateTime Time)> GetHistory()
Returns
GetLastKeys(int)
Gets the last N keys from history
public IReadOnlyList<ConsoleKeyInfo> GetLastKeys(int count)
Parameters
countint
Returns
GetRecommendedSleepDuration(int, int)
Gets a recommended sleep duration based on current activity
public int GetRecommendedSleepDuration(int minSleep = 10, int maxSleep = 100)
Parameters
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
handlerFunc<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
KeyPressed
Event fired when a key is pressed (after enqueue)
public event EventHandler<KeyPressEventArgs>? KeyPressed