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
PreventMouseEvent
class name has been changed toMousePreventOption
. - The
KeyPressedArgs
class has changed its name toKeyboardInputArgs
. - In the
MouseManipulator
class, thePrevent()
method now acceptsMousePreventOption
instead ofPreventMouseEvent
. - In the
KeyboardManipulator
class, theSimulate()
method now acceptsKeyboardSimulateOption
instead 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 acceptsKeyboardSimulateOption
instead ofKeyboardInputEvent
. - Added
KeyPressIntervals
collection, which contains buttons for which intervals are set.
MouseManipulator
- Added
Simulate()
method. - Now the
Prevent()
method acceptsMousePreventOption
instead ofPreventMouseEvent
.
Other changes
PreventMouseEvent
changed name toMousePreventOption
.KeyPressedArgs
changed its name toKeyboardInputArgs
.- Added
SystemChanges
attribute, 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
interval
instead ofintervalOfClick
.
var keyboardListener = new KeyboardListener();
keyboardListener.Subscribe(Key.Space, () =>
{
Trace.WriteLine("Space");
}, interval: TimeSpan.FromSeconds(1)); // Instead of intervalOfClick
Changelog
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 asKeyDown
andKeyUp
.
var keyboard = new KeyboardManipulator();
keyboard.Simulate(Key.LeftShift, KeyboardInputEvent.KeyDown); // Press the left Shift button
// ...
keyboard.Simulate(Key.LeftShift, KeyboardInputEvent.KeyUp); // Release the button
KeyboardBinder
- 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); // true
NumpadListener
- Added
IsListening
property, 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
Speed
property has been changed to theGetSpeed()
method - The
Layout
property has been changed to theGetLayout()
method - The
Type
property 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]
PreventMouseOption
class name has been changed toPreventMouseEvent
Changelog
- Fixed a bug with multithreading in WinUI applications, which caused the application to start freezing.
PreventMouseOption
class 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]
Coordinates
class name has been changed toPoint
NumpadButton
class name has been changed toNumButton
GetPosition()
method changed toPosition
property
Changelog
MouseListener
GetPosition
method changed toPosition
property.
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
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.
KeyboardInfo
New class that contains information about the keyboard and its current state.
- Added
Layout
property that returns the current keyboard layout. - Added
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
Other 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. Coordinates
class name has been changed toPoint
.NumpadButton
class 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
PressCombination
method has been changed toPress
Changelog
KeyboardManipulator
- The
PressCombination
method has been changed to an overload of thePress
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});
KeyboardBinder
- Added
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);
- Added
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
Other 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
PreventMany
method has been changed toPrevent
- KeyboardBinder - The name of the
BindMany
method has been changed toBind
- MouseManipulator - The
ClickPrevented
event has been changed toInputPrevented
Changelog
KeyboardListener
- Added generic
KeyboardEvent.All
event forSubscribe
methods, which will be triggered on bothKeyDown
andKeyUp
methods
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
Prevent
method 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
SetInterval
method, which allows you to set the frequency of pressing the specified button - Added
ResetInterval
method, 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
PreventMany
method has been changed toPrevent
KeyboardBinder
- The name of the
BindMany
method has been changed toBind
MouseManipulator
- The
ClickPrevented
event has been changed toInputPrevented
- Added
Scroll
method that allows you to scroll the mouse wheel
var mouseManipulator = new MouseManipulator();
mouseManipulator.Scroll(150); // Scroll up
mouseManipulator.Scroll(-150); // Scroll down
Other changes
- Added handy methods for classes like
KeyboardManipulator
that 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
IsKeyPressed
method, which returns whether the button is pressed or not - Added useful properties such as
IsCapsLockActive
,IsNumLockActive
, etc
MouseListener
- Added
SubscribeAll
method that subscribes to all mouse input events - Added overloading for
Subscribe
methods, now you can get the input event. This is especially useful when using generic events likeButtonDown
andButtonUp
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]
LeftButtonDoubleClick
has been removed due to unstable operation.
Changelog
KeyboardListener
- Added
SubscribeAll
method, which subscribes to all possible buttons (about 200)
KeyboardManipulator
- Added
PressCombination
method, 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:
ButtonDown
andButtonUp
. Which are not bound to a specific button - Removed
LeftButtonDoubleClick
subscription due to unstable operation
MouseManipulator
- Added new types of prevent:
MiddleButton
andScroll
- Added a new overload of the
Click
method, 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.None
as 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
KeyboardSubscription
class has been changed toKeySubscription