aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Keydown event on input gets cancelled

Open neerajbharti opened this issue 8 years ago • 7 comments

Created a blazor project from default template. Then modifed the index.cshtml as the below code. Then type something in the input box. The browser console prints the pressed key, but the keyboard event seems cancelled. Nothing gets printed in the input field. Also the arg.Key property gives me a string. Can I get a integer specifying as to which key was pressed (27 for escape key for example)

@page "/"
<input onkeydown="@HandleKeyDown" />
@functions{
    void HandleKeyDown(UIKeyboardEventArgs arg) {
        Console.WriteLine(arg.Key);
    }
}

neerajbharti avatar May 09 '18 14:05 neerajbharti

We should have a way to say that event was fully handled in our code or not. Of course the same is true for all other events.

Andrzej-W avatar May 11 '18 00:05 Andrzej-W

Thanks for logging this, @neerajbharti. We'll make sure to add arg.KeyCode or similar, and not to prevent the default action for the keydown event. Added to backlog.

We should have a way to say that event was fully handled in our code or not

Unfortunately that might not be possible in general. The only way it could be possible is if the event raising code is synchronous. But longer term one of our goals is to support running Blazor across a boundary such as in a WebWorker. In that case, all JS-->.NET calls have to be async (even if the .NET->JS ones can be synchronous).

SteveSandersonMS avatar May 11 '18 22:05 SteveSandersonMS

Let's assume I have an input field where I want to enter date in ISO format (YYYY-MM-DD). I want to attach my code to keyboard events and handle Plus and Minus keys to increment or decrement date. Those keys are fully handled in my code - I don't want to see something like this: 2018-05-12+++ in my input box after a few key press events. Will it be possible or not to implement similar functionality after the bug will be fixed?

Andrzej-W avatar May 11 '18 23:05 Andrzej-W

@Andrzej-W Ultimately it's always possible to implement low-level things like a special keyboard event override in JS and encapsulate that in a component. Hopefully it's also possible to do nicely purely within the Blazor .NET model but would have to try it to determine any rough spots.

SteveSandersonMS avatar May 12 '18 00:05 SteveSandersonMS

Make sure that after the fix this will still work

@page "/"

<div onclick="@ClickInDiv" onkeypress="@KeyPressInDiv" style="background-color:aqua;width:300px;height:100px">
    <input onclick="@ClickInInput" onkeypress="@KeyPressInInput" type="text"/>
</div>

@functions{
    int divClicks;
    int inputClicks;

    void ClickInDiv() => Console.WriteLine($"Click in div: { ++divClicks }");
    void ClickInInput() => Console.WriteLine($"Click in input: { ++inputClicks }");
    void KeyPressInDiv(UIKeyboardEventArgs ev) => Console.WriteLine($"KeyPress in div: { ev.Key }");
    void KeyPressInInput(UIKeyboardEventArgs ev) => Console.WriteLine($"KeyPress in input: { ev.Key }");
}

Currently it works as expected - I can handle event first in child component (HTML element) and then in parent component.

Andrzej-W avatar May 12 '18 12:05 Andrzej-W

Will we have this fixed in 0.4?

neerajbharti avatar May 28 '18 08:05 neerajbharti

@neerajbharti This isn't currently slated for 0.4. We'll look at it in a future release.

danroth27 avatar May 29 '18 18:05 danroth27