Yehuda Gutstein
Yehuda Gutstein

Reputation: 381

Combining a mousedown event with a keydown event

I am trying to combine a keydown event with a mousedown event. Basically, I have a chat dialog, and if the user writes in one of 2 divs (keydown event) or makes a selection from either (or both) of 2 dropdowns (mousedown), I need these to have the same exact function. Thanks in advance.

Code (to be combined):

$('#usermsg').add('#otherComments').keydown(function() { // When key pressed

and

$('#strategies').add('#whySwitch').mousedown(function() { // When mouse is clicked
  //function body
});

Upvotes: 1

Views: 3576

Answers (4)

vladkras
vladkras

Reputation: 17227

why not

$('#usermsg').add('#otherComments').keydown(yourFunction);

and

$('#strategies').add('#whySwitch').mousedown(yourFunction);

then

function yourFunction() {
// do what you want
}

?

Upvotes: 0

Ayyappan Sekar
Ayyappan Sekar

Reputation: 11485

Write a function that serves both the div and call it like,

$('#usermsg').add('#otherComments').keydown(functionToCall);

$('#strategies').add('#whySwitch').mousedown(functionToCall);

/do not forget to remove ()/

then the function implementation:

function functionToCall() {
    //body
}

Upvotes: 1

scel.pi
scel.pi

Reputation: 703

I dont know if is the right way, but i everytime do it:

var keyPressed = new Object(); 
keyPressed.mouse = 0; 
keyPressed.keyboard = 0;

one event to keydown and keyup, and another one to mouseup and mousedown

both events keydown and mousedown sets keyPressed.mouse or keyboard to 1 both events keyup and mouseup sets keyPressed.mouse or keyboard to 0

mousedown and and keydown will call a function who see if mouse and keyboard are down and do something you want.

Upvotes: 0

sgun
sgun

Reputation: 899

Implement the callback function and provide its name in both events as callback function argument.

Upvotes: 0

Related Questions