Detect SHIFT key pressed and direct mouse click

Asked

Viewed 453 times

0

Hello

I need to create a shortcut where by pressing the SHIFT key + LEFT mouse button, displays an alert. How to do this very simple and using Jquery ?

Thank you

  • Take a look here, it can help you: http://answall.com/a/161539/57573

  • Thank you Junior

1 answer

1


Use Event.shiftKey for that reason:

With jQuery

$(document).click(function(event) {
    if (event.shiftKey) { // tecla shift
        console.log("shift+click")
    } 
    if (event.ctrlKey) { // tecla Ctrl
        console.log("ctrl+click")
    } 
    if (event.metaKey) { // tecla Meta (CMD nos teclados Apple ou Windows nos outros)
        console.log("meta+click")
    } 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Without jQuery:

function logChar(e) {
  e.preventDefault();
  
  if(e.shiftKey) {
    console.log('shift+click');
  }
  else if(e.ctrlKey) {
    console.log('ctrl+click');
  }
  else if(e.metaKey) {
    console.log('meta+click');
  } else {
    console.log('click simples');
  }
}
<a href='#' onclick="logChar(event);">clique aqui</a>

  • Hey, Ricardo, nice, thanks. I am learning javascript and I have a doubt, in the function this / Function logChar(e) / has the letter ( e ) as parameter of the event, correct ? Can I pass other parameters through this function without interfering with the functioning of the event parameter ? How do I do ?

  • As this function was defined right there you can pass as many parameters as you want, as long as it is the same number in the setting and in the call. If you have more questions open another question!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.