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?
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) {...}
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!
}
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With