Keyup does not fire when keydown opens a warning

I have two event handlers: one for keydown and one for keyup. The keydown event handler fires a warning message, but this prevents the keyup event from being fired.

Here you can see a very simple example: http://jsfiddle.net/boblauer/jaGwT/ When keydown opens a warning, the keyboard does not start, but when the warning does not open, the key starts. Here is the code from jsfiddle:

var i = 0; window.addEventListener('keydown', function(e) { if (i++ % 2) alert('down'); console.log('down'); }); window.addEventListener('keyup', function(e) { alert('up'); console.log('up'); }); 

I have a library that supports listening to several key combinations (for example, "d + f"), so when a key is pressed, I need to add it to the list of pressed keys and when the key, I have to remove it from the specified list. The problem I am facing is that if I want a warning to be shown while pressing d + f at the same time, my code to remove these keys from the โ€œcurrentlyโ€ list never fires because my keyup handler never gets called.

I canโ€™t come up with a good job on this issue. Any ideas?

+4
source share
1 answer

A warning prevents an event. Instead, you can run this function manually, because it happens anyway.

 var keyupfunction = function(e){ alert('up'); console.log('up'); } window.addEventListener('keyup', keyupfunction); window.addEventListener('keydown', function(e) { if (i++ % 2) alert('down'); console.log('down'); keyupfunction(e); }); 

But in fact, you should not use alerts. He prevents these events, but who knows what else can break. Use something non-standard instead.

+3
source

All Articles