3

I registered a handler for the KeyDown event of my Windows Forms form. I want to react to pressing of Ctrl and +. Here I mean the + key which is not in the calculator part of the keyboard. While debugging I saw that if I press only this key, the KeyCode of the KeyEventArgs object is LButton | RButton | Back | ShiftKey | Space | F17. Then I tested some other keys. Also for some simple number and letter keys I get this code. Can anyone please tell me why this is the case? Furthermore I cannot understand why a click of the mouse buttons are given as Keys values. And why are they in the KeyCode if I only pressed some key? But the F17 key is still stranger. As far as I know a keyboard has only function keys from F1 to F12. What is this F17 key?

The handler is the following method:

private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
    if(e.KeyCode == Keys.Escape)
    {
        Close();
        e.Handled = true;
    }
    else if((e.Modifiers==Keys.Control)&&(e.KeyCode==Keys.Add))
    {
        /* omitted */
        e.Handled = true;
    }
    else if((e.Modifiers==Keys.Control)&&(e.KeyCode == Keys.Subtract)&&(lbFiles.SelectedItems.Count>0))
    {
        /* omitted */
        e.Handled = true;
    }
}

This handler is registered by the forms designer.

user3570134
  • 109
  • 8
  • 1
    show the code where you have done the registering of the event handlers also show your code where you are checking for the Key concat meaning which combination keys you have pressed – MethodMan Aug 20 '14 at 18:35
  • [MSDN Key Enumerations](http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.input.keys.aspx) this will explain the keyboards and what each Key does || Means – MethodMan Aug 20 '14 at 18:37
  • But why do I get the values for the control key if I only press the Add key and no other key, also no control key? – user3570134 Aug 20 '14 at 18:50
  • take a look at this posting as well has everything you need in the accepted answer [KeyCodes](http://stackoverflow.com/questions/8644083/keydown-in-c-sharp-doesnt-work-for-some-reason) – MethodMan Aug 20 '14 at 18:53
  • Sorry, but I do not really understand what you mean. If it is the Preview property, it is set to true. – user3570134 Aug 20 '14 at 18:57
  • Set thru the code I bet you have an `&&` condition where you probably need to use an `||` conditional as well – MethodMan Aug 20 '14 at 19:00
  • Where will that have any effects to the value of e.KeyCode or e.KeyData which I consider while debugging? – user3570134 Aug 20 '14 at 19:07
  • good luck they closed your post.. DEBUG your code – MethodMan Aug 20 '14 at 19:16
  • Does you mean I should use (e.KeyData & Keys.Add) == Keys.Add as a part of condition? This expression is also false. – user3570134 Aug 20 '14 at 19:33
  • don't you mean `e.KeyData && Keys.Add)` there is a difference between `&` and `&&` think of Bitwise which would be `|` vs `||` – MethodMan Aug 20 '14 at 19:34
  • I haven't a clue. Can you please simply say me the correct condition. – user3570134 Aug 20 '14 at 19:38
  • I would actually simply say as well as ask again.. what do you see when you are debugging the code ? can you perhaps also change your if code to a Switch Case..? – MethodMan Aug 20 '14 at 19:40
  • switch (e.KeyCode) { case Keys.Shift: case Keys.CapsLock: case Keys.Tab: // ... case Keys.L: break; default: // Do some work... break; } – MethodMan Aug 20 '14 at 19:42
  • I can't believe that you are getting a key combination when you press a single key.. I try the code with simple keys and do not get the same results.. – MethodMan Aug 20 '14 at 19:43
  • I only have a breakpoint in the first line of code of the handler method. There I watch the value of `e.KeyData` and `e.KeyCode`. So the handler code does not have any effects to the watched values. Then I also watched some other expressions like `(e.KeyData & Keys.Add) == Keys.Add` at this point. This expression checks whether the `Keys` value contains the `Add` value. – user3570134 Aug 20 '14 at 19:53
  • A such switch expression with the Keys values of my question does also not work here on my machine. – user3570134 Aug 20 '14 at 20:07
  • good luck I am sorry I do not believe you are doing something correctly this is something extremely simple.. try to stop VisualStudio and restart and see if your program changes or starts working .. wonder also what your Culture settings for your local Environment are as well... – MethodMan Aug 20 '14 at 20:09
  • A restart of VS has no effects. But it is actually clear that the switch expression will not work. Because it is equal to a disjunction of equality tests of a combined value and uncombined values. And a combination of the simple values is not equal to any of this simple values. For testing whether a combined value contains a simple value you need the bitwise expression I wrote before. – user3570134 Aug 20 '14 at 20:30

1 Answers1

0

Your answer is in the documentation for the Keys enumeration...

http://msdn.microsoft.com/en-us/library/system.windows.forms.keys%28v=vs.110%29.aspx

Do not use the values in this enumeration for combined bitwise operations. The values in the enumeration are not mutually exclusive.

KeyCode is a single value and the comparison should be equality only.

RomSteady
  • 398
  • 2
  • 13