Haradzieniec
Haradzieniec

Reputation: 9338

jQuery: triggering (mousemove or "keypressed")

I've read some answers regarding triggering mousemove any key pressed but I'm still not sure, what's the best way to unite both events - mousemove and pressing any key two in one.

What's the best way to modify

$(document).mousemove(function ()
{
  alert('The mouse was touched');
}

to

$(document).<???>(function ()
{
  alert('The mouse was touched or any key pressed');
}

Upvotes: 1

Views: 3019

Answers (2)

Hmmm
Hmmm

Reputation: 1764

you could use .on()

$(document).on( "mousemove keypress", function () {
 code
});

Upvotes: 3

Felix Guo
Felix Guo

Reputation: 2708

Having 3 functions is probably the best way to go.

$(document).mousemove(movePressed()); //calls function
$(document).keypress(movePressed()); //calls same function

function movePressed(){//do whatever you want when mousemove or keypressed}

Upvotes: 1

Related Questions