Checking a bitwise enumeration

  • Thread starter Thread starter Paul E Collins
  • Start date Start date
P

Paul E Collins

Suppose I have a few Keys objects:

Keys k1 = Keys.V; // V
Keys k2 = Keys.Control | Keys.V; // Ctrl+V
Keys k3 = Keys.Shift | Keys.J; // Shift+J

I need to determine which of these include the Keys.V element,
regardless of any other keys. I know it will be a bitwise comparison,
but I can't work out the correct syntax to use.

P.
 
Try this

if((k1 & Keys.V)== Keys.V)
MessageBox.Show("key found in k1");

if((k2 & Keys.V) == Keys.V)
MessageBox.Show("key found in k2");

if((k3 & Keys.V) == Keys.V)
MessageBox.Show("key found in k3");
 
Paul said:
Suppose I have a few Keys objects:

Keys k1 = Keys.V; // V
Keys k2 = Keys.Control | Keys.V; // Ctrl+V
Keys k3 = Keys.Shift | Keys.J; // Shift+J

I need to determine which of these include the Keys.V element,
regardless of any other keys. I know it will be a bitwise comparison,
but I can't work out the correct syntax to use.

Use the Keys.KeyCode enum value to isolate only the keycode portion of
the value (ie., get rid of any modifier bits):

bool isVKey = ((k2 & Keys.KeyCode) == Keys.V);
 
Hi Paul,

Be adviced that you may check for Key modifiers like Control, Shiftm and Alt
because they are specific bits, but you cannot check for keys in any cases
becuse they are just a numbers. The are not meant to be check like that. So
bitwise operations won't do you have to go with the not so cool looking
*if* or *switch* and check KeyEventArgs.KeyCode property in order to get rid
of the modifiers.

Just to demosntrate my point
Keys key = Keys.K;
Console.WriteLine((key & Keys.J)!=0);
Console.WriteLine((key & Keys.K)!=0);

both print *true*

that is because
Key.J = 74 -> 1001010
Key.K = 75 -> 1001011

See they differ only in one bit K has all the (1) bits of J so if you have K
there is now way via bitwise operations to make the difference
 
Hi Stoitcho,

Stoitcho Goutsev (100) said:
Hi Paul,

Be adviced that you may check for Key modifiers like Control, Shiftm and Alt
because they are specific bits, but you cannot check for keys in any cases
becuse they are just a numbers. The are not meant to be check like that. So
bitwise operations won't do you have to go with the not so cool looking
*if* or *switch* and check KeyEventArgs.KeyCode property in order to get rid
of the modifiers.

Just to demosntrate my point
Keys key = Keys.K;
Console.WriteLine((key & Keys.J)!=0);
Console.WriteLine((key & Keys.K)!=0);

both print *true*

that is because
Key.J = 74 -> 1001010
Key.K = 75 -> 1001011

See they differ only in one bit K has all the (1) bits of J so if you have K
there is now way via bitwise operations to make the difference

You can do this:

Keys key = Keys.K;
Console.WriteLine((key & ~Keys.Modifiers)==Keys.J); // prints false
Console.WriteLine((key & ~Keys.Modifiers)==Keys.K); // prints true

Regards,
Daniel
 
That's right,
this is the same as
Keys key = Keys.K;
Console.WriteLine(key.KeyCode == Keys.J); // prints false
Console.WriteLine(key.KeyCode == Keys.K); // prints true

This is exactly what I said. One cannot use bitwise operations to find out
the key pressed, but bitwise operations can be used to find the modifier
key.
 
Oops. I meant my example to look more like this:

Keys key = Keys.K | Keys.Shift;
Console.WriteLine((key & ~Keys.Modifiers)==Keys.J); // prints false
Console.WriteLine((key & ~Keys.Modifiers)==Keys.K); // prints true

I'm not disagreeing with you, btw. I think it comes down to semantics. I am
using bitwise operations to find out the key pressed, but your point is well
taken.

Regards,
Daniel

Stoitcho Goutsev (100) said:
That's right,
this is the same as
Keys key = Keys.K;
Console.WriteLine(key.KeyCode == Keys.J); // prints false
Console.WriteLine(key.KeyCode == Keys.K); // prints true

This is exactly what I said. One cannot use bitwise operations to find out
the key pressed, but bitwise operations can be used to find the modifier
key.



--

Stoitcho Goutsev (100) [C# MVP]


Daniel Pratt said:
You can do this:

Keys key = Keys.K;
Console.WriteLine((key & ~Keys.Modifiers)==Keys.J); // prints false
Console.WriteLine((key & ~Keys.Modifiers)==Keys.K); // prints true

Regards,
Daniel
 
Actually you don't discover the key using a bitwise operation. You use &
just to remove the modifers from the key value.

If there were no modifiers the only thing that you would do is

Keys key = Keys.K;
Console.WriteLine(key == Keys.J); // prints false
Console.WriteLine(key ==Keys.K); // prints true
there is no bitwise operation.

What I understand by "using bitwise operations for..." is
if we could do (but we can't)

key = Keys.K | Keys.J

how would I find whether K has been pressed?

if((key & Keys.K) != 0)
{
K is pressed
}

But because key codes are not a bit flags we can not actually do that.

Only thing that we can use bitwise operations is to remove modifer keys as
you suggested, but less verbose, I believe, is to use KeyCode that what it's
for

--

Stoitcho Goutsev (100) [C# MVP]


Daniel Pratt said:
Oops. I meant my example to look more like this:

Keys key = Keys.K | Keys.Shift;
Console.WriteLine((key & ~Keys.Modifiers)==Keys.J); // prints false
Console.WriteLine((key & ~Keys.Modifiers)==Keys.K); // prints true

I'm not disagreeing with you, btw. I think it comes down to semantics. I am
using bitwise operations to find out the key pressed, but your point is well
taken.

Regards,
Daniel

Stoitcho Goutsev (100) said:
That's right,
this is the same as
Keys key = Keys.K;
Console.WriteLine(key.KeyCode == Keys.J); // prints false
Console.WriteLine(key.KeyCode == Keys.K); // prints true

This is exactly what I said. One cannot use bitwise operations to find out
the key pressed, but bitwise operations can be used to find the modifier
key.



--

Stoitcho Goutsev (100) [C# MVP]


Daniel Pratt said:
You can do this:

Keys key = Keys.K;
Console.WriteLine((key & ~Keys.Modifiers)==Keys.J); // prints false
Console.WriteLine((key & ~Keys.Modifiers)==Keys.K); // prints true

Regards,
Daniel
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top