Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FluentKeyCodeProvider] Add a global service to capture keystrokes #1740

Merged
merged 10 commits into from
Mar 26, 2024

Conversation

dvoituron
Copy link
Collaborator

Add a global service to capture keystrokes

Section

In some circumstances, Blazor does not retrieve all the KeyDown information received from JavaScript. FluentKeyCode retrieves this data, in a similar way to the JavaScript KeyCode library and to this demo sample.

The FluentKeyCode component extends the functionality of OnKeyDown by adding the KeyCode property when the OnKeyDown event is raised.

👉 Global

peek

You can also capture keystrokes globally on the current page. To do this, we provide you with a IKeyCodeService injected by the AddFluentUIComponents method. Add the following component at the end of your MainLayout.razor file.

<FluentKeyCodeProvider />

Once the provider and service have been injected, you can:

  • Retrieve the service and register the method that will capture the keys:

    [Inject]
    private IKeyCodeService KeyCodeService { get; set; }  // 👈
    
    protected override void OnInitialized()
    {
        KeyCodeService.RegisterListener(OnKeyDownAsync);  // 👈
    }
    
    public async Task OnKeyDownAsync(FluentKeyCodeEventArgs args) => { // ... }  // 👈
    
    public ValueTask DisposeAsync()
    {
        KeyCodeService.UnregisterListener(OnKeyDownAsync);
        return ValueTask.CompletedTask;
    }
  • Implement the interface IKeyCodeListener, retrieve the service and register the method that will capture the keys:

    public partial MyPage : IDisposableAsync, IKeyCodeListener  // 👈
    {
        [Inject]
        private IKeyCodeService KeyCodeService { get; set; }  // 👈
    
        protected override void OnInitialized()
        {
            KeyCodeService.RegisterListener(this);  // 👈
        }
    
        public async Task OnKeyDownAsync(FluentKeyCodeEventArgs args) => { // ... }  // 👈
    
        public ValueTask DisposeAsync()
        {
            KeyCodeService.UnregisterListener(this);
            return ValueTask.CompletedTask;
        }
    }

Some keystrokes are used by the browser (e.g. Ctrl + A). You can disable this function using the PreventDefault attribute with the FluentKeyCodeProvider component.

<FluentKeyCodeProvider PreventDefault="true" />

Unit Tests

  • FluentKeyCode: 100%
  • FluentKeyCodeEventArgs: 100%
  • FluentKeyCodeProvider: 45% (only a single method not tested, raised by JS: KeyDownHandler)
  • KeyCodeService: 95%

@dvoituron dvoituron requested a review from vnbaaij March 26, 2024 11:23
@vnbaaij vnbaaij merged commit b4dd38a into dev Mar 26, 2024
3 of 4 checks passed
@vnbaaij vnbaaij deleted the users/dvoituron/global-keycode branch March 26, 2024 14:06
vnbaaij added a commit that referenced this pull request Apr 29, 2024
…1740)

* Move KeyCode classes to a KeyCode folder

* Add first FluentKeyCodeProvider

* Update Interface

* Add sample and doc

* Update doc

* Add PreventDefault

* Add Unit Tests

---------

Co-authored-by: Vincent Baaij <vnbaaij@outlook.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants