检测鼠标左键按下

发布于 2024-09-28 00:53:54 字数 443 浏览 10 评论 0原文

我讨厌 W3C 和 MS 创建的鼠标按钮的混乱!我想知道当我收到 mousedown 事件时是否按下了鼠标左键。

我使用这段代码

// Return true if evt carries left mouse button press
function detectLeftButton(evt) {
  // W3C
  if (window.event == null) {
    return (evt.button == 0)
  }
  // IE
  else {
    return (evt.button == 1);
  }
}

但是,它在 Opera 和 Chrome 中不起作用,因为碰巧 window.event 也存在在那里。

那我该怎么办?我有一些浏览器检测,但我们都知道它不能依赖于一些浏览器最近所做的所有屏蔽。如何可靠地检测鼠标左键?

I hate this mess with the mouse buttons created by W3C an MS! I want to know if the left mouse button is pressed when I get a mousedown event.

I use this code

// Return true if evt carries left mouse button press
function detectLeftButton(evt) {
  // W3C
  if (window.event == null) {
    return (evt.button == 0)
  }
  // IE
  else {
    return (evt.button == 1);
  }
}

However, it does not work in Opera and Chrome, because it so happens that window.event exists there too.

So what do I do? I have some browser detection, but we all know it cannot be relied upon with all the masking some browsers do lately. How do I detect the left mouse button RELIABLY?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱人如己 2024-10-05 00:53:54

更新了答案。以下将检测是否按下鼠标左键且仅按下鼠标左键:

function detectLeftButton(evt) {
    evt = evt || window.event;
    if ("buttons" in evt) {
        return evt.buttons == 1;
    }
    var button = evt.which || evt.button;
    return button == 1;
}

有关在 JavaScript 中处理鼠标事件的更多信息,请尝试 http://unixpapa.com/js/mouse.html

Updated answer. The following will detect if the left and only the left mouse button is pressed:

function detectLeftButton(evt) {
    evt = evt || window.event;
    if ("buttons" in evt) {
        return evt.buttons == 1;
    }
    var button = evt.which || evt.button;
    return button == 1;
}

For much more information about handling mouse events in JavaScript, try http://unixpapa.com/js/mouse.html

嗳卜坏 2024-10-05 00:53:54

现在有一个 W3C 标准 < 标准模式下的 IE9Gecko 15+< /a>.

W3C 完全塞满了 event.button 属性,因此对于符合标准的browser event.button 为 0,但对于标准之前创建的浏览器,event.button 为 1。

因此代码必须避免使用 event.button(旧版浏览器除外)。以下代码应该可以工作:

function detectLeftButton(event) {
    if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) {
        return false;
    } else if ('buttons' in event) {
        return event.buttons === 1;
    } else if ('which' in event) {
        return event.which === 1;
    } else {
        return (event.button == 1 || event.type == 'click');
    }
}

There is now a W3C standard event.buttons property supported by IE9 in standards mode, and Gecko 15+.

The W3C completely stuffed up the event.button property, so for a standards compliant browser event.button is 0, but for browsers created before the standard, event.button is 1.

So code must avoid using event.button except for older browsers. The following code should work:

function detectLeftButton(event) {
    if (event.metaKey || event.ctrlKey || event.altKey || event.shiftKey) {
        return false;
    } else if ('buttons' in event) {
        return event.buttons === 1;
    } else if ('which' in event) {
        return event.which === 1;
    } else {
        return (event.button == 1 || event.type == 'click');
    }
}
青巷忧颜 2024-10-05 00:53:54

您可以使用以下代码 -

onmouseup="if(window.event.which==1){//code for left click}
           else if(window.event.which==3){//code for right click}"

You can use the following code-

onmouseup="if(window.event.which==1){//code for left click}
           else if(window.event.which==3){//code for right click}"
眉黛浅 2024-10-05 00:53:54

尽管 event.buttons 现在可以在 Chrome 中使用,但 Safari 仍然不支持它。一种解决方法是在文档或父级别使用 onmousedown 和 onmouseup 事件,例如:
onmousedown="bMouseDown=true" onmouseup="bMouseDown=false"

Even though event.buttons now works in Chrome, Safari still does not support it. One workaround is to use onmousedown and onmouseup events at document or parent level like:
onmousedown="bMouseDown=true" onmouseup="bMouseDown=false"

流星番茄 2024-10-05 00:53:54
// 0 left, 2 right, 1 middle, other.. extra buttons, gaming mouses

var buttonsArray = [false, false, false, false, false, false, false, false, false];
var mousePressed = false;

document.onmousedown = function(e) {
    buttonsArray[e.button] = true;
    mousePressed = true;
};

document.onmouseup = function(e) {
    buttonsArray[e.button] = false;
    mousePressed = false;
};

document.oncontextmenu = function() {
    return false;
}

说明:当鼠标按下时,我们将按钮数组中按下的按钮更改为 true。
当鼠标抬起时,我们将按下的按钮更改为 false。

现在我们可以更准确地确定哪个按钮被按下,但是我们遇到了右键单击问题..因为使用该按钮我们在浏览器中打开一个上下文菜单,并且逃脱了我们的控制...所以,我们禁用上下文菜单以便正确检测右键。如果我们不这样做,我们也必须解决左键单击问题……这是一个逃避此响应的复杂情况。

为了简化事情,我们可以添加另一个变量 mousePressed 并标记鼠标是否按下或向上。

在 Chrome 上完美运行,我没有在其他浏览器中测试它,但我想它在 Firefox 和 Opera 中也可以...... IE ???我不在乎IE。

// 0 left, 2 right, 1 middle, other.. extra buttons, gaming mouses

var buttonsArray = [false, false, false, false, false, false, false, false, false];
var mousePressed = false;

document.onmousedown = function(e) {
    buttonsArray[e.button] = true;
    mousePressed = true;
};

document.onmouseup = function(e) {
    buttonsArray[e.button] = false;
    mousePressed = false;
};

document.oncontextmenu = function() {
    return false;
}

Explanation: When mouse is down, we change to true the pressed button in our buttons array.
When mouse is up, we change to false the pressed button to false.

Now we can establish which button is pressed more accurately, but we have a right click problem.. because with that button we open a contextmenu at the browser, and that escapes our control... so, we disable the context menu in order to properly detect right click. If we don't do that, we must resolve left click too... and its a complication that escapes this response.

In order to simplify things, we can add another variable mousePressed and flag if mouse is down or up.

Works perfect on chrome, I didn't test it in other browser but I guess its ok in firefox and opera too... IE??? I don't care IE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文