Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: keydown event not firing

I am writing a userscript with the following code:

(function() {
    'use strict';
    window.addEventListener("keydown", arrows, false);
    function arrows(e) {
        debugger;
        switch(e.keycode) {
             case 37: alert("Left"); break;
             case 39: alert("Right"); break;
        }
    }
})();

Eventually the left and right cases will navigate to the previous and next articles in a series, respectively, with something like:

window.location = String(parseInt(window.location.href.match(/\d+$/))-1);

However, pressing the arrow keys does not cause an alert. The script is clearly loaded, the Chrome developer menu shows the arrows() function is registered as an event listener for window.keydown, and yet the function never fires. I added debugger; to the arrows() function, but the debugger does not show when I press the arrow keys.

like image 852
Griffin Young Avatar asked Jun 06 '16 15:06

Griffin Young


People also ask

Is Keydown an event?

The keydown event is fired when a key is pressed. Unlike the deprecated keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.

What is the difference between keypress () and Keydown ()?

keydown – fires when any key is pressed down, fires first, and always before the browser processes the key (e.g. inserting text, moving focus, etc). keypress – fires when a key that produces a character value is pressed down, fires after keydown , and before the browser processes the key.

What does the Keydown () do?

The keydown event is sent to an element when the user presses a key on the keyboard. If the key is kept pressed, the event is sent every time the operating system repeats the key. It can be attached to any element, but the event is only sent to the element that has the focus.

Should I use keypress or Keydown?

The keypress ignores keys such as delete , arrows , page up , page down , home , end , ctrl , alt , shift , esc , etc. So, if we need to handle those keys, it's better to use either keydown or keyup event. }); The keydown and keypress events fire multiple times if user press and hold a key.

How do you get the value of a Keydown event?

The simple solution: just use the keyup event (and not the keydown event). This will give you the latest value after the user typed the latest character. That resolves the issue.


2 Answers

The event propagation is probably getting stopped at some point on the handler for an element, before it bubbles up to window (probably due to a poorly-written onkeydown returning false to prevent the default action, not caring that this also stops propagation).

You should attach your listener with capturing, so that it will capture the event at window, before it bubbles:

// note the third parameter
window.addEventListener("keydown", arrows, true);
like image 137
Stuart P. Bentley Avatar answered Oct 14 '22 04:10

Stuart P. Bentley


You have mispelled the keyCode:

switch(e.keyCode) { // Code is uppercase
    case 37: alert("Left"); break;
    case 39: alert("Right"); break;
}
like image 41
Azamantes Avatar answered Oct 14 '22 05:10

Azamantes