Skip to content

KeyInterceptorService: Add option to ignore repeat events when holding down keys#12376

Merged
ScarletKuro merged 3 commits intoMudBlazor:devfrom
JMolenkamp:feature/add-key-interception-ignore-repeat-option
Jan 2, 2026
Merged

KeyInterceptorService: Add option to ignore repeat events when holding down keys#12376
ScarletKuro merged 3 commits intoMudBlazor:devfrom
JMolenkamp:feature/add-key-interception-ignore-repeat-option

Conversation

@JMolenkamp
Copy link
Contributor

@JMolenkamp JMolenkamp commented Jan 1, 2026

Add KeyOptions.IgnoreDownRepeats to prevent from invoking the .NET callback when the subscriber is only interested in the initial keydown event and not in the repeat stream from holding down the key.

Usage example:

<MudStack id="@_elementId">
    <MudButton Class="@_className" Variant="Variant.Outlined">Click Me</MudButton>
    <MudText Class="d-flex">
        <MudIcon Icon="@Icons.Material.Filled.ArrowCircleLeft" Class="mr-1" />
        @_keyDownCountLeft
    </MudText>
    <MudText Class="d-flex">
        <MudIcon Icon="@Icons.Material.Filled.ArrowCircleRight" Class="mr-1" />
        @_keyDownCountRight
    </MudText>
</MudStack>

@code {
    private readonly string _elementId = Identifier.Create();
    private readonly string _className = Identifier.Create();

    private int _keyDownCountLeft = 0;
    private int _keyDownCountRight = 0;

    [Inject]
    public IKeyInterceptorService KeyInterceptorService { get; set; } = null!;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (!firstRender)
        {
            return;
        }

        var options = new KeyInterceptorOptions()
        {
            TargetClass = _className,
            EnableLogging = false,
            Keys = [
                new KeyOptions("ArrowLeft", subscribeDown: true),
                new KeyOptions("ArrowRight", subscribeDown: true, ignoreDownRepeats: true),
            ]
        };

        await KeyInterceptorService.SubscribeAsync(_elementId, options, HandleKeyDownAsync);
    }

    private Task HandleKeyDownAsync(KeyboardEventArgs args)
    {
        switch (args.Key)
        {
            case "ArrowLeft":
                _keyDownCountLeft++;
                StateHasChanged();
                break;

            case "ArrowRight":
                _keyDownCountRight++;
                StateHasChanged();
                break;
        }

        return Task.CompletedTask;
    }

Checklist:

  • I've read the contribution guidelines
  • My code follows the style of this project
  • I've added or updated relevant unit tests
    I could not find any JS testing, are those omitted?
    Should a sample be created in MudBlazor.UnitTests.Viewer to at least be able to see it in action?

…net callback for repeated event when holding down a key
@mudbot mudbot bot added the enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library label Jan 1, 2026
Changed the default value of IgnoreDownRepeats to be unset.
@ScarletKuro
Copy link
Member

ScarletKuro commented Jan 1, 2026

  • I could not find any JS testing, are those omitted?

Sadly, we don't have JS unit tests like Jest, Jasmine, Playwright etc.
All you can do is to mock the JS InvokeAsync calls in bUnit, but this method isn't called directly, so... nothing u can do.

  • Should a sample be created in MudBlazor.UnitTests.Viewer to at least be able to see it in action?

Yes, that would be ideal.

@ScarletKuro
Copy link
Member

LGTM, just add MudBlazor.UnitTests.Viewer too see in action, and I will merge.

@mudbot mudbot bot added the needs: example A usage example is absent (reproduction link or code snippet) - logic is described but not shown label Jan 2, 2026
@JMolenkamp
Copy link
Contributor Author

Added the sample in the Services folder.

Do you perhaps have some tips on how to develop in this repository?
Building takes quite some time on my system, making it very time-consuming to even test some small changes.

@ScarletKuro
Copy link
Member

Do you perhaps have some tips on how to develop in this repository?
Building takes quite some time on my system, making it very time-consuming to even test some small changes.

Unfortunately there’s no easy way around the build time without more powerful hardware. One possible workaround is to skip parts of the build (like JS, CSS, or docs) if you only care about core changes, but currently no built-in way to disable parts of the build.

@ScarletKuro ScarletKuro merged commit 6e87046 into MudBlazor:dev Jan 2, 2026
5 of 6 checks passed
@JMolenkamp JMolenkamp deleted the feature/add-key-interception-ignore-repeat-option branch January 4, 2026 09:53
This was referenced Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement Adds a new feature or enhances existing functionality (not fixing a defect) in the main library needs: example A usage example is absent (reproduction link or code snippet) - logic is described but not shown

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants