Search code examples
typescriptkeyboardinput-field

How to Disable Keyboard Keys Using Angular 2+. But, The keys should be work on HTML input fields


Is there any way to disable the keyboard keys using Angular 2+(Typescript). I want to disable all keys in keyboard. But, the keys should be work on HTML input fields.


Solution

  • Import this HostListener

     import { HostListener } from '@angular/core';
    

    Then, put this code to prevent keyboard keys..

      @HostListener('document:keydown', ['$event'])
      handleKeyboardEvent(event: KeyboardEvent) {
    
      console.log(event);
       event.returnValue = false;
       event.preventDefault();
    
       //or
       //do something
    
    }