Pergunta

I want to trigger mouse middle click event using javascript. Is it possible to trigger mouse middle click using Javascript?

I want it because it is pasting last selected object from clipboard.

Thanks, Jimit

Foi útil?

Solução

Had the same question, did a lot of digging, and this is what I ended up using:

if ( window.CustomEvent ) {
    var middleClick = new MouseEvent( "click", { "button": 1, "which": 2 });
    jQuery(".selector")[0].dispatchEvent( middleClick );
}

Outras dicas

You can use

event.button

to identify which mouse button was clicked.

Returns an integer value indicating the button that changed state.

* 0 for standard 'click', usually left button
* 1 for middle button, usually wheel-click
* 2 for right button, usually right-click

  Note that this convention is not followed in Internet Explorer: see 
  QuirksMode for details.

The order of buttons may be different depending on how the pointing device has been configured.

Also read

Which mouse button has been clicked?

There are two properties for finding out which mouse button has been clicked: which and button. Please note that these properties don’t always work on a click event. To safely detect a mouse button you have to use the mousedown or mouseup events.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top