Get mousebuttonstate in NS

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Silvio Bierman

    #1

    Get mousebuttonstate in NS

    Hello,

    In an event handler for onmousemove I want to know if a mousebutton is
    currently prtessed. In IE "event.butt on == 1" works fine for me. In NS I
    always get event.button == 0 in onmousemove event if the mousebutton is
    pressed.

    How do I get the mousebutton state?

    TIA

    Silvio Bierman


  • Janwillem Borleffs

    #2
    Re: Get mousebuttonstat e in NS

    Silvio Bierman wrote:[color=blue]
    > In an event handler for onmousemove I want to know if a mousebutton is
    > currently prtessed. In IE "event.butt on == 1" works fine for me. In
    > NS I always get event.button == 0 in onmousemove event if the
    > mousebutton is pressed.
    >
    > How do I get the mousebutton state?
    >[/color]

    By adding a listener for the mousedown event:
    document.addEve ntListener("mou sedown", showButtonFunct ion, false);


    JW



    Comment

    • DU

      #3
      Re: Get mousebuttonstat e in NS

      Silvio Bierman wrote:
      [color=blue]
      > Hello,
      >
      > In an event handler for onmousemove I want to know if a mousebutton is
      > currently prtessed. In IE "event.butt on == 1" works fine for me. In NS I
      > always get event.button == 0 in onmousemove event if the mousebutton is
      > pressed.
      >
      > How do I get the mousebutton state?
      >
      > TIA
      >
      > Silvio Bierman
      >
      >[/color]


      You must query the button property of the created event object once
      you've created an event listener in Mozilla-based browsers.
      "0" is the left button, "1" is the middle button and "2" is the right
      button.

      E.g.:

      function init()
      {
      document.addEve ntListener("mou sedown", showButtonFunct ion, false);
      }

      function showButtonFunct ion(evt)
      {
      document.getEle mentById("idPar g").style.visib ility = "visible";
      var txtMouseButton = evt.button == "0" ? "left" : evt.button == "1" ?
      "middle" : "right";
      document.getEle mentById("idMou seButton").firs tChild.nodeValu e =
      txtMouseButton;
      }
      // this code could be further improved and made more robust
      </script>

      </head>

      <body onload="init(); ">

      <p id="idParg" style="visibili ty:hidden;">You just have clicked the
      button of your <span id="idMouseButt on">&nbsp;</span> mouse.</p>

      Tested and working without a problem in Mozilla 1.6 beta, NS 7.1 and
      K-meleon 0.8.2.

      DU

      Comment

      • Silvio Bierman

        #4
        Re: Get mousebuttonstat e in NS

        OK guys,

        That is what I thought. I have solved it that way but thought there might be
        a more elegant way of querying the button states during any other event. I
        now use a document level event that sets/clear global button-down flags for
        use in other event handlers, not nice but it works.

        Thanks,

        Silvio Bierman


        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Get mousebuttonstat e in NS

          Silvio Bierman wrote:
          [color=blue]
          > In an event handler for onmousemove I want to know if a mousebutton is
          > currently prtessed. In IE "event.butt on == 1" works fine for me. In NS I
          > always get event.button == 0 in onmousemove event if the mousebutton is
          > pressed. How do I get the mousebutton state?[/color]

          Use the "which" property instead and be sure to pass and use
          a reference to the local event object ("event" in an intrinsic
          HTML event handler attribute) in the case the IE-proprietary
          global "event" object is undefined.


          PointedEars

          Comment

          Working...