Releases: Empiree/DeftSharp.Windows.Input
Release 0.10
0.10 Release available!
What's new:
- The library has migrated to the .NET 8 framework.
- Added new functionality.
- Small changes in usage.
[Breaking changes] ⚠️
- The
PreventMouseEventclass name has been changed toMousePreventOption. - The
KeyPressedArgsclass has changed its name toKeyboardInputArgs. - In the
MouseManipulatorclass, thePrevent()method now acceptsMousePreventOptioninstead ofPreventMouseEvent. - In the
KeyboardManipulatorclass, theSimulate()method now acceptsKeyboardSimulateOptioninstead ofKeyboardInputEvent.
Changelog
The library has completely migrated to the .NET 8 framework.
NumberListener
- Added a new class that can be used to subscribe to all number buttons on the keyboard (including Numpad).
LetterListener
- Added a new class that can be used to subscribe to all character buttons in the QWERTY layout.
KeyboardKeySet
- Added a new class that provides different sets of keys such as: NumpadKeys, NumberKeys, FunctionKeys, ArrowKeys, NavigationKeys. MultimediaKeys, EditingKeys, SpecialKeys.
var listener = new KeyboardListener();
// Subscription to F1 - F12 buttons
listener.Subscribe(KeyboardKeySet.FunctionKeys, key =>
{
Trace.WriteLine(key);
});KeyboardManipulator
- The
Simulate()method now acceptsKeyboardSimulateOptioninstead ofKeyboardInputEvent. - Added
KeyPressIntervalscollection, which contains buttons for which intervals are set.
MouseManipulator
- Added
Simulate()method. - Now the
Prevent()method acceptsMousePreventOptioninstead ofPreventMouseEvent.
Other changes
PreventMouseEventchanged name toMousePreventOption.KeyPressedArgschanged its name toKeyboardInputArgs.- Added
SystemChangesattribute, which denotes methods that change system settings and have a permanent effect. - Improved XML comments.
Release 0.9
0.9 Release available!
What's new:
- Added new functionality.
- Changed the names of existing methods and added new overloads to them.
- General improvements to the library's operations.
[Breaking changes] ⚠️
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!
- The
UnsubscribeAll()method has been replaced by an overload of theUnsubscribe()method. - The
UnbindAll()method has been replaced by an overload of theUnbind()method. - The
ResetIntervals()method has been replaced by an overload of theResetInterval()method. - The
ReleaseAll()method has been replaced by an overload of theRelease()method. - Changed the name of the interval in Subscribe methods. Now it is just
intervalinstead ofintervalOfClick.
var keyboardListener = new KeyboardListener();
keyboardListener.Subscribe(Key.Space, () =>
{
Trace.WriteLine("Space");
}, interval: TimeSpan.FromSeconds(1)); // Instead of intervalOfClickChangelog
KeyboardListener
- Added new overloads for Subscribe methods.
- Added new overloads for Unsubscribe methods.
- The
UnsubscribeAll()method has been replaced by an overload of theUnsubscribe()method.
KeyboardManipulator
- The
ReleaseAll()method has been replaced by an overload of theRelease()method. - The
ResetIntervals()method has been replaced by an overload of theResetInterval()method. - Added a
Simulate()method that can be used to simulate keyboard events such asKeyDownandKeyUp.
var keyboard = new KeyboardManipulator();
keyboard.Simulate(Key.LeftShift, KeyboardInputEvent.KeyDown); // Press the left Shift button
// ...
keyboard.Simulate(Key.LeftShift, KeyboardInputEvent.KeyUp); // Release the buttonKeyboardBinder
- The
UnbindAll()method has been replaced by an overload of theUnbind()method.
MouseListener
- Added new overloads for Unsubscribe methods.
- The
UnsubscribeAll()method has been replaced by an overload of theUnsubscribe()method. - Added
IsKeyPressed()method, which returns whether the button is pressed or not.
var mouseListener = new MouseListener();
var isLeftButtonPressed = mouseListener.IsKeyPressed(MouseButton.Left);MouseManipulator
- The
Click()method now supports the middle mouse button. - The
ReleaseAll()method has been replaced by an overload of theRelease()method. - Changed the
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); // trueNumpadListener
- Added
IsListeningproperty, which returns whether there are active subscriptions or not. - Added optional parameters for subscriptions, such as trigger interval and press event type.
var keyboardListener = new KeyboardListener();
var numpadListener = new NumpadListener(keyboardListener);
numpadListener.Subscribe(number =>
{
Trace.WriteLine(number);
},
interval: TimeSpan.FromSeconds(1),
keyboardEvent: KeyboardEvent.KeyUp);Other changes
- Improvement in the use of tracking the current state of the keys.
- XML comments improvement.
Release 0.8.2
0.8.2 Release available!
What's new:
- Small changes to the classes
[Breaking changes]
- The
Speedproperty has been changed to theGetSpeed()method - The
Layoutproperty has been changed to theGetLayout()method - The
Typeproperty has been changed to theGetKeyboardType()method
Changelog
NumpadListener
- Changed
Unsubscribe()method in NumpadListener, now this class unsubscribes only from subscriptions that were created by itself
Release 0.8.1
0.8.1 Release available! (HotFix)
What's new:
- Fixed multithreading bug in WinUI application
- Class name changes
[Breaking changes]
PreventMouseOptionclass name has been changed toPreventMouseEvent
Changelog
- Fixed a bug with multithreading in WinUI applications, which caused the application to start freezing.
PreventMouseOptionclass name has been changed toPreventMouseEvent.
Release 0.8
0.8 Release available!
What's new:
- Added new classes
- Bug fixes
- Improving the functionality of existing classes
[Breaking changes]
Coordinatesclass name has been changed toPointNumpadButtonclass name has been changed toNumButtonGetPosition()method changed toPositionproperty
Changelog
MouseListener
GetPositionmethod changed toPositionproperty.
MouseManipulator
- Added a
SetMouseSpeed()method that allows you to change the mouse speed. - Added an additional optional parameter to the
Prevent()method that accepts theFunc<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;
});MouseInfo
New class that contains information about the mouse and its current state.
- Added
Speedproperty that returns the current mouse speed.
var mouseInfo = new MouseInfo();
Trace.WriteLine(mouseInfo.Speed); // 10
// The speed can be changed through the MouseManipulator class.KeyboardInfo
New class that contains information about the keyboard and its current state.
- Added
Layoutproperty that returns the current keyboard layout. - Added
Typeproperty 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) keyboardOther changes
- Fixed a bug where the event handler was called before the input event itself.
- Fixed a bug where the
SubscribeAll()method subscribed to some events multiple times. - Added extension method for
Key-ToUnicode()which returns the interpretation of the key as a unicode string. Coordinatesclass name has been changed toPoint.NumpadButtonclass name has been changed toNumButton.
Release 0.7.1
0.7.1 Release available!
What's new:
- Improving the functionality of existing classes
[Breaking changes]
- KeyboardManipulator - The name of the
PressCombinationmethod has been changed toPress
Changelog
KeyboardManipulator
- The
PressCombinationmethod has been changed to an overload of thePressmethod that accepts a collection of keys:
var keyboardManipulator = new KeyboardManipulator();
keyboardManipulator.Press(Key.LeftCtrl, Key.V);
// before
// keyboardManipulator.PressCombination(new[]{Key.LeftCtrl, Key.V});KeyboardBinder
- Added
Swapmethod 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);- Added
GetBoundKeymethod, 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 EOther changes
- Added XML comments to existing classes
Release 0.7
0.7 Release available!
What's new:
- Improving the functionality of existing classes
[Breaking changes]
- KeyboardManipulator - The name of the
PreventManymethod has been changed toPrevent - KeyboardBinder - The name of the
BindManymethod has been changed toBind - MouseManipulator - The
ClickPreventedevent has been changed toInputPrevented
Changelog
KeyboardListener
- Added generic
KeyboardEvent.Allevent forSubscribemethods, which will be triggered on bothKeyDownandKeyUpmethods
var 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);KeyboardManipulator
- Added an additional optional parameter to the
Preventmethod that accepts theFunc<bool>predicate, in order to block pressing only under a certain condition
var keyboardManipulator = new KeyboardManipulator();
keyboardManipulator.Prevent(Key.Escape, () =>
{
var currentTime = DateTime.Now;
if (currentTime.Minute > 30)
return true;
return false;
});- Added
SetIntervalmethod, which allows you to set the frequency of pressing the specified button - Added
ResetIntervalmethod, which allows you to reset the set interval of a certain button
var keyboardManipulator = new KeyboardManipulator();
keyboardManipulator.SetInterval(Key.Space, TimeSpan.FromSeconds(1));
// ...
keyboardManipulator.ResetInterval(Key.Space);
// or
keyboardManipulator.SetInterval(Key.Space, TimeSpan.Zero);- The name of the
PreventManymethod has been changed toPrevent
KeyboardBinder
- The name of the
BindManymethod has been changed toBind
MouseManipulator
- The
ClickPreventedevent has been changed toInputPrevented - Added
Scrollmethod that allows you to scroll the mouse wheel
var mouseManipulator = new MouseManipulator();
mouseManipulator.Scroll(150); // Scroll up
mouseManipulator.Scroll(-150); // Scroll downOther changes
- Added handy methods for classes like
KeyboardManipulatorthat can take multiple values at once - Namespaces refactoring
Release 0.6
0.6 Release available!
What's new:
- Custom interceptors
- MouseListener class improvement
- KeyboardListener class improvement
Changelog
Custom Interceptors
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");KeyboardListener
- Added
IsKeyPressedmethod, which returns whether the button is pressed or not - Added useful properties such as
IsCapsLockActive,IsNumLockActive, etc
MouseListener
- Added
SubscribeAllmethod that subscribes to all mouse input events - Added overloading for
Subscribemethods, now you can get the input event. This is especially useful when using generic events likeButtonDownandButtonUp
Release 0.5
0.5 Release available!
What's new:
- New useful methods (
SubscribeAll,PressCombination, etc.) - New subscription types for MouseListener
- Small changes
[Breaking changes]
LeftButtonDoubleClickhas been removed due to unstable operation.
Changelog
KeyboardListener
- Added
SubscribeAllmethod, which subscribes to all possible buttons (about 200)
KeyboardManipulator
- Added
PressCombinationmethod, which acceptsIEnumerable<Key>and synchronously presses specified keyboard buttons
var keyboardManipulator = new KeyboardManipulator();
Key[] paste = { Key.LeftCtrl, Key.V };
keyboardManipulator.PressCombination(paste);MouseListener
- Added new subscription types:
MiddleButtonDown,MiddleButtonUp,Scroll - Added new generic subscriptions types:
ButtonDownandButtonUp. Which are not bound to a specific button - Removed
LeftButtonDoubleClicksubscription due to unstable operation
MouseManipulator
- Added new types of prevent:
MiddleButtonandScroll - Added a new overload of the
Clickmethod, which now does not need coordinates. Clicking is based on the current location of the mouse
Other changes
- It is no longer possible to specify
Key.Noneas a key. If you try to do so, the user will get an exception
Release 0.4
0.4 Release available!
What's new:
KeyboardListener
- Sequence subscriptions
- Combination subscriptions
- Small changes of use
Changelog
KeyboardListener
The KeyboardListener class now has the ability to subscribe to combinations of button presses, as well as their sequence.
New Methods:
- SubscribeSequence - subscribe to a sequence of button presses.
Key[] sequence = { Key.Q, Key.W, Key.E };
keyboardListener.SubscribeSequence(sequence, () =>
{
// This code will trigger after successive presses of 'Q W E' buttons
});- SubscribeCombination - subscribe to a combination of button presses.
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.
Other changes
- Removed overloading method for
Unsubscribe(IEnumerable<Key> keys)due to the addition of new subscription types, as such, this method could cause misconception of usage. - The name of the
KeyboardSubscriptionclass has been changed toKeySubscription
