jsakamoto / Toolbelt.Blazor.HotKeys2

This is a class library that provides configuration-centric keyboard shortcuts for your Blazor apps.
https://jsakamoto.github.io/Toolbelt.Blazor.HotKeys2/
Mozilla Public License 2.0
87 stars 6 forks source link

Example not working #10

Open Martin-Andersen opened 1 year ago

Martin-Andersen commented 1 year ago

In the old version of HotKeys. I wrote this

HotKeysContext = HotKeys.CreateContext()
                .Add(ModKeys.Ctrl, Keys.K, SetFocusInGridSearchField, "Set focus in grid search field");

I tried this in v2, not working

HotKeysContext = HotKeys.CreateContext()
            .Add(ModCode.Ctrl,Key.k, SetFocusInGridSearchField, "Set focus in grid search field");

And this not working

HotKeysContext = HotKeys.CreateContext()
            .Add(ModCode.Ctrl,Key.k, "Set focus in grid search field", _ => SetFocusInGridSearchField);

Is it because my handler is async?

private async Task SetFocusInGridSearchField()
    {
        await JsRuntime.InvokeVoidAsync("focusElement", GridSearchBoxPortfolioListGrid);
    }
Martin-Andersen commented 1 year ago

Better headline

jsakamoto commented 1 year ago

@Martin-Andersen Currently, it is intentionally by design.

See also: https://github.com/jsakamoto/Toolbelt.Blazor.HotKeys2/issues/7#issuecomment-1372142048

Pinox commented 10 months ago

This also tripped me up a bit as the errors in Visual Studio are not that great.

Would be a great idea to add a note in the sample to the async method exception.

wokket commented 5 months ago

Just a +1 here. Had an async Task method called on button click, tried to wire up a shortcut to call the same method per the sample, it can't resolve the method, and I get a thousand other overload options to scroll through.

My understanding of ValueTask is it's only of performance improvement if the underlyng async operation completes synchronously...They also have a drastically reduced API surface area (by design). See https://devblogs.microsoft.com/dotnet/understanding-the-whys-whats-and-whens-of-valuetask/

jsakamoto commented 5 months ago

Hi everyone, I've changed my opinion that I don't provide the Add() methods of overloaded version for async Task because so many developers were confused by my decision.

So, the latest preview version of the HotKeys2 now starts to support async Task for its Add() method.

See also: https://www.nuget.org/packages/Toolbelt.Blazor.HotKeys2/3.3.0-preview.1#releasenotes-body-tab