Skip to content
Advertisement

jQuery capture mousemove when a certain key is pressed

I am trying to capture event if suppose key H is pressed and mouse is moving over some div.

KeyPressed and mouseMoving both have to be true.

https://jsfiddle.net/bababalcksheep/1Loeh2pn/

Code:

$('#header').on("mousemove keydown", function(e) {
  //only print if key 'H' is pressed and mouse is moving over $('#header')
  if (e.type === 'mousemove' && e.which === 72) {
    console.log('working');
  }
});

Advertisement

Answer

This may helpful for you.

https://jsfiddle.net/1Loeh2pn/3/

$(function() {
    $("#header").hover(function() {
        $(document).keydown(function(e) {
            if(e.which == 72){
                console.log('H')
            }
        });
    });
});
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement