开发者

Right Click Detection for Firefox

Before beginning, I am not inquiring about stopping users from right-clicking. This is for right-click detection on a html5 canvas.

So currently, what I have is:


  function init() {
   var canvas = document.getElementById('canvasID');\
   if (canvas.getContext) {
    ctx = ca开发者_开发知识库nvas.getContext('2d');
    ctx.font = '10px sans-serif';
    ctx.canvas.onmousedown=ctx.canvas.onclick=onMousePressed;
    ctx.canvas.onmouseup=ctx.canvas.onmouseout=onMouseReleased;
    ctx.canvas.onmousemove = onMouseMove;
   }
  }

And also:


      function onMouseMove(e) {
   if (mouseDown) {
    click2 = convertCoordsToTileCoords({
     x: e.offsetX || e.layerX - this.style.left,
     y: e.offsetY || e.layerY - this.style.top
    });
    var rightclick;
    if (!e) e = window.event;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
..etc

Where the variable 'rightclick' is a boolean, resulting in true if the person is right clicking.

Which browsers this works for: Chrome

Opera, no, because of their mouse gestures and stuff. IE, no, because of canvas. But I cannot figure out why it will not work on Firefox.

As usual, if there is a better, more efficient, method of tackling this problem, it would be highly appreciated.

-Firstmate

Edit1: Provided more code.


If you don't mind using jQuery, there exists a Right-click Plugin that makes this much simpler.


Testing this in Firefox 3.6.4/Mac worked as expected:

document.onclick = function(e) { 
    var rightclick;
    if (!e) e = window.event;
    if (e.which) rightclick = (e.which == 3);
    else if (e.button) rightclick = (e.button == 2);
    alter(rightclick);
}

Perhaps you're not attaching the click handler correctly?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜