An open-source .NET library designed for keyboard and mouse control in Windows.
MIT License
Bot releases are visible (Hide)
We had to change a few things about using the library. The goal of our changes is to create a more user-friendly and intuitive design. We try to minimize these things so that you don't have to edit already created code. Thank you for your understanding!
UnsubscribeAll()
method has been replaced by an overload of the Unsubscribe()
method.UnbindAll()
method has been replaced by an overload of the Unbind()
method.ResetIntervals()
method has been replaced by an overload of the ResetInterval()
method.ReleaseAll()
method has been replaced by an overload of the Release()
method.interval
instead of intervalOfClick
.var keyboardListener = new KeyboardListener();
keyboardListener.Subscribe(Key.Space, () =>
{
Trace.WriteLine("Space");
}, interval: TimeSpan.FromSeconds(1)); // Instead of intervalOfClick
UnsubscribeAll()
method has been replaced by an overload of the Unsubscribe()
method.ReleaseAll()
method has been replaced by an overload of the Release()
method.ResetIntervals()
method has been replaced by an overload of the ResetInterval()
method.Simulate()
method that can be used to simulate keyboard events such as KeyDown
and KeyUp
.var keyboard = new KeyboardManipulator();
keyboard.Simulate(Key.LeftShift, KeyboardInputEvent.KeyDown); // Press the left Shift button
// ...
keyboard.Simulate(Key.LeftShift, KeyboardInputEvent.KeyUp); // Release the button
UnbindAll()
method has been replaced by an overload of the Unbind()
method.UnsubscribeAll()
method has been replaced by an overload of the Unsubscribe()
method.IsKeyPressed()
method, which returns whether the button is pressed or not.var mouseListener = new MouseListener();
var isLeftButtonPressed = mouseListener.IsKeyPressed(MouseButton.Left);
Click()
method now supports the middle mouse button.ReleaseAll()
method has been replaced by an overload of the Release()
method.IsKeyLocked()
method, it now accepts the same event type as the Prevent method.var mouse = new MouseManipulator();
mouse.Prevent(PreventMouseEvent.Scroll);
var isScrollLocked = mouse.IsKeyLocked(PreventMouseEvent.Scroll); // true
IsListening
property, which returns whether there are active subscriptions or not.var keyboardListener = new KeyboardListener();
var numpadListener = new NumpadListener(keyboardListener);
numpadListener.Subscribe(number =>
{
Trace.WriteLine(number);
},
interval: TimeSpan.FromSeconds(1),
keyboardEvent: KeyboardEvent.KeyUp);
Published by Empiree 6 months ago
Speed
property has been changed to the GetSpeed()
methodLayout
property has been changed to the GetLayout()
methodType
property has been changed to the GetKeyboardType()
methodUnsubscribe()
method in NumpadListener, now this class unsubscribes only from subscriptions that were created by itselfPublished by Empiree 7 months ago
PreventMouseOption
class name has been changed to PreventMouseEvent
PreventMouseOption
class name has been changed to PreventMouseEvent
.Published by Empiree 7 months ago
Coordinates
class name has been changed to Point
NumpadButton
class name has been changed to NumButton
GetPosition()
method changed to Position
propertyGetPosition
method changed to Position
property.SetMouseSpeed()
method that allows you to change the mouse speed.Prevent()
method that accepts the Func<bool>
predicate, in order to block pressing only under a certain condition.var mouseManipulator = new MouseManipulator();
mouseManipulator.Prevent(PreventMouseOption.Scroll, () =>
{
var currentTime = DateTime.Now;
if (currentTime.Minute > 30)
return true;
return false;
});
New class that contains information about the mouse and its current state.
Speed
property that returns the current mouse speed.var mouseInfo = new MouseInfo();
Trace.WriteLine(mouseInfo.Speed); // 10
// The speed can be changed through the MouseManipulator class.
New class that contains information about the keyboard and its current state.
Layout
property that returns the current keyboard layout.Type
property that returns the keyboard type.var keyboardInfo = new KeyboardInfo();
Trace.WriteLine(keyboardInfo.Layout.DisplayName); // English (United States)
Trace.WriteLine(keyboardInfo.Type.Name); // IBM enhanced (101- or 102-key) keyboard
SubscribeAll()
method subscribed to some events multiple times.Key
- ToUnicode()
which returns the interpretation of the key as a unicode string.Coordinates
class name has been changed to Point
.NumpadButton
class name has been changed to NumButton
.Published by Empiree 7 months ago
PressCombination
method has been changed to Press
PressCombination
method has been changed to an overload of the Press
method that accepts a collection of keys:var keyboardManipulator = new KeyboardManipulator();
keyboardManipulator.Press(Key.LeftCtrl, Key.V);
// before
// keyboardManipulator.PressCombination(new[]{Key.LeftCtrl, Key.V});
Swap
method that swaps button bindings between each other: var keyboardBinder = new KeyboardBinder();
keyboardBinder.Swap(Key.Q, Key.W);
// before
// keyboardBinder.Bind(Key.Q, Key.W);
// keyboardBinder.Bind(Key.W, Key.Q);
GetBoundKey
method, which returns the current button bind. If the bind of the button has not been changed, it will return the same button:var keyboardBinder = new KeyboardBinder();
keyboardBinder.Bind(Key.C, Key.V);
keyboardBinder.GetBoundKey(Key.C); // return V
keyboardBinder.GetBoundKey(Key.V); // return V
keyboardBinder.GetBoundKey(Key.E); // return E
Published by Empiree 7 months ago
PreventMany
method has been changed to Prevent
BindMany
method has been changed to Bind
ClickPrevented
event has been changed to InputPrevented
KeyboardEvent.All
event for Subscribe
methods, which will be triggered on both KeyDown
and KeyUp
methodsvar keyboardListener = new KeyboardListener();
keyboardListener.Subscribe(Key.LWin, (key, eventType) =>
{
if(eventType is KeyboardInputEvent.KeyDown)
Trace.WriteLine($"{key} pressed");
if(eventType is KeyboardInputEvent.KeyUp)
Trace.WriteLine($"{key} released");
}, keyboardEvent: KeyboardEvent.All);
Prevent
method that accepts the Func<bool>
predicate, in order to block pressing only under a certain conditionvar keyboardManipulator = new KeyboardManipulator();
keyboardManipulator.Prevent(Key.Escape, () =>
{
var currentTime = DateTime.Now;
if (currentTime.Minute > 30)
return true;
return false;
});
SetInterval
method, which allows you to set the frequency of pressing the specified buttonResetInterval
method, which allows you to reset the set interval of a certain buttonvar keyboardManipulator = new KeyboardManipulator();
keyboardManipulator.SetInterval(Key.Space, TimeSpan.FromSeconds(1));
// ...
keyboardManipulator.ResetInterval(Key.Space);
// or
keyboardManipulator.SetInterval(Key.Space, TimeSpan.Zero);
PreventMany
method has been changed to Prevent
BindMany
method has been changed to Bind
ClickPrevented
event has been changed to InputPrevented
Scroll
method that allows you to scroll the mouse wheelvar mouseManipulator = new MouseManipulator();
mouseManipulator.Scroll(150); // Scroll up
mouseManipulator.Scroll(-150); // Scroll down
KeyboardManipulator
that can take multiple values at oncePublished by Empiree 7 months ago
What's new:
Version 0.6 introduced the ability to create your own interceptors. This means that if your use case is unique and requires its own implementation, you can create a new interceptor, similar to KeyboardListener or KeyboardManipulator!
Let's try to do it together!
Our goal: block the scroll mouse event, and output all mouse events to the console except Move
, since that event will quickly spam the console.
First, let's create an interceptor to block mouse scroll events:
public class ScrollDisabler : MouseInterceptor
{
protected override bool IsInputAllowed(MouseInputArgs args)
{
if (args.Event is MouseInputEvent.Scroll)
return false; // disallow mouse scroll input
return true; // all other input events can be processed
}
}
To do this, we need to inherit from the MouseInterceptor
class and implement the IsInputAllowed
method, which is responsible for blocking events.
Next, let's create another interceptor to output all events to the console.
public class MouseLogger : MouseInterceptor
{
// Always allow input because it's a logger
protected override bool IsInputAllowed(MouseInputArgs args) => true;
// If the input event was successfully processed
protected override void OnInputSuccess(MouseInputArgs args)
{
if (args.Event is MouseInputEvent.Move) // Don't log a move event
return;
Trace.WriteLine($"Processed {args.Event}");
}
// If the input event has been blocked
protected override void OnInputFailure(MouseInputArgs args, IEnumerable<InterceptorInfo> failedInterceptors)
{
var failureReason = failedInterceptors.ToNames();
Trace.WriteLine($"Failed {args.Event} by: {failureReason}");
}
}
In addition to the familiar IsInputAllowed
method, we have overridden two more methods for input processing.
OnInputSuccess
- called if the input was processed successfully and no interceptor blocked it.
OnInputFailure
- called if the event was blocked by one or more interceptors. In it we will get the list of these interceptors.
[!NOTE]
The implementation of these 2 interceptors can be placed in one interceptor, but it is better to separate it. So that each is responsible for its own task.
All we have to do is to call the Hook
method of these two classes to make the interceptors work:
var scrollDisabler = new ScrollDisabler();
var mouseLogger = new MouseLogger();
scrollDisabler.Hook();
mouseLogger.Hook();
Now let's run our project and test their work:
In the Debug console, we can see that the mouse button events have fired. And mouse wheel scrolling was blocked by ScrollDisabler
class. If we need to disable this interceptor, it is enough to call the Unhook
method.
It was a simple implementation of a custom interceptor. In your scenarios they can be much larger and with stronger logic.
To do the same thing but using already created interceptors, just do this:
var mouseListener = new MouseListener();
var mouseManipulator = new MouseManipulator();
mouseListener.SubscribeAll(mouseEvent =>
{
if (mouseEvent is MouseInputEvent.Move)
return;
Trace.WriteLine($"Processed {mouseEvent}");
});
mouseManipulator.Prevent(PreventMouseOption.Scroll);
mouseManipulator.ClickPrevented += mouseEvent =>
Trace.WriteLine($"Failed {mouseEvent} by: MouseManipulator");
IsKeyPressed
method, which returns whether the button is pressed or notIsCapsLockActive
, IsNumLockActive
, etcSubscribeAll
method that subscribes to all mouse input eventsSubscribe
methods, now you can get the input event. This is especially useful when using generic events like ButtonDown
and ButtonUp
Published by Empiree 7 months ago
What's new:
SubscribeAll
, PressCombination
, etc.)[Breaking change] : LeftButtonDoubleClick
has been removed due to unstable operation.
SubscribeAll
method, which subscribes to all possible buttons (about 200)PressCombination
method, which accepts IEnumerable<Key>
and synchronously presses specified keyboard buttonsvar keyboardManipulator = new KeyboardManipulator();
Key[] paste = { Key.LeftCtrl, Key.V };
keyboardManipulator.PressCombination(paste);
MiddleButtonDown
, MiddleButtonUp
, Scroll
ButtonDown
and ButtonUp
. Which are not bound to a specific buttonLeftButtonDoubleClick
subscription due to unstable operationMiddleButton
and Scroll
Click
method, which now does not need coordinates. Clicking is based on the current location of the mouseKey.None
as a key. If you try to do so, the user will get an exceptionPublished by Empiree 7 months ago
What's new:
KeyboardListener
The KeyboardListener class now has the ability to subscribe to combinations of button presses, as well as their sequence.
New Methods:
Key[] sequence = { Key.Q, Key.W, Key.E };
keyboardListener.SubscribeSequence(sequence, () =>
{
// This code will trigger after successive presses of 'Q W E' buttons
});
Key[] combination = { Key.Q, Key.W, Key.E };
keyboardListener.SubscribeCombination(combination , () =>
{
// This code will be triggered by pressing the 'Q W E' button combination
});
It is also possible to call these methods once, using the SubscribeSequenceOnce
and SubscribeCombinationOnce
methods.
[!NOTE]
The length of combinations/sequences should be between 2 and 10 buttons.
Unsubscribe(IEnumerable<Key> keys)
due to the addition of new subscription types, as such, this method could cause misconception of usage.KeyboardSubscription
class has been changed to KeySubscription
Published by Empiree 7 months ago
What's new:
A class that allows you to change the bindings of the specified buttons.
Methods:
keyboardBinder.Bind(Key.A, Key.B); // when the 'A' button is pressed, the 'B' button will be pressed instead.
Key[] keys = { Key.Q, Key.W, Key.E };
keyboardBinder.BindMany(keys, Key.B); // Q, W, E -> B
keyboardBinder.Bind(Key.Q, Key.W);
keyboardBinder.Unbind(Key.Q);
// Or
keyboardBinder.UnbindAll();
Important! The principle of KeyboardBinder operation is the same as in the KeyboardManipulator class. You can have as many objects as you want, but they all work with one single interceptor. Therefore, the state is shared between all KeyboardBinder objects.
Published by Empiree 8 months ago
0.2 Release available!
What's new:
Changelog:
KeyboardManipulator - a keyboard control class is created to emulate pressing buttons, as well as disabling them.
Methods:
keyboardManipulator.Press(Key.A);
keyboardManipulator.Prevent(Key.A);
keyboardManipulator.Release(Key.A);
You can also use the ReleaseAll() method:
keyboardManipulator.ReleaseAll();
Important! Locked buttons are shared among all KeyboardManipulator classes. You don't have to worry that an object in this class has locked a particular button and you no longer have access to that object.
For example:
var first = new KeyboardManipulator();
first.Prevent(Key.A);
var second = new KeyboardManipulator();
second.Prevent(Key.B);
first.LockedKeys // Key.A, Key.B
second.LockedKeys // Key.A, Key.B
Also, several events are available: KeyPrevented and KeyReleased. They are only triggered by a successful operation. For example, if a button is locked several times, the KeyPrevented event will be triggered only once.
Mouse Manipulator - a mouse control class.
Methods:
mouseManipulator.Click(500, 500, MouseButton.Left); // the left mouse button will be pressed at the coordinates: x: 500, y:500
Published by Empiree 8 months ago
DeftSharp.Windows.Input Release 0.1
Features:
Classes: