Simple function for mouse
$ dotnet add package MouseHookAuthor: FauZaPespi
Date: 13.11.2024
Tired of having to rehook for each project? Use this already pre-made hook to call events at the right time, everything is perfect, use us
A .NET library designed to globally hook mouse events, providing real-time monitoring and simulation of mouse actions. This library enables developers to intercept, track, and simulate mouse clicks programmatically.
Global Mouse Hooking:
Mouse Button State Tracking:
IsKeepingLeftDown, IsKeepingRightDown).Mouse Click Simulation:
SendLeftDown, SendLeftUp, SendRightDown, SendRightUp).Customizable Event Handling:
GlobalMouseClick event when a mouse button is pressed or released, with details of the button and cursor position.Clone or download the repository and include the GlobalMouseHook class in your project.
Instantiate the GlobalMouseHook class to start monitoring mouse events globally.
var mouseHook = new GlobalMouseHook();
mouseHook.GlobalMouseClick += (button, position) =>
{
Console.WriteLine($"Button: {button}, Position: {position}");
};
Use these methods to check the current state of the mouse buttons:
bool isLeftButtonPressed = mouseHook.IsLeftDown();
bool isRightButtonPressed = mouseHook.IsRightDown();
bool isLeftHeld = mouseHook.IsKeepingLeftDown();
bool isRightHeld = mouseHook.IsKeepingRightDown();
Simulate mouse clicks programmatically:
mouseHook.SendLeftDown();
mouseHook.SendLeftUp();
mouseHook.SendRightDown();
mouseHook.SendRightUp();
Unhook the mouse hook when it is no longer needed:
mouseHook.Unhook();
| Functionality | Description |
|---|---|
SetHook() | Sets up a global mouse hook using SetWindowsHookEx. |
Unhook() | Releases the global mouse hook using UnhookWindowsHookEx. |
HookCallback() | Processes mouse events and raises the GlobalMouseClick event. |
GlobalMouseClick (event) | Raised when a mouse button is pressed or released, providing button name and cursor position. |
IsLeftDown() | Checks if the left mouse button is currently pressed. |
IsRightDown() | Checks if the right mouse button is currently pressed. |
IsKeepingLeftDown() | Checks if the left mouse button is being held down. |
IsKeepingRightDown() | Checks if the right mouse button is being held down. |
SendLeftDown() | Simulates a left mouse button press. |
SendLeftUp() | Simulates a left mouse button release. |
SendRightDown() | Simulates a right mouse button press. |
SendRightUp() | Simulates a right mouse button release. |
This project is licensed under the MIT License. See the LICENSE file for details.