How can I cancel the keydown of a specific key on the keyboard, for example(space, enter and arrows) in an HTML page.
How can I cancel the keydown of a specific key on the keyboard, for
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you’re only interested in the example keys you mentioned, the
keydownevent will do, except for older, pre-Blink versions of Opera (up to and including version 12, at least) where you’ll need to cancel thekeypressevent. It’s much easier to reliably identify non-printable keys in thekeydownevent than thekeypressevent, so the following uses a variable to set in thekeydownhandler to tell thekeypresshandler whether or not to suppress the default behaviour.Example code using
addEventListenerand ignoring ancient version of OperaOriginal example code from 2010