5

Suppouse we have this template:

<div class="myClass" (click)="doSomething($event)">

soSomething is not called on a middle click. How can I solve this problem?

Artur Stary
  • 690
  • 1
  • 13
  • 30

2 Answers2

19

I solved this problem by using a small custom middleclick directive which is listening to the mouseup event because in my example (on macOS) the click HostListener is not fired on a middle click.

import { Directive, Output, EventEmitter, HostListener } from '@angular/core';

@Directive({ selector: '[middleclick]' })
export class MiddleclickDirective  {
  @Output('middleclick') middleclick = new EventEmitter();

  constructor() {}

  @HostListener('mouseup', ['$event'])
  middleclickEvent(event) {
    if (event.which === 2) {
      this.middleclick.emit(event);
    }
  }
}

With this you can use something like

<button (middleclick)="doSomething()">Do something</button>
thegnuu
  • 527
  • 4
  • 9
2

This should work

<div class="myClass" (click)="$event.which == 2 ? doSomething($event) : null">

See also Triggering onclick event using middle click

update 2/2018

When I posted above answer I was working on a Linux machine and it worked for me.

Now I'm on a Mac and I'm not able to get a click event for anything else than the left mouse button.

You can try it yourself using

StackBlitz example

See also

Günter Zöchbauer
  • 558,509
  • 191
  • 1,911
  • 1,506