/// <summary> /// Represents the worker thread that monitors pressed keys. /// </summary> /// <param name="data">The thread arguments.</param> /// <exception cref="ArgumentOutOfRangeException"> /// Is thrown if the specified instance for data is not of type <see cref="KeyboardWatcherThreadArguments"/>. /// </exception> private void Worker(object data) { if (!(data is KeyboardWatcherThreadArguments)) { throw new ArgumentOutOfRangeException(nameof(data), $"The specified data must be an instance of the {nameof(KeyboardWatcherThreadArguments)} class"); } KeyboardWatcherThreadArguments args = (KeyboardWatcherThreadArguments)data; while (!args.Exit) { if (!Console.KeyAvailable) { Thread.Sleep(10); continue; } ConsoleKeyInfo cki = Console.ReadKey(true); this.FireOnKeyPressed(new OnKeyPressedEventArgs(cki)); } }
/// <summary> /// Initializes a new instance of the <see cref="KeyBoardWatcher"/> class. /// </summary> public KeyBoardWatcher() { this.threadArguments = new KeyboardWatcherThreadArguments(); }