Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify if a key is down on hover?

I would like to show one div only when another div is hovered and the Space Bar is down. I tried to use keyCode and which properties of the event, but none of them worked. Though, I could create an example for CTRL key pressed rather than the Space Bar, as you can see also here.

How should I change the code so it will work with Space Bar (or any other key) down?

HTML:

<div class='a'></div>
<div class='b'></div>

CSS:

body {
    position: relative;
}
div {
    width: 100px;
    height: 100px;
    position: absolute;
}
.a {
    background: #777;
    left: 50px;
    top: 30px;
}
.b {
    display: none;
    background: #000;
    left: 250px;
    top: 100px;
}

JS:

$(function() {
    $('div').hover(function(e) {
        if (e.ctrlKey) {
            $('.b').show();
        }
    }, function() {
        if ($('.b').is(':visible')) $('.b').hide();
    });
});
like image 766
Misha Moroshko Avatar asked Sep 17 '10 02:09

Misha Moroshko


People also ask

How do I know if my mouse button is down?

We can check if a mouse button is kept down by listening to the mousedown and mouseup events and mount how many times the mouse button is pressed down or lifted up.

How do you know if an element is hover?

You can simply use the CSS :hover pseudo-class selector in combination with the jQuery mousemove() to check whether the mouse is over an element or not in jQuery. The jQuery code in the following example will display a hint message on the web page when you place or remove the mouse pointer over the DIV element's box.

Is mouse over same as hover?

The hover() function is more high level - it's built to call functions to handle both a mouseenter event and a mouseleave event. It's very convenient for a UI element that has a hover and normal state (e.g. a button.) The mouseover() function specifically binds to the mouseover event.

What is Mousedown event?

The mousedown event occurs when the left mouse button is pressed down over the selected element. The mousedown() method triggers the mousedown event, or attaches a function to run when a mousedown event occurs. Tip: This method is often used together with the mouseup() method.


1 Answers

You can make use of the fact that .hover() binds 2 handlers. One for mouseenter and one for mouseleave.

Bind a .keydown() on mouseenter and simply .unbind() it on mouseleave:

$(function() {

      // Define the mouseenter and mouseleave handlers with hover
    $("div.a").hover(function() {

          // Show other div if a key is pressed.
          // You can of course check for on particular key.
        $(document).keydown(function() {
            $("div.b").show();
        });

    }, function() {

         // unbind the keydown handler on mouseleave
       $(document).unbind("keydown");

       $("div.b").hide();
    });
});​

jsFiddle example


An important note is that .hover() will work even if the window hasn't been focused, but .keydown() will only work if the window is in focus.

like image 198
Peter Ajtai Avatar answered Oct 11 '22 11:10

Peter Ajtai