Is it possible to find out which key was pressed when using (keypress) in Angular 2?
E.g.
<input type=text (keypress)=eventHandler()/> public eventHandler() { //Some code console.log(keyPressed); }
Edit: Seems my naming conventions were a bit off. I did not mean AngularJS 2, I meant Angular 2.0 with typescript.
The Windows on-screen keyboard is a program included in Windows that shows an on-screen keyboard to test modifier keys and other special keys. For example, when pressing the Alt , Ctrl , or Shift key, the On-Screen Keyboard highlights the keys as pressed.
Keycode 13 is the Enter key.
The keypress event is fired when a key that produces a character value is pressed down. Examples of keys that produce a character value are alphabetic, numeric, and punctuation keys. Examples of keys that don't produce a character value are modifier keys such as Alt , Shift , Ctrl , or Meta .
The keypress ignores keys such as delete , arrows , page up , page down , home , end , ctrl , alt , shift , esc , etc. So, if we need to handle those keys, it's better to use either keydown or keyup event. }); The keydown and keypress events fire multiple times if user press and hold a key.
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) {...}
@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)">
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With