npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ngx-keyevent

v3.0.4

Published

Angular directive and service for detecting key events

Readme

⌨️ ngx-keyevent

npm npm npm npm

ngx-keyevent is a simple Angular directive / service that can be used on any HTML element, or injected to listen for user's key presses.

Demo

A demo is available at here.

Compatibility

| Angular | ngx-keyevent | | -----------| ----------- | | 10 | ^v1.x.x | | 11 | ^v1.x.x | | 12 | ^v2.x.x | | 13 | ^v3.x.x |

Usage

npm install ngx-keyevent --save

Import NgxKeyeventModule in your app.module:

import { NgxKeyeventModule } from 'ngx-keyevent';

@NgModule({
  declarations: [
    AppComponent
  ],

  imports: [
    NgxKeyeventModule
  ],

  bootstrap: [AppComponent]
})
export class AppModule { }

Example

Listen for key events in MyComponent using ngx-keyevent directive.

// my-component.component.ts
@Component({
  selector: 'my-component',
  templateUrl: 'my-component.component.html',
})
export class MyComponent {
  constructor() {}

  onKeyEvent(event: KeyboardEvent) {
    if (event.key === 'Enter') {
      // ... Do stuff when Enter key is pressed
    }
  }
}
<!-- my-component.component.html -->
<div (ngxKeyEvent)="onKeyEvent($event)"></div>

Or, listen for key events in MyComponent by injecting the ngx-keyevent service:

@Component({
  selector: 'my-component',
  templateUrl: 'my-component.component.html',
})
export class MyComponent implements OnInit {
  
  constructor(private keyService: NgxKeyeventService) {}

  ngOnInit(): void {
    this.keyService.keyEventChanges().subscribe((keyEvent: KeyboardEvent) => {
      console.log('Just pressed: ', keyEvent);
    });
  }
}

The output type is KeyboardEvent.

Dialogs example: Closing a Material Dialog

We often use Material Dialog to request data from users. In the example below, we will use ngx-keyevent to close a Material dialog with no data by Escape key, and grab the data with the Enter key.

Dialog component:

import { Component, Inject } from "@angular/core";
import { MatDialogRef, MAT_DIALOG_DATA } from "@angular/material/dialog";

@Component({
  selector: 'dialog-overview-example-dialog',
  templateUrl: 'dialog.component.html',
})
export class DialogOverviewExampleDialog {

  userData: any; // Data collected from the user via form, or whatever.

  constructor(
    public dialogRef: MatDialogRef<DialogOverviewExampleDialog>,
    @Inject(MAT_DIALOG_DATA) public data: any,
  ) {}

  closeDialog(hasData: boolean): void {
    this.dialogRef.close(hasData ? this.userData : undefined);
  }

  onKeyPress(event: KeyboardEvent) {
    if (event.key === 'Escape') {
      this.closeDialog(false);
    } else if (event.key === 'Enter') {
      this.closeDialog(true);
    }
  }
}
<!-- dialog.component.html -->
<div (ngxKeyEvent)="onKeyPress($event)">
  <h1 mat-dialog-title>Hello, I am a dialog.</h1>
  <div mat-dialog-content>
    <!-- Get the use data via forms... -->
  </div>
</div>

App component:

import { Component, Inject } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { DialogOverviewExampleDialog } from './dialog/dialog.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {

  keyEvents: KeyboardEvent[] = [];

  constructor(public dialog: MatDialog) {
  }

  openDialog(): void {
    const dialogRef = this.dialog.open(DialogOverviewExampleDialog, {
      width: '450px',
      disableClose: true
    });
    dialogRef.afterClosed().subscribe((data) => {
      // Do whatever with the data
    })
  }
}