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
102 stars 7 forks source link

Disable not working #25

Open Int32Overflow opened 4 months ago

Int32Overflow commented 4 months ago

I would like to activate the HotKeys only for a specific TabPage. To do this, I have used a HotKeyEntryState for several HotKeys. When switching tabs, the "Disabled" property is set correctly, but the HotKeys still trigger (on the hidden tab)

private readonly HotKeyEntryState overviewTabHotKeyState = new HotKeyEntryState();
protected override async Task OnAfterRenderAsync(bool firstRender)
{
    await base.OnAfterRenderAsync(firstRender);

    if (firstRender)
    {
        hotKeysContext = this.HotKeys.CreateContext()
            .Add(ModCode.Ctrl, Code.F, ShowSearch, new() { Description = "Show search bar", Exclude = Exclude.InputNonText | Exclude.TextArea, State = overviewTabHotKeyState })
            .Add(ModCode.None, Code.F5, ReloadTable, new() { Description = "Reload table", Exclude = Exclude.InputNonText | Exclude.TextArea, State = overviewTabHotKeyState });
    }
}

private async Task DoActivePanelIndexChanged(int newIndex)
{
    if (this.selectedTabIndex != newIndex)
    {
        this.selectedTabIndex = newIndex;
        if (this.selectedTabIndex == 0)
        {
            overviewTabHotKeyState.Disabled = false;
        }
        else
        {
            overviewTabHotKeyState.Disabled = true;
        }
    }
}
jsakamoto commented 4 months ago

Hi @Int32Overflow,

Unfortunately, I've still not succeeded in reproducing the problem that you reported. Could you provide me with a whole of a minimal sample project that can reproduce the issue?

By the way, as far as I can read the code fragment you posted above, once the "Disabled" property of the state object is set to true, the browser's default keyboard shortcuts will be available. For example, hitting Ctrl+F will open the search box built into the web browser. Is that your intentional design?

geralt18 commented 4 months ago

Hi @jsakamoto ,

I attach sample project. On Index page you can use "?" (question mark) to show help window and I also set State.Disable to true. But you can press F1, F2 or F3 and you can see that Shortcuts are working because app navigates to new Urls. Please don't pay attention to this popup window - I'm new to Blazor.

I hope that it will help you diagnose that problem.

BlazorHotKeysTest.zip

Int32Overflow commented 4 months ago

@geralt18 Sorry for the late reply. Thank you very much for your time and effort. I also tried it out in a small project and it seems to work here too. Now I have to look for the error again in my large project... I will reply again.

Int32Overflow commented 4 months ago

@geralt18 If I use the same HotKeyEntryState for multiple hotkeys, disabled is not working as expected. Is that by design? HotKey-demo

HotKeyTest.zip