Home > OS >  How to capture KeyboardEvent "Ctrl C" but not KeyboardEvent "C"?
How to capture KeyboardEvent "Ctrl C" but not KeyboardEvent "C"?

Time:08-01

I have a @HostListener that listens to document:keydown events like the following

@HostListener('document:keydown', ['$event'])
selectHandler(event: KeyboardEvent) {
  switch (event.key) {
    case 'n':
      this.useNode();
      break;
    case 'c':
      this.useConnector();
      break;
    case 'd':
      this.useDefault();
      break;
    case 'g':
      this.useGrip();
      break;
    default:
      break;
  }
}

The problem is that I also have a handler for Copy operation which is achieved using Ctrl C, and when I press Ctrl C my object gets copied, but this also triggers case 'c' in my code.

The question is how do I detect pressing "Ctrl C" alone but not "C"?

CodePudding user response:

The KeyboardEvent contains the property KeyboardEvent.ctrlKey. More details can be found here.

  • Related