jQuery Event How to - Check if shift or control key holded while clicking








Question

We would like to know how to check if shift or control key holded while clicking.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.4.2.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--  ww w .  java  2 s .c  om-->
    $(document).ready(function(){
        $('input').each(function (index, element) {
            $(element).click(function (event) {
                if (!event.shiftKey && !event.ctrlKey) {
                   console.log("test");
                }
                else {
                   console.log("blah");
                }
            });
        });
    });
});
</script>
</head>
<body>
  <input type="button" value="test">
</body>
</html>

The code above is rendered as follows: