Basic example of how it works on dotnet maui #95
-
|
I tried to use the package in a real case, but I couldn't understand how to use it, I would like to see a basic real case of how to use it, for example, if in the standard project that is created in dotnet where there is a counter, how do I do it? that when my user presses the enter key on the keyboard, I call the button click method? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
Hello! Thanks for asking! Please, bear in mind that SharpHook lets you create hooks that are global to the entire OS. The hook will work even if the application is not in the foreground, and it requires special permissions on macOS. Are you sure that this is what you really need? Perhaps you need to handle keyboard events only when your application is in the foreground, or you need to handle the Enter key only when a button is in focus - and for that you should use MAUI-specific APIs. If you really do need a global hook, then here's a basic usage example. To be honest, I don't have any experience with MAUI, but the basic idea is that you create an instance of The hook object should be global and should in general exist for the whole time your application runs (or not, depending on your use-case). In your example this can be achieved like this: namespace ExampleKeysIndiFarma
{
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
var hook = new TaskPoolGlobalHook();
hook.KeyPressed += (sender, e) =>
{
if (e.Data.KeyCode == KeyCode.VcEnter)
{
this.Dispatcher.Dispatch(() => OnCounterClicked(sender, e));
}
};
_ = hook.RunAsync();
}
private void OnCounterClicked(object sender, EventArgs e)
{
count++;
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
SemanticScreenReader.Announce(CounterBtn.Text);
}
}
}Don't forget to wrap stuff that modifies UI into Remember that there must be one global hook in the entire application, so you should probably create it in the application class or something like that and then pass it down to individual views if needed. The example above is too simple to use in a real application. Here's another example which uses dependency injection: // MauiProgram.cs
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App>();
builder.Services.AddSingleton<IGlobalHook>(sp => new TaskPoolGlobalHook(runAsyncOnBackgroundThread: true));
var app = builder.Build();
var hook = app.Services.GetRequiredService<IGlobalHook>();
hook.RunAsync();
return app;
}
}
// MainPage.xaml.cs
public partial class MainPage : ContentPage
{
int count = 0;
public MainPage()
{
this.InitializeComponent();
// There are most probably better ways to get the global hook from the service provider, this is just for simplicity
var hook = Application.Current!.Handler.MauiContext!.Services.GetRequiredService<IGlobalHook>();
this.Loaded += (sender, e) => hook.KeyPressed += OnKeyPressed;
this.Unloaded += (sender, e) => hook.KeyPressed -= OnKeyPressed;
}
public void OnKeyPressed(object? sender, KeyboardHookEventArgs e)
{
if (e.Data.KeyCode == KeyCode.VcEnter)
{
this.Dispatcher.Dispatch(() => OnCounterClicked(sender, e));
}
}
private void OnCounterClicked(object? sender, EventArgs e)
{
count++;
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
SemanticScreenReader.Announce(CounterBtn.Text);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
How could I create key combination, for example ctrl + enter performs such action |
Beta Was this translation helpful? Give feedback.
Hello! Thanks for asking!
Please, bear in mind that SharpHook lets you create hooks that are global to the entire OS. The hook will work even if the application is not in the foreground, and it requires special permissions on macOS. Are you sure that this is what you really need? Perhaps you need to handle keyboard events only when your application is in the foreground, or you need to handle the Enter key only when a button is in focus - and for that you should use MAUI-specific APIs.
If you really do need a global hook, then here's a basic usage example.
To be honest, I don't have any experience with MAUI, but the basic idea is that you create an instance of
IReactiveGlobalHook(e.g.Tas…