1
votes

I would like to know when a user is pressing down the Shift key during mousemove.

The problem is that Windows doesn't trigger keydown event when pressing down Ctrl, Shift or Alt. Instead, the event is triggered on releasing the key. On Mac everything is working as expected, however (keydown is triggered when you press down the key)

About the same problem with mouse move events. The event.shiftKey will only be true after you've held down Shift and triggered a mousedown...

Are there any workarounds for this? Here's an example: https://jsfiddle.net/ecz0deqw/4/

document.onkeydown = function (e) {
    addText('Key pressed: ' + e.key) 
};
document.onmousemove = function (e) {
    if(e.shiftKey)
        addText('ShiftMove');
   else {
       addText('NoShift')
   }
} 
function addText(text) {
    var newP = document.createElement("p"); 
    newP.innerText = text;
    document.body.insertBefore(newP,document.body.childNodes[0]);
}

Thanks / Eric

1
Try adding the events on document.body instead of document, and use addEventListener() to do soLennholm
According to the keydown spec, and the spec regarding "modifier keys", the keydown event should be raised.user47589
Thanks for reaching back to me! No difference, the events of those keys still triggers on "keyup" rather than "keydown" on windows. jsfiddle.net/ecz0deqw/7Eric Jacobsson
I just tried your code on my Windows box and it's behaving as expected. Event is triggered on key down. Something must be different on your system.gil.neo

1 Answers

1
votes

Is your Windows machine a VM? Parallels (at least) does funny stuff with the modifier keys.

Under Actions > Configure go to the Mouse & Keyboard tab. Next to the Keyboard label, choose Optimize for games.

Underneath that option Parallels states: "When the keyboard is optimized for games, modifier keys become more responsive."