Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 - handle two events at the same time

I want to handle a click event + CTRL keypress event in the same function in my component.ts file, but I don't seem to find a solution anywhere. I've tried:

@HostListener("click")
    onDropdownItemClick( evt: keyboardEvent) {
        console.log(evt.keyCode);
    }

but it just returns "ERROR TypeError: Cannot read property 'keyCode' of undefined"

I've also tried this too:

@Component({
        selector: '....',
        templateUrl: '....',
        host: {
            '(window:keydown)': 'findKey($event)',
            '(window:mousedown)': 'findKey($event)'
        }
   });

findKey(event) {
         if(event.ctrlKey && event.which === 1){
               console.log("CTRL + mouse-click");
        }
    }

But it's not working either. Anyone have suggestions on how to catch both events at the same time?

like image 262
Vluiz Avatar asked Oct 11 '17 05:10

Vluiz


People also ask

Can we use two events together in Angular?

Turns out, you totally can. Run this demo in my JavaScript Demos project on GitHub. In Angular 1.

What does $event mean in Angular?

The $event object often contains information the method needs, such as a user's name or an image URL. The target event determines the shape of the $event object. If the target event is a native DOM element event, then $event is a DOM event object, with properties such as target and target.

What type is $event Angular?

Angular includes $event that contains the information about an event. The type of $event depends on the target event, e.g., if the target event is a native DOM element event, then it is an object. A component should define the onShow(event) method where the type of the parameter can be KeyboardEvent, MouseEvent, etc.

What is event binding in Angular?

In Angular 8, event binding is used to handle the events raised by the user actions like button click, mouse movement, keystrokes, etc. When the DOM event happens at an element(e.g. click, keydown, keyup), it calls the specified method in the particular component.


1 Answers

The MouseEvent event provides a ctrlKey property that allows to read the state of the Ctrl key state when the click happened

@HostListener("click", ['$event'])
onDropdownItemClick( evt: MouseEvent) {
  console.log('clicked - with ctrl pressed:', evt.ctrlKey);
}

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/ctrlKey

Just store the status of the control key in a field and read it in the click event:

ctrlDown = false;

@HostListener('window:keydown.ctrl')
onCtrlDown() {this.ctrlDown = true; }

@HostListener('window:keydown.ctrl')
onCtrlUp() {this.ctrlDown = false; }

@HostListener("click")
onDropdownItemClick( evt: keyboardEvent) {
  console.log('clicked - with ctrl pressed:', this.ctrlDown);
}

If above doesn't work please try

@HostListener('window:keydown', ['$event'])
onCtrlDown(event) {this.ctrlDown = event.ctrlKey; }

@HostListener('window:keydown')
onCtrlUp() {this.ctrlDown = false; }

like image 84
Günter Zöchbauer Avatar answered Oct 13 '22 21:10

Günter Zöchbauer