Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Javascript drop keyUp events when the metaKey is pressed on Mac browsers?

On Mac browsers, javascript does not receive keyup events for most keys (other modifier keys seem to be an exception) when the metakey is down. Use this jsfiddle to demonstrate (focus the result area and try something like cmd + x, the x will not receive a keyup event): http://jsfiddle.net/mUEaV/

I've reproduced this in stable releases for Chrome, FF, Safari and Opera. The same thing does not seem to happen with the control key in Windows 7.

Is the OS hijacking the keyup event? This seems especially strange since commands that use the metakey such as save, find, cut, copy, etcetera all activate on keydown not on keyup, and can be hijacked by the javascript just fine.

like image 410
anonymouse Avatar asked Aug 05 '12 17:08

anonymouse


4 Answers

It's simply not possible to get the onKeyUp events when meta is used, I learned today. Very unfortunate and difficult to work around. You'll have to emulate them some other way.

Edit: To clarify, this is only on Mac and occurs due to OS level handling of the event. It cannot be overridden. Sorry to be the bearer of bad news.

like image 104
Slbox Avatar answered Oct 23 '22 13:10

Slbox


Is the browser window retaining the focus when you press those keys? In windows you can get similar result when pressing windows+R or CTRL+ESC and similar key combinations that make browser to loose focus and that results in missed events.

like image 28
valentinas Avatar answered Oct 23 '22 12:10

valentinas


Although event.metaKey returns false, event.keyCode and event.key are still populated.

document.addEventListener('keyup', function(e) {
    console.log(e.metaKey || e.key);
});
Click here then press the Command, Control, or Option keys.
like image 2
AaronJ Avatar answered Oct 23 '22 12:10

AaronJ


While keyup events are indeed not available when the meta key is pressed, you can still get keydown events for all keys, as well as keyup events for the meta key itself.

This allows us to just simply keep track of the state of the meta key ourselves, like so:

let metaKeyDown = false;

window.addEventListener("keydown", event => {
    if (event.key == 'Meta') { metaKeyDown = true; }
});

window.addEventListener("keyup", event => {
    if (event.key == 'Meta') { metaKeyDown = false; }
});

By now additionally checking for the main key, plus cancelling the default behavior with Event.preventDefault() we can easily listen for key combinations (like here e.g. CMD+K) and prevent the browser from handling them:

let metaKeyDown = false;

window.addEventListener("keydown", event => {
    if (event.key == 'Meta') { metaKeyDown = true; }

    if (event.key == 'k' && metaKeyDown) {
        event.preventDefault();
        console.log('CMD+K pressed!');
    }
});

window.addEventListener("keyup", event => {
    if (event.key == 'Meta') { metaKeyDown = false; }
});

(Note the observation of the k key taking place already on keydown.)

Also, please be aware that when used incorrectly, this can break standard browser functionality (e.g. like CMD+C or CMD+R), and lead to poor user experience.

like image 1
max Avatar answered Oct 23 '22 13:10

max