Javascript Detect 'Shift' Key Down Within Another Function

javascript

Solution

Attach a `keydown` and `keyup` event to the `document` on the page and listen for the shift key.

var shiftDown = false;
var setShiftDown = function(event){
    if(event.keyCode === 16 || event.charCode === 16){
        window.shiftDown = true;
    }
};

var setShiftUp = function(event){
    if(event.keyCode === 16 || event.charCode === 16){
        window.shiftDown = false;
    }
};

window.addEventListener? document.addEventListener('keydown', setShiftDown) : document.attachEvent('keydown', setShiftDown);
window.addEventListener? document.addEventListener('keyup', setShiftUp) : document.attachEvent('keyup', setShiftUp);

Problem

I am calling a Javascript function in my html page from a Flash movie (using ExternalInterface) and I want to know if the user has the Shift key down when the function is called. It seems straight forward if, for example, I call the function from a mouse click because I can pass the event and check 'if (event.shiftKey)'. I don't have an event to check! Many thanks Chris

Original source