jQuery’s ‘keypress’ doesn’t work for some keys in Chrome. How to work around?
Try handling keydown instead.
Try handling keydown instead.
Form approach As scoota269 says, you should use onSubmit instead, cause pressing enter on a textbox will most likey trigger a form submit (if inside a form) <form action=”#” onsubmit=”handle”> <input type=”text” name=”txt” /> </form> <script> function handle(e){ e.preventDefault(); // Otherwise the form will be submitted alert(“FORM WAS SUBMITTED”); } </script> Textbox approach If you … Read more
Set the IsDefault property on the button to true to enable the Enter key to activate that button’s action. There is also the IsCancel property that does the same thing for the Escape key.
You should use .keydown() because .keypress() will ignore “Arrows”, for catching the key type use e.which Press the result screen to focus (bottom right on fiddle screen) and then press arrow keys to see it work. Notes: .keypress() will never be fired with Shift, Esc, and Delete but .keydown() will. Actually .keypress() in some browser … Read more
You’ll have to use a setTimeout (like you are) but also store the reference so you can keep resetting the limit. Something like: // // $(‘#element’).donetyping(callback[, timeout=1000]) // Fires callback when a user has finished typing. This is determined by the time elapsed // since the last keystroke and timeout parameter or the blur event–whichever … Read more
Try it with a keydown or keyup event to capture the Esc key. In essence, you can replace document:keypress with document:keydown.escape: @HostListener(‘document:keydown.escape’, [‘$event’]) onKeydownHandler(event: KeyboardEvent) { console.log(event); }
You shouldn’t use the keypress event, but the keyup or keydown event because the keypress event is intended for real (printable) characters. keydown is handled at a lower level so it will capture all nonprinting keys like delete and enter.
“Clear” JavaScript: function myKeyPress(e){ var keynum; if(window.event) { // IE keynum = e.keyCode; } else if(e.which){ // Netscape/Firefox/Opera keynum = e.which; } alert(String.fromCharCode(keynum)); } <input type=”text” onkeypress=”return myKeyPress(event)” /> JQuery: $(“input”).keypress(function(event){ alert(String.fromCharCode(event.which)); }); <script src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js”></script> <input/>
I would use @HostListener decorator within your component: import { HostListener } from ‘@angular/core’; @Component({ … }) export class AppComponent { @HostListener(‘document:keypress’, [‘$event’]) handleKeyboardEvent(event: KeyboardEvent) { this.key = event.key; } } There are also other options like: host property within @Component decorator Angular recommends using @HostListener decorator over host property https://angular.io/guide/styleguide#style-06-03 @Component({ … host: { … Read more
You should use tabIndex attribute to be able to listen onKeyDown event on a div in React. Setting tabIndex=”0″ should fire your handler.