The ConsoleInput library provides functionality for reading mouse states and positions within the Windows console window. This library is designed to facilitate mouse and keyboard input handling in console-based applications (.NET Framework 4.8).
There are a few ways you can use the library.
To get started with using the library as a nuget package, just install the package. The package is both hosted on Github and on Nuget.org
dotnet add package ConsoleInput
To get started with using the library as a direct dependency, follow these steps:
- Clone the ConsoleInput repository
- Open the solution (with Visual Studio 2022)
- Build the solution to ensure all dependencies are resolved and the library is compiled successfully
- Add a references to the
ConsoleInput,ConsoleInput.WinAPIandConsoleInput.Logicprojects in your solution, and then in your project.
To use the ConsoleInput library in your own project, follow these steps:
- Add the project as dependency using either guide above
- Instantiate an input device, e.g.
ConsoleMouse - Instantiate the input manager,
InputManager - Add input devices to
InputManagerusingIInputManager.AddDevice(IDevice) - Poll input using
IInputManager.Update() - Use the provided methods and properties to read the device state and position
Here's an example of how to use the ConsoleInput library (the following example applies to the new ConsoleInput 2.0):
using ConsoleInput; // Instantiate the input devices you are going to use. var mouse = new ConsoleMouse(); var keyboard = new ConsoleKeyboard(); // Instantiate an input manager. var inputManager = new InputManager(); // Add devices (IDevice) to the input manager. inputManager.AddDevice(mouse); inputManager.AddDevice(keyboard); while (...) { // polls input and updates states. inputManager.Update(); // Changes console cursor position to the cell the mouse is hovering over. Console.SetCursorPosition(mouse.X, mouse.Y); // The ICursorDevice provides `short X` and `short Y` properties. // The initial frame the key is pressed. if (mouse.IsButtonPressed(MouseButton.Left)) { Console.WriteLine("Pressed"); } // The frame after the key has been released. if (mouse.IsButtonReleased(MouseButton.Left)) { Console.WriteLine("Released"); } // All frames during the press. if (mouse.IsButtonDown(MouseButton.Left)) { Console.Write("x"); } // Both devices implement the same interface (IButtonDevice), but with different enums (button options). This keeps the API simple. if (keyboard.IsButtonPressed(KeyboardButton.Space)) { Console.WriteLine("SPACE!"); } }
I've been spending some time splitting up the project and focusing each subproject on a single responsibility.
These are the projects defining the functionality of the library.
The main project of ConsoleInput. It contains most the API to interact with, and the core functionality. This project depends on other non-test projects.
Contains the core logic surrounding determining when a an input should move between Pressed, Released and Down state.
Contains Windows API wrappers and utilities used for interoping with mouse and keyboard.
These projects showcase how to use the API. Furthermore, they're useful for letting me know immediately if I were to break API compatibility.
A painting tool (the name isn't technical, it's just a pun) for drawing with your mouse. It primarily focuses on making use of the mouse coordinates. It makes use of the 2.0 API.
The same tool as above, but maintained to use the legacy API (1.3). I don't advocate using it, but it's useful for keeping track of backwards compatibility.
These projects contain unit-tests for the other projects.
Contains additional tests for the ConsoleInput library. Only contains mock-classes right now.
Contains unit tests for the logic components of the ConsoleInput library, to make sure it's correctly categorizing input sequences in each state.
ANY contribution to the ConsoleInput library is welcome!
I'm using StyleCop.Analyzer, but I don't agree with every single pedantic rule. I haven't taken the time to disable all the rules I don't agree with, so you can just try to follow the rules to the best of your ability.