KeyCode
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.
- Value:
- Key:
- Code:
- Meta:
- Location:
- TargetId:
- Repeat:
Download:
Global
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 : IKeyCodeListener, IDisposableAsync { [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" />
Press any key to get the keycode info.
Download:
Documentation
FluentKeyCode Class
Parameters
Name | Type | Default | Description |
---|---|---|---|
Anchor | string | Gets or sets the control identifier associated with the KeyCode engine. If not set, the KeyCode will be applied to the FluentKeyCode content: see FluentKeyCode.ChildContent .This attribute is ignored when the FluentKeyCode.ChildContent is used.. | |
ChildContent | RenderFragment? | Gets or sets the content to be managed by the KeyCode engine. | |
GlobalDocument | bool | False | Gets or sets whether the KeyCode engine is global (using document DOM element) or not (only for FluentKeyCode.Anchor or FluentKeyCode.ChildContent ). |
Ignore | KeyCode[] | Gets or sets the list of KeyCode to ignore when evaluating the key code. | |
IgnoreModifier | bool | True | Ignore modifier keys (Shift, Alt, Ctrl, Meta) when evaluating the key code. |
Only | KeyCode[] | Gets or sets the list of KeyCode to accept, and only this list, when evaluating the key code. | |
PreventDefault | bool | False | Gets or sets a way to tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be. |
PreventDefaultOnly | KeyCode[] | Gets or sets the list of KeyCode to tells the user agent that if the event does not get explicitly handled,its default action should not be taken as it normally would be. | |
StopPropagation | bool | False | Gets or sets a way to prevent further propagation of the current event in the capturing and bubbling phases. |
StopRepeat | bool | False | Gets or sets whether the key pressed can be repeated. |
EventCallbacks
Name | Type | Description |
---|---|---|
OnKeyDown | EventCallback<FluentKeyCodeEventArgs> | Event triggered when a KeyDown event is raised. |
OnKeyUp | EventCallback<FluentKeyCodeEventArgs> | Event triggered when a KeyUp event is raised. |
Methods
Name | Parameters | Type | Description |
---|---|---|---|
DisposeAsync | ValueTask | ||
OnKeyDownRaisedAsync | int keyCode string value bool ctrlKey bool shiftKey bool altKey bool metaKey int location string targetId bool repeat | Task | Internal method. |
OnKeyUpRaisedAsync | int keyCode string value bool ctrlKey bool shiftKey bool altKey bool metaKey int location string targetId bool repeat | Task | Internal method. |
FluentKeyCodeEventArgs Class
Properties
Name | Type | Default | Description |
---|---|---|---|
AltKey | bool | False | Gets a boolean value that indicates if the Alt key was pressed |
CtrlKey | bool | False | Gets a boolean value that indicates if the Control key was pressed |
Key | KeyCode | Unknown | Gets the FluentKeyCodeEventArgs.KeyCode equivalent value. |
KeyCode | int | 0 | Gets the system- and implementation-dependent numerical code identifying the unmodified value of the pressed key. |
Location | KeyLocation | Standard | Gets an KeyLocation representing the location of the key on the keyboard or other input device. |
MetaKey | bool | False | Gets a boolean value that indicates if the Meta key was pressed |
Name | string | Gets the name of the event ('keydown' or 'keyup'). | |
Repeat | bool | False | Gets a boolean value that is true if the given key is being held down such that it is automatically repeating. |
ShiftKey | bool | False | Gets a boolean value that indicates if the Shift key was pressed |
TargetId | string | Gets the identifier of the targeted DOM element. | |
Value | string | Gets the value of the key pressed by the user |
FluentKeyCodeProvider Class
Parameters
Name | Type | Default | Description |
---|---|---|---|
PreventDefault | bool | False | Gets or sets a way to tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be. |