I Have a character limit in my text area and when it reaches a verge of 999 out of 1000 i dont want any accent characters to be entered.
Im not able to capture the final output of combination key, so when only 1 characters is left i need to detect Alt gr key being pressed and prevent its action further as not proceed with that combination.
keyDownEvent(e){
if ( (charactersLeft === maxLength -1) && e.keyCode === 18) {
e.preventDefault();
}
}
The above is not working still the key combination happens and accent key(alt gr + a = ā) is being printed. How to stop this event.
Three separate events are triggerred onKeydown (Alt gr + a)
- ctrl key – altKey: false, ctrlKey: true, key: "Control", keyCode: 17
- alt key – altKey: false, ctrlKey: false, key: "AltGraph", keyCode: 18
- a key – altKey: false, ctrlKey: false, key: "a", keyCode: 65
3
Answers
You are checking only the
alt
key, but you also need to check thectrl
key.you may want to log the values of e.altKey and e.key to see what values they have when the keypress event is triggered.
You can use the below code, where we capture the input using the regex
/[^x00-x7F]/i
and remove the extra characters using the regex/[\x00-\x7F]*/i
, after the character limit!FULL CODE:
Stackblitz Demo