How to define whether the left mouse button is pressed in Firefox

Discussion in 'Javascript' started by marss, Apr 16, 2007.

  1. marss

    marss Guest

    Hi,
    How can I define in Firefox whether the left mouse button is pressed
    when I move mouse over a html element?
    The documentation says that "e.button == 0 for standard 'click',
    usually left button"
    http://developer.mozilla.org/en/docs/DOM:event.button
    Tracing of mouse clicks or mousedown/mouseup events looks not
    reliable, because mouse can be pressed elsewhere out of the html
    element (even outside of the browser) and then move over the html
    element.
    Maybe exists a better way to do it?

    Here the test that reproducing the problem.
    <head>
    <script type="text/javascript">
    function test(e)
    {
    if (e.button == 0)
    document.getElementById("d").innerHTML = "Left mouse button is
    pressed";
    }
    </script>
    </head>
    <body>
    <div onmousemove="test(event)" style="height:50px;background-
    color:gray"></div>
    <div id="d"></div>
    </body>
     
    marss, Apr 16, 2007
    #1
    1. Advertising

  2. marss

    Daz Guest

    On Apr 16, 4:38 pm, "marss" <> wrote:
    > Hi,
    > How can I define in Firefox whether the left mouse button is pressed
    > when I move mouse over a html element?
    > The documentation says that "e.button == 0 for standard 'click',
    > usually left button"http://developer.mozilla.org/en/docs/DOM:event.button
    > Tracing of mouse clicks or mousedown/mouseup events looks not
    > reliable, because mouse can be pressed elsewhere out of the html
    > element (even outside of the browser) and then move over the html
    > element.
    > Maybe exists a better way to do it?
    >
    > Here the test that reproducing the problem.
    > <head>
    > <script type="text/javascript">
    > function test(e)
    > {
    > if (e.button == 0)
    > document.getElementById("d").innerHTML = "Left mouse button is
    > pressed";}
    >
    > </script>
    > </head>
    > <body>
    > <div onmousemove="test(event)" style="height:50px;background-
    > color:gray"></div>
    > <div id="d"></div>
    > </body>


    I think you are along the right lines. Personally. I would add an
    onmouseover and onmouseout event to the elements in question. The
    event will attach and dettach an onclick handler to the element when
    you have moved the mouse over, which will test to see if the left
    button was clicked.

    I hope this helps.

    Daz.
     
    Daz, Apr 16, 2007
    #2
    1. Advertising

  3. marss

    marss Guest

    Daz wrote:

    > I think you are along the right lines. Personally. I would add an
    > onmouseover and onmouseout event to the elements in question. The
    > event will attach and dettach an onclick handler to the element when
    > you have moved the mouse over, which will test to see if the left
    > button was clicked.
    >
    > I hope this helps.
    >
    > Daz.


    It is to be regretted that only way to determine mouse buttons state
    in Firefox is calculating of clicks. :(
    Thanks for the answer.
     
    marss, Apr 17, 2007
    #3
  4. marss

    marss Guest

    Randy Webb wrote:

    >
    > That won't tell you though. If I move outside the browser, hold down the
    > left mouse button, move over your div, move outside the browser window
    > again, release the mouse button and then mouse back over your element,
    > how would it know what state the mouse is in?
    >


    You are right. I see that the situation is rather sad. But I have to
    do something. As I said before it is not reliable but lacking
    something better...
     
    marss, Apr 17, 2007
    #4
  5. marss

    Daz Guest

    On Apr 17, 1:42 pm, Randy Webb <> wrote:
    > That won't tell you though. If I move outside the browser, hold down the
    > left mouse button, move over your div, move outside the browser window
    > again, release the mouse button and then mouse back over your element,
    > how would it know what state the mouse is in?


    This is very true, but unfortunately, as all browsers only monitor
    events within the window, without an external API, it would be
    impossible to achieve the exact results we'd like. If the mouse
    buttons are clicked or released outside of the window, it won't
    register the button state within the browser, (or can it the button
    state be worked out onmousemove?).

    Also, remember that the div isn't meant to detect when the button is
    down, but rather, when the button is clicked on top of it. If you
    click on a blank spot in your browser window, and hold the button
    down, then drag your mouse over something that's clickible outside of
    the browser window, such as your start menu, the start menu doesn't
    open up just because the mouse is down. It has to be explicitly
    clicked for the start menu to open. Obviously, you can drag files into
    it, but they must be from the operating system itself, and not from
    within an application that doesn't tie in quite heavily to the inner
    workings of the OS. Basically, I guess that what I am saying, is that
    there's a bridge between browser and OS, unless of course the browser
    has been made to specifically work as part of the core OS. I
    completely agree with what you've said, but we can only do the best we
    can with the tools we have to hand. We cannot always acheive the
    impossible with the low level user-functionality, that most modern
    applications tend to provide us with, as they are intended to be used
    by "average" users and be quite secure against idiots/accidents.
     
    Daz, Apr 17, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Claus Atzenbeck
    Replies:
    1
    Views:
    1,089
    Claus Atzenbeck
    Nov 26, 2004
  2. Rune
    Replies:
    6
    Views:
    1,269
    Richie Hindle
    Jan 29, 2004
  3. Jeroni Paul

    Mouse movement while a button is pressed

    Jeroni Paul, Sep 4, 2003, in forum: Javascript
    Replies:
    2
    Views:
    120
    Jeroni Paul
    Sep 5, 2003
  4. Chuck
    Replies:
    0
    Views:
    271
    Chuck
    Nov 10, 2003
  5. Replies:
    1
    Views:
    161
Loading...

Share This Page