Visual-Vincent / InputHelper

A .NET friendly library for simulating mouse and keyboard input.
BSD 3-Clause "New" or "Revised" License
14 stars 6 forks source link

Differentiate between user-pressed and InputHelper-generated keystrokes? #6

Closed ryderhaggard closed 1 year ago

ryderhaggard commented 1 year ago

Is there a way for the hook to tell if its a user pressed key versus a input helper pressed key?

sorry not sure how to flag this as a question instead of an issue.

Visual-Vincent commented 1 year ago

As a matter of fact there is! (sort of) Which was something I had overlooked in my previous implementation of the hooks.

As it turns out there is a way to check whether a keystroke was injected by an application rather than by an input device, but unfortunately there is currently no way of knowing whether it came specifically from InputHelper (e.g. it could come from a completely different application).

I just released v2.1.0.0 (diff) which adds the functionality to check for mouse/keyboard event injection. Go grab it over at Releases.

Example usage:

VB.NET

Private Sub KeyboardHook_KeyDown(sender As Object, e As InputHelper.EventArgs.KeyboardHookEventArgs)
    'If the keystroke was not generated by the user, ignore it
    If e.Injected Then
        Return
    End If

    'Do something here...
End Sub

C#

private void KeyboardHook_KeyDown(object sender, InputHelper.EventArgs.KeyboardHookEventArgs)
{
    // If the keystroke was not generated by the user, ignore it
    if(e.Injected)
        return;

    // Do something here...
}

As for flagging your issue as a question, I think that's only something maintainers and collaborators of the repository are able to do. Don't worry about it, I take care of it for every issue. :)

ryderhaggard commented 1 year ago

Thank you very much! I'm very impressed all around!