将 IE event.button 转换为 W3C event.button

发布于 2024-08-21 07:16:15 字数 302 浏览 5 评论 0原文

在按下鼠标(和向上)时,我需要检查哪个鼠标按钮已更改其状态。这对于 W3C 模型甚至旧的 netscape 方式(event.which)来说都很容易,但 IE 让我头疼。我知道 IE 中的 event.button 是一个位掩码,但问题是我无法从该位掩码中找出按下了哪个按钮。如果 IE 给我 event.button == 3 那么我无法判断用户是否在右侧按钮已按下时按下了左侧按钮,或者用户是否在左侧按钮已按下时按下了右侧按钮。

为了一劳永逸地解决这个问题,我正在寻找一个很好的通用解决方案,将损坏的 IE 事件转换为 W3C 兼容事件。也许有人已经这样做了并想分享?

On mouse-down (and up) I need to check which mouse button has changed its state. This is pretty easy with the W3C model and even the old netscape way (event.which) but IE gives me a headache. I know that event.button in IE is a bitmask but the problem is that I can't find out which button was pressed from this bitmask. If IE gives me event.button == 3 then I can't tell if the user pressed the left button while the right button was already down or if the user pressed the right button while the left button was already down.

To solve this problem once and for all I'm searching for a nice generic solution to convert the broken IE event into a W3C compatible event. Maybe someone already did that and wants to share?

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

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

发布评论

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

评论(2

在梵高的星空下 2024-08-28 07:16:15

IMO(以及 quirksmode 的),IE 的位屏蔽比 W3C 版本更好,因为可以检测组合点击,例如左+右或中+左。它提供了更多的灵活性。两者(按下的按钮总数和按下的最新按钮)都会更好。

我不知道有哪个库或脚本已经完成了您想要做的事情,我认为大多数开发人员都会忽略组合点击,除非他们想对组合点击应用特定操作。另外,这相当棘手,您必须检测整个文档的 onmousedownonmouseup 并记录鼠标按钮的状态。然后,在您想要施展魔法的元素上,从新的鼠标状态中减去之前的鼠标状态。类似的东西(经过测试):

if (document.attachEvent)
{
    (function ()
    {
        var mState = 0;
        document.documentElement.attachEvent("onmousedown", function () 
        {
            mState += event.button;
        });
        document.documentElement.attachEvent("onmouseup", function () 
        {
            mState -= event.button;
        });
        document.getElementById("jim").attachEvent("onmousedown", function ()
        {
            var realButton = event.button - mState;

            alert(realButton); 
            // Your code here
        });
    })();
}

当然,这个脚本只有大约一百万个可能出错的地方。例如,在我的示例中,我有一个弹出的警报窗口,其中包含真实的按钮状态。如果在警报对话框出现时释放按钮,文档将不会检测到鼠标松开,并且会使一切变得不正常。同样,其他元素上的任何 mouseup 或 mousedown 事件停止传播到 documentElement 的事件处理程序都会导致它中断。

如果您需要我的建议,请在组合鼠标单击时显示一条消息,猜测“正确”的鼠标按钮或忽略它们。这些选项中的任何一个都比黑客方法更好。

IMO (and quirksmode's), IE's bitmasking is better than the W3C version because you can detect combo clicks, like left+right or middle+left. It offers a bit more flexibility. Both (total buttons pressed and newest button pressed) would be even better.

I don't know of a library or script that already does what you're trying to do, I think most devs would just ignore combo clicks unless they wanted to apply a specific action to a combo click. Also, it's rather tricky, you'd have to detect both onmousedown and onmouseup for the entire document and record the state of the mouse buttons. Then on the element you want to work your magic on, subtract the previous mouse state from the new one. Something like (tested):

if (document.attachEvent)
{
    (function ()
    {
        var mState = 0;
        document.documentElement.attachEvent("onmousedown", function () 
        {
            mState += event.button;
        });
        document.documentElement.attachEvent("onmouseup", function () 
        {
            mState -= event.button;
        });
        document.getElementById("jim").attachEvent("onmousedown", function ()
        {
            var realButton = event.button - mState;

            alert(realButton); 
            // Your code here
        });
    })();
}

Of course, there are only about a million things that could go wrong with this script. For instance, in my example there I have an alert window that pops up with the real button state. If you release the button whilst that alert dialog is up, the document won't detect the mouseup and it will throw everything out of whack. Likewise, any mouseup or mousedown events on other elements that stop propagation up to the documentElement's event handler would cause it to break.

If you want my advice, display a message on a combo mouse click, guess at the "correct" mouse button or ignore them all together. Any one of those options is better than a hacky approach.

饭团 2024-08-28 07:16:15

您可能会发现此页面很有用:http://unixpapa.com/js/mouse.html 。它有一个关于鼠标按钮检测的部分,我以前使用过并且发现它有用且准确,包括以下内容:

虽然没有独立于浏览器
识别哪个鼠标按钮的方法
对于所有接受调查的浏览器来说,
你可以非常接近。这
以下测试适用于所有浏览器
Opera 7 除外。(它混合了右
Opera 7 中的和鼠标中键,
但 Opera 7 已经很老了,有
只有少数几个版本是正确的
中间的按钮甚至可以工作,并且
那么只有当用户设置一个模糊的
选项,所以你真的不在乎。)
也没有正确处理
多个鼠标同时点击
IE 中的按钮。要做到这一点,你的
应用程序需要跟踪哪些
据报道按钮被按下
以前的 mousedown 事件,这样它就可以
找出添加了哪一个。

if (event.which == null)
   /* IE case */
   button= (event.button < 2) ? "LEFT" :
             ((event.button == 4) ? "MIDDLE" : "RIGHT");
else
   /* All others */
   button= (event.which < 2) ? "LEFT" :
             ((event.which == 2) ? "MIDDLE" : "RIGHT");

You may find this page useful: http://unixpapa.com/js/mouse.html. It has a section on mouse button detection that I have used before and found useful and accurate, including the following:

While there is no browser-independent
way to recognize which mouse button is
which for all the browsers surveyed,
you can come extremely close. The
following test works for all browsers
except Opera 7. (It mixes up the right
and middle mouse buttons in Opera 7,
but Opera 7 is pretty old, there were
only a few versions where the right
and middle buttons even worked, and
then only if the user set an obscure
option, so you really don't care.) It
also doesn't correctly handle
simultaneous clicks of multiple mouse
buttons in IE. To do that your
application needs to track which
buttons were reported to be down in
previous mousedown events so it can
figure out which one was added.

if (event.which == null)
   /* IE case */
   button= (event.button < 2) ? "LEFT" :
             ((event.button == 4) ? "MIDDLE" : "RIGHT");
else
   /* All others */
   button= (event.which < 2) ? "LEFT" :
             ((event.which == 2) ? "MIDDLE" : "RIGHT");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文