Hi:
My guess is that you defined Keys as:
enum Keys {Left, Right}
So below is what happened in your original code:
Keys keyStates = new Keys(); // keyStates is initialized as "0", which is
Keys.Left
keyStates |= Keys.Left; // Now keyStates is 0 which is
Keys.Left (0|0 = 0)
keyStates |= Keys.Right // keyState is 1 (0|1 = 1) which is
Key.Right
keyStates -= Keys.Left // now keyState is still 1, 1-0 = 1,
which is Keys.Right.
You could try this:
[Flags] // indicating enum vars below can be treated using bit operations
enum Keys {Left=1, Right=2}
Keys keyStates = new Keys(); // keyStates is initialized as "0", which is
Keys.Left
keyStates = Keys.Left; // keyStates = 1;
keyStates |= Keys.Right // keyState=3 (Left, Right);
keyStates -= Keys.Left; // keyState=2. keyStates &= ~Keys.Left
works too.
HTH
Robin Theilade said:
Thanks Rudy!
I think your answer is correct but I can't validate it since my test code
bugs me at the moment...can you see what is wrong? When I OR the current
keyState (Keys.Left) with Keys.Right it equals "Keys.Right" and not
"Keys.Left, Keys.Right" as I would expect..
using System;
using System.Windows.Forms;
namespace Test
{
public class StaticTest
{
static void Main()
{
Console.WriteLine( "- STATIC -" );
Keys keyStates = Keys.None;
Console.WriteLine( "Should be 'None': " + keyStates );
keyStates |= Keys.Left;
Console.WriteLine( "Added 'Left': " + keyStates );
keyStates |= Keys.Right;
Console.WriteLine( "Added 'Right': " + keyStates );
keyStates &= ~Keys.Right;
Console.WriteLine( "Substracted 'Right': " + keyStates );
Console.ReadLine();
}
}
}
Best Regards,
Robin Theilade
At 16:08:36, 13.03.2004, Robin Theilade wrote:
keyStates |= Keys.Left;
keyStates |= Keys.Right
keyStates -= Keys.Left // here is the problem
keyStates &= ~Keys.Left.
--
Rudy Velthuis
"A man can't get rich if he takes proper care of his family."
- Navaho saying.