tags:

views:

1228

answers:

4

I was trying to test whether the Alt key was pressed.

I had a check similar to:

private void ProcessCmdKey(Keys keyData)
{
 if (keyData == Keys.Alt)
 {
  System.Console.WriteLine ("Alt Key Pressed");
 } 
}

Anyways needless to say when I breakpointed when I had pressed the Alt key the debugger told me the key that was pressed was actually Keys.RButton | Keys.ShiftKey | Keys.Alt

Can anyone shed some light on what is going on or perhaps point me to an article that can explain?

Thanks FZ

Edit: I am still a bit lost as to why the ENUM would have have other bit values set and not simply the Alt key? I understand that the enum can include more than 1 state with the flags attrivbute but I am not sure why it does if all I pressed was Alt?

+7  A: 

If you want to test whether Alt is part of the pressed keys, you can perform a bitwise test;

if((keyData & Keys.Alt) == Keys.Alt) {...}
Marc Gravell
Beat me to it...
Donut
Which is better from a coding perspective? == Keys.Alt or != 0?
Yuriy Faktorovich
"== Keys.Alt" is the best form. In any case where the value (Keys.Alt in this case) is not a single bit value, the !=0 test will give false positives. Although in this cae there is no difference, it is better practice to be precise so that you never get caught out by a case where !=0 will fail.
Jason Williams
Jason Williams
@Jason good idea. Check out the functions below http://stackoverflow.com/questions/1369312/c-keys-enumeration-confused-keys-alt-or-keys-rbutton-keys-shiftkey-keys-alt/2033796#2033796
cdiggins
+5  A: 

Keys is a Flags Enumeration. This means it can have more than one value at a given time. You should check it like so:

if ( (keyData & Keys.Alt) == Keys.Alt)
{
   // Alt was pressed!
}
Reed Copsey
+1  A: 

Enum with FlagsAttribute are implemented using bits.
See this link for a good start - http://msdn.microsoft.com/en-us/library/cc138362.aspx

EDIT: Are you pressing RIGHT (mouse button) with Shift key during the operation, to select/highlight something, while debugging?

shahkalpesh
I am still a bit lost as to why the ENUM would have have other bit values set and not simply the Alt key?I understand that the enum can include more than 1 state but I am not sure why it does if all I pressed was Alt?
Setheron
Nope. I had simply placed the breakpoint at the start of the method and hit the ALT key.
Setheron
I have to add that I am using a 3rd party GUI toolkit, so there may be a chance they are adding to the key to be processed, but i hope they aren't.
Setheron
Assuming you don't add code to handle ALT, will the control take some action when you press ALT?
shahkalpesh
shahkalpesh
+1  A: 

Mark's technique (the accepted answer) works for modifier keys, but it caught me by surprise that some keys (e.g. arrows) are combinations of bits, and won't work. For example the following test turns out to be true:

((Keys.Right & Keys.Left) == Keys.Left) 

I have posted some useful little functions for key handling on a related StackOverflow post regarding arrow key handling.

cdiggins