how do my program know that both buttons are down
-
In C# how do my app get true wen both mouse buttons are down?
-
In C# how do my app get true wen both mouse buttons are down?
The
MouseEventArgs
class has a propertyButton
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
-
The
MouseEventArgs
class has a propertyButton
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
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
-
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
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;
} -
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;
}lol, that wont work buddy :p :laugh::laugh: top secret
Download xacc-ide 0.0.3 now! -
lol, that wont work buddy :p :laugh::laugh: top secret
Download xacc-ide 0.0.3 now!What you mean? I badly speak english.
-
What you mean? I badly speak english.
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 -
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 screenshotsYou 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.
-
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.
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