Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Detect 'Shift' Key Down Within Another Function

Tags:

javascript

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

like image 966
Chris Avatar asked Jan 25 '26 20:01

Chris


1 Answers

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);
like image 102
tkone Avatar answered Jan 28 '26 10:01

tkone