Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: How to catch keydown + mouse click event?

I have a div element. I need to catch a mouse click on this div while alt-key (keyCode = 17) is pressed.

Here is what i've got to catch key press:

// Html
<div id="targetDiv">I want to put a ding in the universe.</div>
// Java-script
$(document).ready(function(){
    $(window).bind('keydown', function(event){
        if ( 17 == event.keyCode ) {
           // How to catch mouse click on $('#targetDiv') ?
        }
    });
});

How to catch mouse click on div while alt-key is pressed?

like image 242
foreline Avatar asked Oct 06 '10 12:10

foreline


People also ask

Is Keydown an event?

The keydown event is fired when a key is pressed. Unlike the deprecated keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.

How do you check if a key is pressed in jQuery?

The keypress() method in jQuery triggers the keypress event whenever browser registers a keyboard input. So, Using keypress() method it can be detected if any key is pressed or not.

What is e keycode === 13?

Keycode 13 is the Enter key.

What is Keydown event in jQuery?

jQuery keydown() Method keydown - The key is on its way down. keypress - The key is pressed down. keyup - The key is released.


1 Answers

You can check the .altKey property, for example:

$(function() {
  $("#targetDiv").click(function(event) {
    if (event.altKey) {
       //do something, alt was down when clicked
    }
  });
});

You can test it out here.

like image 100
Nick Craver Avatar answered Sep 21 '22 03:09

Nick Craver