Blazor 监听键盘输入,并显示!
我希望能够在不使用 Blazor 中的 HTML INPUT 标记的情况下捕获键盘输入。
@page "/test" <table @ref="testRef" tabindex="0" @onkeydown="HandleKeyDown"> <thead> <tr> <th> Pressed Key </th> </tr> </thead> <tbody> <tr> <td> @pressedKey </td> </tr> </tbody> </table> <h1 @ref="testRef" tabindex="0" @onkeydown="HandleKeyDown">@pressedKey</h1> @code { 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(); } } }