0

Using javascript (jquery okay), how could I fire an event on a shift+option+command+left click on an object?

$(document).on('keypress', function(e) {
        if( e.keyCode == 224 && e.keyCode == 16 && e.keyCode == 18 ){
               console.log('keys pressed');
        }
});

It seems like something like this should work for logging the keys (but it doesn't at all ^^ ) And also, how do I track the click?

Thanks!

1252748
  • 14,597
  • 32
  • 109
  • 229

1 Answers1

1

Try this:

Live Demo (for Mac OS): http://jsfiddle.net/NDuDZ/2/

Code:

//Combination: Shift+Option+Command+LeftClick

//Shift Firefox, Safari, Chrome, Opera keyCode: 16
//Option Firefox, Safari, Chrome, Opera keyCode: 18
//Command Firefox keyCode: 224
//Command Chrome, Safari keyCode: 91
//Command Opera keyCode: 17

var isOption = false;
var isCommand = false;
var isLeftClick = false;

$(document).on('keyup', function(e){
    if(e.which == 91 || e.which == 224 || e.which == 17) isCommand = false;
    if(e.which == 18) isOption = false;
});

$(document).on('click', function(e){
    isLeftClick = true;
});

$(document).on('keyup', function(e){
    if(e.which == 91 || e.which == 224 || e.which == 17) isCommand = true;
    if(e.which == 18) isOption = true;
    if(e.which == 16 && isOption && isCommand && isLeftClick) {
       alert('Shift+Option+Command+LeftClick invoked!');
       e.preventDefault();
    }
});
Oscar Jara
  • 14,129
  • 10
  • 62
  • 94
  • it works pretty well. thank you. I do notice that after you click once, every time you do the `ctl+opt+cmd` afterward the alert is triggered. also the even fires when the keys are released. is there anyway to fire it on the mouse click. it's totally brilliant by the way; thanks much! – 1252748 Aug 13 '12 at 20:42
  • @thomas yes, I noticed that after I posted my answer, I tried to do an improved version but no success (also I am on Windows) and this is the best I can do by now. You can play with that code and maybe do something better because everything is done I think :-) – Oscar Jara Aug 13 '12 at 20:49