6

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?

Setheron
  • 3,520
  • 3
  • 34
  • 52

4 Answers4

6

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Which is better from a coding perspective? == Keys.Alt or != 0? – Yuriy Faktorovich Sep 02 '09 at 18:38
  • 1
    "== 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 Sep 02 '09 at 19:22
  • P.S. The cleanest way is actually to implement a library method that encapsulates this test in a human-readable and easily typed form. It's easy to type && or | when you meant &. Using if (IsBitSet(keyData, Keys.Alt)) is more readable and less error prone. – Jason Williams Sep 02 '09 at 19:25
  • @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 Jan 09 '10 at 15:14
  • This doesn't answer the OP's question. And I'm very curious about knowing that! – Áxel Costas Pena Mar 18 '15 at 13:50
  • @ÁxelCostasPena can you be specific about how it doesn't answer it? – Marc Gravell Mar 18 '15 at 14:02
  • Title: "C# Keys Enumeration Confused: Keys.Alt or Keys.RButton | Keys.ShiftKey | Keys.Alt". Body resume: "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?" The phenomenon of the ghost keypresses hasn´t been explained, only a workaround provided. – Áxel Costas Pena Mar 18 '15 at 20:06
  • Is the confusing part that Microsoft did not use powers of two for each of the Keys? For example the Enter key enum is 13. Which shows in the debugger as LButton | MButton | Back (1 + 4 + 8). – John B Feb 12 '16 at 01:21
4

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
  • 554,122
  • 78
  • 1,158
  • 1,373
  • Doesn't answer the OP's question – Áxel Costas Pena Mar 18 '15 at 13:51
  • @ÁxelCostasPena Not sure I agree - it explains why the issue exists, and shows how to check for it correctly. What else do you want? – Reed Copsey Mar 18 '15 at 18:46
  • Title: "C# Keys Enumeration Confused: Keys.Alt or Keys.RButton | Keys.ShiftKey | Keys.Alt". Body resume: "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?" The phenomenon of the ghost keypresses hasn´t been explained, only a workaround provided. – Áxel Costas Pena Mar 18 '15 at 20:06
  • @ÁxelCostasPena The phenomenon is exactly what I posted - it's a flags enum, which means that it turns on Alt, but also that it was the right alt button, etc. – Reed Copsey Mar 18 '15 at 20:10
  • I am still not understanding why a flag ShiftKey is being set when I just press Alt. And I think the OP's scenario is the same. – Áxel Costas Pena Mar 19 '15 at 08:37
1

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
  • 33,172
  • 3
  • 63
  • 88
  • 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 Sep 02 '09 at 18:53
  • Nope. I had simply placed the breakpoint at the start of the method and hit the ALT key. – Setheron Sep 02 '09 at 18:59
  • 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 Sep 02 '09 at 19:00
  • Assuming you don't add code to handle ALT, will the control take some action when you press ALT? – shahkalpesh Sep 02 '09 at 19:04
  • Remove the breakpoint & put statement like Console.WriteLine(Keys) to see what it has to say? – shahkalpesh Sep 02 '09 at 19:06
1

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.

Community
  • 1
  • 1
cdiggins
  • 17,602
  • 7
  • 105
  • 102