KeyCode of a key pressed in Lightning Web Component

There will be a scenario where you need to identify if Enter is presses or any special character is pressed. Below code will demonstrate how to identify a keypress code. In Lightning Web Component.

For this scenario we need to add a span section with onkeypress event above the input text field. And call a JS method. Something like this.


    <span onkeypress={key_Code_Checker}>
        <lightning-input id="a_Name_Id" 
                         label="Enter Name: " 


import { LightningElement, track } from 'lwc';

export default class Lwc_Key_Code_Check extends LightningElement {

    @track a_Text_Value;

    key_Code_Checker(component, event, helper){
        if (component.which == 13){
            // JS code follows.
            var a_Name = this.a_Text_Value;
            alert('Entered name is: '+a_Name);

        var a_Name =;
        this.a_Text_Value = a_Name;


<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="">


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s