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.

Example
Click here and press any key to get the event keycode info.
  • 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" />
Example
KeyDown and KeyUpKeyDown only Stop RepeatAllow Repeat
Press any key to get the keycode info.
Download: 

Documentation

FluentKeyCode Class

Parameters

Name
Type
Default
Description
Anchorstring
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..
ChildContentRenderFragment?
Gets or sets the content to be managed by the KeyCode engine.
GlobalDocumentbool
False
Gets or sets whether the KeyCode engine is global (using document DOM element) or not (only for FluentKeyCode.Anchor or FluentKeyCode.ChildContent).
IgnoreKeyCode[]
Gets or sets the list of KeyCode to ignore when evaluating the key code.
IgnoreModifierbool
True
Ignore modifier keys (Shift, Alt, Ctrl, Meta) when evaluating the key code.
OnlyKeyCode[]
Gets or sets the list of KeyCode to accept, and only this list, when evaluating the key code.
PreventDefaultbool
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.
PreventDefaultOnlyKeyCode[]
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.
StopPropagationbool
False
Gets or sets a way to prevent further propagation of the current event in the capturing and bubbling phases.
StopRepeatbool
False
Gets or sets whether the key pressed can be repeated.

EventCallbacks

Name
Type
Description
OnKeyDownEventCallback<FluentKeyCodeEventArgs>
Event triggered when a KeyDown event is raised.
OnKeyUpEventCallback<FluentKeyCodeEventArgs>
Event triggered when a KeyUp event is raised.

Methods

Name
Parameters
Type
Description
DisposeAsyncValueTask
OnKeyDownRaisedAsyncint keyCode
string value
bool ctrlKey
bool shiftKey
bool altKey
bool metaKey
int location
string targetId
bool repeat
Task
Internal method.
OnKeyUpRaisedAsyncint 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
AltKeybool
False
Gets a boolean value that indicates if the Alt key was pressed
CtrlKeybool
False
Gets a boolean value that indicates if the Control key was pressed
KeyKeyCode
Unknown
Gets the FluentKeyCodeEventArgs.KeyCode equivalent value.
KeyCodeint
0
Gets the system- and implementation-dependent numerical code identifying the unmodified value of the pressed key.
LocationKeyLocation
Standard
Gets an KeyLocation representing the location of the key on the keyboard or other input device.
MetaKeybool
False
Gets a boolean value that indicates if the Meta key was pressed
Namestring
Gets the name of the event ('keydown' or 'keyup').
Repeatbool
False
Gets a boolean value that is true if the given key is being held down such that it is automatically repeating.
ShiftKeybool
False
Gets a boolean value that indicates if the Shift key was pressed
TargetIdstring
Gets the identifier of the targeted DOM element.
Valuestring
Gets the value of the key pressed by the user

FluentKeyCodeProvider Class

Parameters

Name
Type
Default
Description
PreventDefaultbool
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.
Unknown
Cancel
Backspace
Tab
Clear
Enter
Shift
Ctrl
Alt
PauseBreak
CapsLock
Escape
Henkan
Muhenkan
Space
PageUp
PageDown
End
Home
Left
Up
Right
Down
Select
Print
Execute
F13
Insert
Delete
Help
Digit0
Digit1
Digit2
Digit3
Digit4
Digit5
Digit6
Digit7
Digit8
Digit9
Period
Semicolon
Backquote
Equal
Minus
KeyA
KeyB
KeyC
KeyD
KeyE
KeyF
KeyG
KeyH
KeyI
KeyJ
KeyK
KeyL
KeyM
KeyN
KeyO
KeyP
KeyQ
KeyR
KeyS
KeyT
KeyU
KeyV
KeyW
KeyX
KeyY
KeyZ
Command
WindowsLeft
WindowsRight
ContextMenu
Standby
Numpad0
Numpad1
Numpad2
Numpad3
Numpad4
Numpad5
Numpad6
Numpad7
Numpad8
Numpad9
NumpadMultiply
NumpadAdd
NumpadDecimal
NumpadSubtract
NumpadDecimal2
NumpadDivide
Function1
Function2
Function3
Function4
Function5
Function6
Function7
Function8
Function9
Function10
Function11
Function12
Function13
Function14
Function15
Function16
Function17
Function18
Function19
Function20
Function21
Function22
Function23
Function24
Function25
Function26
Function27
Function28
Function29
Function30
Function31
Function32
NumLock
ScrollLock
BracketLeft2
BracketRight2
Backquote2
Backslash2
Quote
AudioVolumeDown
AudioVolumeUp
MediaTrackNext
MediaTrackPrevious
MediaStop
MediaPlayPause
LaunchMail
AudioVolumeMute
MyComputer
AudioVolumeDownFF
MyCalculator
AudioVolumeUpFF
Semicolon2
Equal2
Comma
Minus2
Period2
Slash
IntlBackslash
IntlRo
NumpadComma
BracketLeft
Backslash
BracketRight
SimpleQuote
Backquote3
Meta
AltGraph
Standard
Left
Right
NumPad
Mobile
Joystick
Unknown
An error has occurred. This application may no longer respond until reloaded. Reload 🗙