Get which key pressed from (keypress) angular2
@Component({
selector: 'key-up3',
template: `
<input #box (keyup.enter)="onEnter(box.value)">
<p>{{value}}</p>
`
})
export class KeyUpComponent_v3 {
value = '';
onEnter(value: string) { this.value = value; }
}
Or use like this..
<input #box (keyup.enter)="onSubmit(form.value)">
<form [formGroup]="form" (ngSubmit)="onSubmit(form.value)">
Pass $event
to your event handler. The $event
is a DOM KeyboardEvent.
<input type=text (keypress)="eventHandler($event)">
eventHandler(event) {
console.log(event, event.keyCode, event.keyIdentifier);
}
If you know which KeyboardEvent property you want, you can pass that into your event handler:
<input type=text (keypress)="eventHandler($event.keyCode)">
eventHandler(keyCode) {...}