Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect key press without using an input tag in Blazor

I want to be able to capture keyboard input without using an HTML INPUT tag in Blazor. Once the key is pressed i will display a graphic to represent the letter pressed.

Something like this

@page "/counter"
@using Microsoft.AspNetCore.Components.Web

<div @onkeypress="e => KeyPress(e)">
    Press any letter key
</div>

@code {

    private void KeyPress(KeyboardEventArgs e)
    {
        var letter = e.Key;
    }
}

The KeyPress method does not appear to be called when I set a breakpoint on it. Any help much appreciated.

like image 254
Mark Lawson Avatar asked Nov 18 '19 18:11

Mark Lawson


People also ask

How do you detect keypress in Blazor?

To detect a browser keypress event, set the @onkeypress event to the corresponding element. For hotkey functions, you can use the event arguments to check whether the Ctrl, Shift, or Alt key is pressed.

Is event a keypress?

The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .

How to detect keypress in Python?

To detect keypress in python, we can use the keyboard module. It works on both Windows and Linux operating systems and supports all the hotkeys. You can install the keyboard module in your machine using PIP as follows. pip install keyboard To detect keypress, we will use the is_pressed()function defined in the keyboard module.

How do I create forms and validate fields in Blazor?

Blazor comes with everything needed to create forms and validate them. You can create a form and validate fields using data annotations. While this works well, the validation occurs when the input loses the focus:

Why can’t I get a keypress event in all browsers?

For non-printable keys such as arrow keys and shortcut keys such as Ctrl-z, Ctrl-x, Ctrl-c that may trigger some action in the browser (for instance, inside editable documents or elements), you may not get a keypress event in all browsers.

How to prevent textbox value mismatch in onkeypress and valuechanged event handlers?

Optionally, you can set the DebounceDelay parameter of the textbox to 0. This can prevent textbox value mismatch in the onkeypress and ValueChanged event handlers. Such mismatch can occur due to the default DebounceDelay value. The same approach can work for capturing events from the Editor component when it is in its Div mode.


1 Answers

If there is still someone who want the solution. I think now in .NET 5 you can achieve this in Blazor without js. To set focus and tabindex is important, when you lost focus, or set focus to another element this will not work. This works for me:

    <table @ref="testRef" tabindex="0" @onkeydown="HandleKeyDown">
    <thead>
        <tr>
            <th>
                Pressed Key
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                @pressedKey
            </td>
        </tr>
    </tbody>
    </table>

    private ElementReference testRef;
    private string pressedKey;
    
    private void HandleKeyDown(KeyboardEventArgs e)
    {
        pressedKey = e.Key;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await testRef.FocusAsync();
        }
    }
like image 172
hlidac22 Avatar answered Oct 25 '22 11:10

hlidac22