Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how do my program know that both buttons are down

how do my program know that both buttons are down

Scheduled Pinned Locked Moved C#
csharpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Umair Ahmad khan
    wrote on last edited by
    #1

    In C# how do my app get true wen both mouse buttons are down?

    M 1 Reply Last reply
    0
    • U Umair Ahmad khan

      In C# how do my app get true wen both mouse buttons are down?

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      The MouseEventArgs class has a property Button that will tell you that. It's a [Flags] enumeration, so more than one value can be OR'ed to build the final value. So your event handler would look somethng like this:

      private void MyApp_MouseDown(object sender, MouseEventArgs e)
      {
      if ((e.Button & MouseButtons.Left) == MouseButtons.Left &&
      (e.Button & MouseButtons.Right) == MouseButtons.Right)
      {
      MessageBox.Show("Both buttons down");
      }
      }

      Regards, mav

      U 1 Reply Last reply
      0
      • M mav northwind

        The MouseEventArgs class has a property Button that will tell you that. It's a [Flags] enumeration, so more than one value can be OR'ed to build the final value. So your event handler would look somethng like this:

        private void MyApp_MouseDown(object sender, MouseEventArgs e)
        {
        if ((e.Button & MouseButtons.Left) == MouseButtons.Left &&
        (e.Button & MouseButtons.Right) == MouseButtons.Right)
        {
        MessageBox.Show("Both buttons down");
        }
        }

        Regards, mav

        U Offline
        U Offline
        Umair Ahmad khan
        wrote on last edited by
        #3

        no... this code does not work correctly .. like if i press both buttons at the same time nothing happen... like if you are playing minesweeper you press both buttons ... i have tried this code but it did not work for me ... Is there any thing else that i can do

        V 1 Reply Last reply
        0
        • U Umair Ahmad khan

          no... this code does not work correctly .. like if i press both buttons at the same time nothing happen... like if you are playing minesweeper you press both buttons ... i have tried this code but it did not work for me ... Is there any thing else that i can do

          V Offline
          V Offline
          Vadim Gusev
          wrote on last edited by
          #4

          private bool leftButtonPressFlag = false;
          private bool rightButtonPressFlag = false;
          private void Form1_MouseDown(object sender, MouseEventArgs e)
          {
          if (e.Button == MouseButtons.Left) leftButtonPressFlag = true;
          if (e.Button == MouseButtons.Right) rightButtonPressFlag = true;
          if (leftButtonPressFlag && rightButtonPressFlag)
          {
          Color swap = ForeColor;
          ForeColor = BackColor;
          BackColor = swap;
          }
          }

          private void Form1_MouseUp(object sender, MouseEventArgs e)
          {
          if (e.Button == MouseButtons.Left) leftButtonPressFlag = false;
          if (e.Button == MouseButtons.Right) rightButtonPressFlag = false;
          }

          L 1 Reply Last reply
          0
          • V Vadim Gusev

            private bool leftButtonPressFlag = false;
            private bool rightButtonPressFlag = false;
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
            if (e.Button == MouseButtons.Left) leftButtonPressFlag = true;
            if (e.Button == MouseButtons.Right) rightButtonPressFlag = true;
            if (leftButtonPressFlag && rightButtonPressFlag)
            {
            Color swap = ForeColor;
            ForeColor = BackColor;
            BackColor = swap;
            }
            }

            private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
            if (e.Button == MouseButtons.Left) leftButtonPressFlag = false;
            if (e.Button == MouseButtons.Right) rightButtonPressFlag = false;
            }

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            lol, that wont work buddy :p :laugh::laugh: top secret
            Download xacc-ide 0.0.3 now!

            V 1 Reply Last reply
            0
            • L leppie

              lol, that wont work buddy :p :laugh::laugh: top secret
              Download xacc-ide 0.0.3 now!

              V Offline
              V Offline
              Vadim Gusev
              wrote on last edited by
              #6

              What you mean? I badly speak english.

              L 1 Reply Last reply
              0
              • V Vadim Gusev

                What you mean? I badly speak english.

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                Just try the code and you will see. You need to combine (OR) the flagged values else they will never be true. :) top secret
                Download xacc-ide 0.0.3 now!
                See some screenshots

                V 1 Reply Last reply
                0
                • L leppie

                  Just try the code and you will see. You need to combine (OR) the flagged values else they will never be true. :) top secret
                  Download xacc-ide 0.0.3 now!
                  See some screenshots

                  V Offline
                  V Offline
                  Vadim Gusev
                  wrote on last edited by
                  #8

                  You are mistaken. Need AND. This code works correctly. It is possible to write so:

                  private bool buttonPressFlag = false;
                  private void Form1_MouseDown(object sender, MouseEventArgs e)
                  {
                  if (buttonPressFlag)
                  {
                  Color swap = ForeColor;
                  ForeColor = BackColor;
                  BackColor = swap;
                  }
                  if ((e.Button & MouseButtons.Left) == MouseButtons.Left ||
                  (e.Button & MouseButtons.Right) == MouseButtons.Right)
                  buttonPressFlag = true;
                  }

                  private void Form1_MouseUp(object sender, MouseEventArgs e)
                  {
                  if ((e.Button & MouseButtons.Left) == MouseButtons.Left ||
                  (e.Button & MouseButtons.Right) == MouseButtons.Right)
                  buttonPressFlag = false;
                  }

                  PS: Excuse me. At first time I has not understood your answer.

                  L 1 Reply Last reply
                  0
                  • V Vadim Gusev

                    You are mistaken. Need AND. This code works correctly. It is possible to write so:

                    private bool buttonPressFlag = false;
                    private void Form1_MouseDown(object sender, MouseEventArgs e)
                    {
                    if (buttonPressFlag)
                    {
                    Color swap = ForeColor;
                    ForeColor = BackColor;
                    BackColor = swap;
                    }
                    if ((e.Button & MouseButtons.Left) == MouseButtons.Left ||
                    (e.Button & MouseButtons.Right) == MouseButtons.Right)
                    buttonPressFlag = true;
                    }

                    private void Form1_MouseUp(object sender, MouseEventArgs e)
                    {
                    if ((e.Button & MouseButtons.Left) == MouseButtons.Left ||
                    (e.Button & MouseButtons.Right) == MouseButtons.Right)
                    buttonPressFlag = false;
                    }

                    PS: Excuse me. At first time I has not understood your answer.

                    L Offline
                    L Offline
                    leppie
                    wrote on last edited by
                    #9

                    Lets go back to your original code listing :)

                    private bool leftButtonPressFlag = false;
                    private bool rightButtonPressFlag = false;

                    private void Form1_MouseDown(object sender, MouseEventArgs e)
                    {
                    if (e.Button == MouseButtons.Left) leftButtonPressFlag = true;
                    if (e.Button == MouseButtons.Right) rightButtonPressFlag = true;
                    if (leftButtonPressFlag && rightButtonPressFlag)
                    {
                    Color swap = ForeColor;
                    ForeColor = BackColor;
                    BackColor = swap;
                    }
                    }

                    private void Form1_MouseUp(object sender, MouseEventArgs e)
                    {
                    if (e.Button == MouseButtons.Left) leftButtonPressFlag = false;
                    if (e.Button == MouseButtons.Right) rightButtonPressFlag = false;
                    }

                    The correct way will be as such:

                    if ((e.Button & MouseButtons.Left) != 0) leftButtonPressFlag = true;
                    if ((e.Button & MouseButtons.Right) != 0) rightButtonPressFlag = true;
                    if (leftButtonPressFlag && rightButtonPressFlag)

                    This is becos MouseButtons is a Flagged enum. Lets look at some real values now.

                    [Flags]
                    enum MouseButtons
                    {
                    Left = 0x 0010 0000,
                    ....
                    Right = 0x 0020 0000,
                    ....
                    }

                    Now the value of e.Buttons, if both buttons were pressed, would be 0x 0030 0000 (Left | Right), you should see now why your method will fail try to match either value, and hence will not work correctly. Note for single buttons your method will indeed work, but will fail matching combinations of buttons. I hope you understand :) If not, read abit about the binary (not boolean) operators of AND, OR, XOR, 1's complement. The windows calculator can actually help alot here! I remember it took me a good few months to understand it fully. :) top secret
                    Download xacc-ide 0.0.3 now!
                    See some screenshots

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups