Handler for clicking the right button of the mouse


#1

Hellow, how to write an event handler for the clicking the right mouse button My example template.hbs

`<button {{action "click_button"}} id='my_button'>Click and get result</button>`
Controller
    import Ember from 'ember';
    export default Ember.Component.extend({
        actions:{
    	     //tried doesn't work
            //the handler fires only after 2 clicks and number of keystrokes depends on the holding time
            click_button()
            {
              $('#my_button').mousedown(function(event){
                   event.preventDefault();
                   if(event.button == 0)
    		          console.log('Left button')
    	           if(event.button == 2)
                        console.log('Rigth button')
            });               
            }
    	}
    )}

I saw the documentation https://guides.emberjs.com/v2.0.0/components/handling-events/ but could not understand. I need a working example, thanks in advance


#2

Try defining a contextMenu handler to handle right-clicking on the component, like this:

export default Ember.Component.extend({
  
  contextMenu() {
    // do your stuff here, it will get triggered on context menu click
  }
});

… or if you want to trigger the context menu of an element within the component…

export default Ember.Component.extend({
  
  contextMenuHandler() {
    // do your stuff here, it will get triggered on context menu click
  }
});
<button oncontextmenu={{action contextMenuHandler}}>Click and get result</button>