Unplanned
Last Updated: 17 Apr 2025 08:39 by ADMIN
Stefan
Created on: 08 Apr 2025 15:08
Category: TextBox
Type: Feature Request
4
Add OnKeyDown Event in Inputs

We have forms that starts with single text box input, all other inputs are either hidden or disabled. Based on the value of that input we load additional data from the server, show/enable the rest of the inputs and move the focus to the next unput. We react to the value in that first input either when the Enter key is pressed, or when the input loses focus e.g. tab-out of the input or click/tap on another input. One or the other but never both. This scenario should be handled by the OnChange event. However, the OnChange event fires twice, when the enter key is pressed and again when the text box loses focus. This results in double data retrieval (takes twice the time), screen flicker and creates unpleasant user experience.

The recommended workaround is to add second variable to track if the input actually changed and ignore consequent events for the same value. This has it’s own caveat. If for any reason the user wants to retrieve the related data from server again e.g. he was notified it has changed, then he cannot use the OnChange event anymore because the value hasn’t changed and the event will be ignored. So the user has to change the value twice.

 We found a second workaround, see code below. Blur the text box on enter and handle only the on OnBlur event to process the value. This way we can process the value repeatedly, the user presses enter or tab-out, then focus back on the same input and hit enter or tab-out and repeat as many times as desired.

 We propose OnEnter event to be added to the text box so we don’t have to handle @onkeypress on outer <div>. The <div> events are also not synchronized with the DebounceDelay parameter of the text box. We have to either set the DebounceDelay to 0 or introduce delay in the KeyHandler method. The text box has OnBlur event already but surprisingly there is no OnEnter event.

We also proposed BlurAsync() method that will eliminate the need to create the javascript function and invoke JSRuntime.

 <<< javascript function >>>
function blurInput(inputId) {

    const textField = document.getElementById(inputId);

    if (textField) {

        textField.blur();

    }

}

<<< Blazor >>>
@page "/"
 
@inject IJSRuntime JSRuntime

<PageTitle>Home</PageTitle>

<div @onkeydown="@(async (args) => await KeyHandler(args))">
    <TelerikTextBox id="txt1" Width="20rem" Placeholder="Pallet" DebounceDelay="0" @bind-Value="@_textInput" OnBlur="@OnInputBlur" />

</div>

<div>

    <TelerikTextBox @ref="_txtRef" Width="20rem" Placeholder="Pallet" />

</div>

@code
{

    private string? _textInput;

    private TelerikTextBox _txtRef = null!;

    private async Task KeyHandler(KeyboardEventArgs e)
    {

        if (e.Key == "Enter")

        {

            await JSRuntime.InvokeVoidAsync("blurInput", "txt1");

        }

    }

    private async Task OnInputBlur()
    {

        Console.WriteLine($"Bound variable: {_textInput}");

        await _txtRef.FocusAsync();

    }

}

}

5 comments
ADMIN
Hristian Stefanov
Posted on: 17 Apr 2025 08:39

Hi Stefan,

I can confirm that this specific item refers to the OnKeyDown event in input components.

As for the BlurAsync method, I’ve gone ahead and submitted a separate feature request on your behalf: Add BlurAsync Method.

Regarding the DebounceDelay, it’s already being considered, as there’s an existing item for it—just as you pointed out.

Regards,
Hristian Stefanov
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Stefan
Posted on: 16 Apr 2025 13:26

I agree, OnKeyDown would be better than OnEnter. You are also adding the BlurAsync(), right?

Couple other things I want to point out.
Please, consider the DebounceDelay when firing events, like in the following construct. The OnKeyDown, OnChange, OnBlur, etc. should fire after the DebounceDelay has passed. There is another feature request 'Make the OnChange event receive the immediate value when the DebounceDelay is greater than 0'.
if (DebounceDelay == 0)
{ ... }
else
{
    _debouncer.Debounce(() =>
        { return this.InvokeAsync(() => { ... }); }
, DebounceDelay);
}


I see the following construct on few places in Telerik's code. Looks like a good idea and could be more efficient to use for all events.
if(OnBlur.HasDelegate)
{
    _ = OnBlur.InvokeAsync(null);
}

ADMIN
Hristian Stefanov
Posted on: 16 Apr 2025 10:33

Hi Stefan,

In that case, would it work for you if we exposed an OnKeyDown event that fires specifically on key press and provides the ability to detect which key was pressed?

After discussing it with the team, we believe this approach would be more efficient and flexible, covering a wider range of scenarios.

Let me know your thoughts.

Regards,
Hristian Stefanov
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!

Stefan
Posted on: 15 Apr 2025 15:57

I'm not sure I understand. How would having such a parameter address our scenario?
Are you thinking we process only when OnChange fires from pressing Enter but ignore the Blur. What if the barcode is damaged and doesn't scan? The user may type the text by hand and tab-out. We cannot force the users to use only Enter and never tab-out.

The OnChange event doesn't fire twice on blur. OnChange fires twice only if there is Enter followed either by clicking outside, tab-out or focus change. The parameter you are proposing will have 'Enter' the first time and 'Blur' the second, but OnChange will still fire twice.

ADMIN
Hristian Stefanov
Posted on: 15 Apr 2025 10:41

Hi Stefan,

We discussed your situation with the team, and our proposal is to introduce a parameter in the OnChange event that indicates what triggered it—whether it was pressing Enter or clicking outside. Since this would address your scenario, would this solution work for you?

I eagerly anticipate hearing back from you.

Regards,
Hristian Stefanov
Progress Telerik

Enjoyed our products? Share your experience on G2 and receive a $25 Amazon gift card for a limited time!