TolikPylypchuk / SharpHook

SharpHook provides a cross-platform global keyboard and mouse hook, event simulation, and text entry simulation for .NET
https://sharphook.tolik.io
MIT License
342 stars 32 forks source link

Listen only when app is in front #78

Closed jupagose closed 9 months ago

jupagose commented 9 months ago

I use your great tool in .net maui windows app but I'm facing some issues due when the user go to another app my app still listening keyboard.

Another question, there is a way to disable mouse listening ? I only need kb.

Thanks

TolikPylypchuk commented 9 months ago

Hi! Thanks for asking!

Regarding the first question - the hooks are global, so they will work whether your app is in the foreground or not. If you want to listen to keyboard events only when your app is in the foreground, then you should use MAUI-specific tools for that (I can't help you with that since I've never worked with MAUI). I'm sure MAUI has something like OnKeyDown and OnKeyUp for window objects. If MAUI doesn't provide such functionality, then you should check whether the app is in the foreground inside event handlers.

Regarding the second question - the global hooks always listen to both keyboard and mouse events, you can't disable the mouse hook, but you don't need to create event handlers for all events.

jupagose commented 9 months ago

Thanks for answer.

  1. I implemented hookservice added Paused property to it and in my mainwindow events OnAcivated an OnDeactivated handle Paused property of my service.

  2. About just listen keyboard is about in debug mode some times mouse move over my app becomes very very slow after 20-30 secones everythig gets normal.

TolikPylypchuk commented 9 months ago

Glad to hear that you've made it work! Regarding the second point - yeah, this is a known issue which cannot be fixed because of the way Windows handles global hooks.

TolikPylypchuk commented 8 months ago

Hi @jupagose! I've recently released version 5.3.0 which adds a way to disable the mouse hook and only listen to keyboard events. It can be done like this for example (it's available for all hook types):

var hook = new TaskPoolGlobalHook(GlobalHookType.Keyboard);

This way when you call hook.Run() or hook.RunAsync() a mouse hook won't even be created, and your mouse cursor won't freeze when debugging an event handler.

jupagose commented 8 months ago

Great news. I'm going to try it.