0
votes

I have overriden onkeyup and onkeydown functions in my web application and i would like to use the keyboard's pause/break key.

But it seems that this is a special key and has a strange behaviour. After a keydown event it fires a keyup immediately, even if you use preventDefault() and stopPropagation() methods.

The following script should demonstrate the behaviour clearly. First press and hold space bar. No alert should pop up until you take your hand off the space key. Then try the same with pause/break key and you should get the alert as soon as you press down the key.

https://jsfiddle.net/at3L9o73/2/

window.onkeydown = keydown;
window.onkeyup = keyup;
function keydown(event)
{   
    if(event.keyCode == 19)
    {
        event.preventDefault();
        event.stopPropagation();
        console.log("break down");
    }
  else if(event.keyCode == 32)
    {
        event.preventDefault();
        event.stopPropagation();
        console.log("space down");
    }
}
function keyup(event)
{   
    if(event.keyCode == 19)
    {
        event.preventDefault();
        event.stopPropagation();
        console.log("break up");
    window.alert("break keyp????");
    }
  else if(event.keyCode == 32)
    {
        event.preventDefault();
        event.stopPropagation();
        console.log("space up");
    window.alert("space keyp");
    }  
}

Thanks in advance

1
Checked - it doesn't happen to me :) in the fiddle.scarto
Which browser do you use?ktsangop
Chrome, when keydown on pause/break, keyup not firing until keyup.scarto
That's weird... maybe it's my keyboard? I tried in both chrome and firefox. Will try another keyboard and post back. Thank you @scartoktsangop
Well that's embarrassing. It was my keyboard after all...! Tried two others, and had no problem.. Thanks again @scarto!ktsangop

1 Answers

2
votes

Seems like it was my keyboard! I checked the script using both a Dell and a HP keyboard, and didn't have any unexpected behaviour.

After investigating further i found reports that some keyboards (like mine) treat the pause/break and print screen/sysrq keys like i described. Sending both keydown and keyup sequences when pressed and nothing when released.

This article describes a similar behaviour :

Keyboard Scancodes - Special Keyboards