1

In my application I have:

private bool _clear = true;

This boolean is used to see if a textbox should be cleared or not when user enters a new text into it (by pressing on a TreeNode in a TreeView).

Then I have these two events for my form:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Modifiers == Keys.Control)
    {
        _clear = false;
    }

}

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Modifiers == Keys.Control)
    {
        _clear = true;
    }
}

I want it somehow when user is holding the CTRL key, clear be FALSE and when CTRL is released, clear goes back to TRUE.

Obviously the code I wrote here, does not work! what can be wrong and/or is there a better way?

KreepN
  • 8,528
  • 1
  • 40
  • 58
Dumbo
  • 13,555
  • 54
  • 184
  • 288

3 Answers3

2

It's a simple fix, as when you release the key, the KeyUp event does not receive any info of the key released itself, so just set the property to true:

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
   _clear = true;       
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.Modifiers == Keys.Control)
   {
      _clear = false;
   }
}

If you want to see it work in real time, add a label to your form and add this under each setting of the '_clear' variable:

label1.Text = _clear.ToString();

Per your comment, change the second code block to:

if (e.KeyData.ToString() == "ControlKey, Control")
{
   _clear = false;                  
}
else if(other shortcut conditionals go here or on other else if's)
{
   _clear = true;
}

The only time this conditional will hold true is when control is held by itself. The else case is there for the purpose of setting _clear to true when you press ctrl followed by another key, due to the fact that as soon as you press control, it will fire the KeyDown event.

Based on this change, as long as you take care of the key presses following that if statement, (such as else if()'s), you will not need to set anything in the KeyUp event.

See my answer here to the intricacies of keys and their properties if you want some more in-depth info.

Edit #3 :

As long as you set the _clear to true on the first line in each conditional, you should be able to avoid the problem you are facing in your comment:

private void Form1_KeyDown(object sender, KeyEventArgs e)
{

   if (e.KeyData.ToString() == "ControlKey, Control")
   {
      _clear = false;

   }
   else if(e.KeyData.ToString() == "O, Control")
   {
       _clear = true;
       //Do other stuff here, such as opening a file dialog

   }

}
Community
  • 1
  • 1
KreepN
  • 8,528
  • 1
  • 40
  • 58
  • Thanks, I tried this also, but there is a problem. I have other shortcuts like `CTRL+A` and that sort of stuf. If I press CTRL+A for another purpose (It is a shortcut for a menu item), this will not be a reliable solution. Is there a way around ? – Dumbo Nov 28 '11 at 18:09
  • 1
    Fixed, check the bottom of the answer. – KreepN Nov 28 '11 at 19:38
  • Hi again, I tried this way also. The problem unforunately still exist. It happens when I press for example `CTRL+O` to open a FileOpenDialog, the _clear will stay false even after the dialog is closed, so I have to press CTRL one more time. is it possible to avoid this behaviour? – Dumbo Nov 29 '11 at 08:24
  • Well at the moment I am using the SHIFT key for this purpose, since it is not a shortcut for anything, it works good. Would be nice to solve the problem for control key though! – Dumbo Nov 29 '11 at 08:32
2

It is much easier if you do this the other way around. Check if the CTRL key is down in the treeview's event. Something like this:

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {
        if ((Control.ModifierKeys & Keys.Control) == Keys.Control) {
            // Control key is down, do something...
        }
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
1

You need to change the KeyPreview property of your form to True.

Mazen313
  • 520
  • 4
  • 14
  • Thanks, this partly worked, it makes the `_clear` False, but the `KeyUp` event somehow does not work and _clear stays false. any ideas? – Dumbo Nov 28 '11 at 15:04
  • i tried a test form your problem doesn't appear, in debug mode you will only see one event triggering but the 2 events are firing check the result on key-up event – Mazen313 Nov 28 '11 at 15:24