Issue
In angular i'm binding data with host listener, like below code
@HostListener('window:keyup', ['$event'])
onKeyUp(event: KeyboardEvent) {
if (event.keyCode === 13) {
this.onEnterClicked(event);
}
}
and in this code I want to replace 13 with something Keycode.ENTER.
but there is no class available which holds constants for each keys in angular and javascript.
so is there any class i'm missing? if not then why javascript or angular not defined constants at one place?
Solution
I think you want
if (event.key === KeyEventEnum.Enter) {
...
}
or
if (event.key === KeyEventEnum.Escape) {
...
}
or
if (event.key === KeyEventEnum.Shift) {
...
}
or
if (event.key === KeyEventEnum.Ctrl) {
...
}
instead of you are using like
if (event.key === "Enter") {
...
}
Angular doesn't have any constant or enum to hold all key code in one place but if you want any enum or constant then you please visit once to below link might be it help you. Its a angular package that you have to include it in your project.
https://github.com/nfriend/ts-keycode-enum
Answered By - er-sho
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.