Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trigger right click if you left click on some html element using jQuery?

Basically, I want to open a context menu on left click itself. Is there anyway to do this using jQuery?

like image 693
RPB Avatar asked Aug 30 '11 12:08

RPB


3 Answers

You can't. JavaScript does not have that access to the browser. Instead you could create your own custom context menu and try to give it the behavior choices you want from the normal context menu (Back, forward, etc). Of course, some of those may be restricted (like copy/paste).

http://labs.abeautifulsite.net/projects/js/jquery/contextMenu/demo/

like image 151
Jason Dean Avatar answered Sep 23 '22 15:09

Jason Dean


you can record event for right click and trigger whatever event you want to perform on right click.

like image 29
TouchWebInc Avatar answered Sep 21 '22 15:09

TouchWebInc


You can't trigger the right click, but you can trigger a keypress using .trigger()

Shift + F10 should trigger the context menu on Windows, something like...

function openContextMenu() {
  jQuery.event.trigger({ type: 'keypress', which: 121, shiftKey: true });
}

Also there's a context-menu key (on the right before CTRL on 104+ key keyboards) that I think might be keycode 93:

function openContextMenu() {
  jQuery.event.trigger({ type: 'keypress', which: 93 });
}

Update

Actually these just simulate the event - any JS events for that event fire, but the actual key doesn't get sent.

You can do this with an ActiveX object:

// ActiveX object
var shell = new ActiveXObject("WScript.Shell");

// Send SHIFT+F10
shell.SendKeys("+{F10}");

However that component is marked as not safe for scripting and is IE only, so that solution is only really practical for intranets and the like.

like image 20
Keith Avatar answered Sep 21 '22 15:09

Keith