Multiple key binding on (keydown) event in angular 6

11,304

You need to remove the shift in the if condition like this:

public onKeydownMain(event): void {
       if (!event.shiftKey && (event.key === "Enter" || event.key === "Tab")){
                event.preventDefault();
                   // My Functionality goes here
        }
    }

You can refer to https://www.w3schools.com/jsref/obj_keyboardevent.asp for KeyboardEvent

Share:
11,304
Parshuram Kalvikatte
Author by

Parshuram Kalvikatte

I love to solve problems. Subscribe My Youtube Page - https://www.youtube.com/channel/UCvtQocKeX_8SF6RDnhorrGQ?sub_confirmation=1 Or Mail Me at [email protected]

Updated on June 29, 2022

Comments

  • Parshuram Kalvikatte
    Parshuram Kalvikatte almost 2 years

    I have used (keydown) event in angular 6 to bind key inputs

    So on enter of 'Tab' and 'Enter' key i have to do my functionality and added a event.prevent default

    But when Shift + Tab is entered it is also preventing the event

    .html

    <input type="text" [(ngModel)]="Result1 (keydown)="onKeydownMain($event)"  >
    

    I dont want (keydown.shift.tab) event seperate..

    .ts

    public onKeydownMain(event): void {
        if (event.key === "Enter" || event.key === "Tab") {
                    event.preventDefault();
                       // My Functionality goes here
        }
    }
    

    But the problem is when Shift + Tab is pressed , event is fired and functionality goes on, which i don't want.

    How can i go this

    I just want Shift + Tab to perform its default functionality on this input.

  • Parshuram Kalvikatte
    Parshuram Kalvikatte over 5 years
    Thanks,Sometimes finding simple things we think a lot..That works thanks..
  • Dharman
    Dharman over 5 years
    When providing an answer please explain why this is the best approach. Answers which just tell the OP to perform some action will not teach them anything. Use links to online documentation to support your claims (e.g. MDN).
  • JFPicard
    JFPicard over 5 years
    You only get a keyCode with the event. So shiftKey, altKey and ctrlKey is the way to find if a control key was pressed.