Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for blocking hotkeys (#11) #12

Merged
merged 1 commit into from
Oct 19, 2022
Merged

Conversation

srwi
Copy link
Contributor

@srwi srwi commented Oct 8, 2022

I added an optional parameter blocking for the RegisterHotkey method to allow the user to block the original input from propagating on a per-hotkey basis. Default behavior is still to not block propagation.

This resolves issue #11.

private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
var vkCode = Marshal.ReadInt32(lParam);

// Debug.WriteLine("Starting");
// To prevent slowing keyboard input down, we use handle keyboard inputs in a separate thread
ThreadPool.QueueUserWorkItem(this.HandleSingleKeyboardInput, new KeyboardParams(wParam, vkCode));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it has been a long time since I have coded/used this package, I recall that this had an important positive impact on performance.

Unfortunately, the new behavior you are trying to introduce requires (at the very least) to check if the pressed keys are assigned to a blocking hotkey before proceeding. I do believe that looking up the hotkey is fast enough for us to move it to the main thread, but as soon as we find out that a hotkey is non-blocking I want it to be handled by the thread pool like it used to be.

The bottom line is we have two choices:

either move the parameter up to the KeyboardManager's constructor parameters and then maintain the old thread pool behavior when non-blocking,
or make the handling of non-blocking hotkeys run in a new thread to maintain the old behavior

Copy link
Contributor Author

@srwi srwi Oct 19, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(reposting comment from correct account)

Thanks for taking the time to look at the PR. I am not entirely sure I understand your comment correctly.

as soon as we find out that a hotkey is non-blocking I want it to be handled by the thread pool like it used to be.

With the changes in this PR every shortcut callback function will be executed in the thread pool (ref). As you said only the check for a matching shortcut will be performed on the main thread.

Regarding your proposed solutions:

either move the parameter up to the KeyboardManager's constructor parameters and then maintain the old thread pool behavior when non-blocking

You mean dedicating a separate instance of KeyboardManager for either blocking or non-blocking behavior?

make the handling of non-blocking hotkeys run in a new thread to maintain the old behavior

By this, do you mean having two separate dictionaries (e.g. _registeredBlockingCallbacks and _registeredNonBlockingCallbacks) and then iterating over one in the main thread and the other in a new thread?

The first option would probably define the behavior a little more clearly for the user while the second approach would give the user a simpler API to work with (although the implementation would probably make me feel a little uneasy).

It's hard for me to get a feeling of the input delay my changes would have. I can't imagine it being noticeable but also I don't know how you can measure it.

Please let me know what you think and whether I understood your comment correctly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My sincerest apologies, I was (and still am) reviewing this PR from an airport terminal. It seems you have already implemented this feature in the way that I described in my 1st comment.

Your changes will likely not impact input delay, and as you said yourself, this API is more developer friendly :)

P.S. what a relief to see another person who struggles juggling 2 github users; I will approve this PR via the correct account in a moment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, considering your location, thank you even more for finding the time!

@kfirprods kfirprods self-assigned this Oct 19, 2022
@kfirprods kfirprods merged commit c2509e1 into kfirprods:master Oct 19, 2022
@kfirprods
Copy link
Owner

Great :) I will release a new version to NuGet on Friday. If you see that it is taking longer feel free to comment here

@kfirprods
Copy link
Owner

Published to nuget. Unfortunately I did not have the time to install the new version on a local project to verify the new feature, but if you could do it @stnkl that'll be lovely :)

@srwi
Copy link
Contributor Author

srwi commented Nov 1, 2022

Hi, sorry for the late reply. Unfortunately I am unable to test the nuget package due to the problems described in #7. I was developing and testing the changes of this PR by manually adding this library as a separate project to my solution,

I might look into it again when I have some time though. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants