4

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.

1
  • 1
    You'll have to be explicit and tell it to check if Shift is pressed. Commented Oct 5, 2018 at 12:40

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks,Sometimes finding simple things we think a lot..That works thanks..
0

Try avoiding the shift keyevent value 16 in your code. event.which is used to display the keyevent value.

alert(event.which);
event.preventDefault();
if (event.keyCode!=16 && (event.keyCode === 13 || event.keyCode === 9 )) {
    alert("Pressed enter");
}

2 Comments

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).
You only get a keyCode with the event. So shiftKey, altKey and ctrlKey is the way to find if a control key was pressed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.