textBox1_KeyUp doesn't work but textBox1_KeyDown works fine

T

Tony Johansson

Hello!

I have dragged a textbox from the toolbox into the form and then used the
designer to create an event handler
named textBox1_KeyUp and a textBox1_KeyDown eventhandler
The eventhandler textBox1_KeyDown works correct. If I hold down for example
control in the textbox field the text KeyDown CTRL is written in the console
window.

Now to my question eventhandler textBox1_KeyUp doesn't work.If I hold down
control in the textbox field this statement e.Control == true is still false
why ?

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control == true)
Console.WriteLine("keyUp CTRL");
if (e.Alt == true)
Console.WriteLine("KeyUp ALT");
if (e.Shift == true)
Console.WriteLine("KeyUp SHIFT");
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control == true)
Console.WriteLine("KeyDown CTRL");
if (e.Alt == true)
Console.WriteLine("KeyDown ALT");
if (e.Shift == true)
Console.WriteLine("KeyDown SHIFT");
}

//Tony
 
J

Jeff Johnson

I have dragged a textbox from the toolbox into the form and then used the
designer to create an event handler
named textBox1_KeyUp and a textBox1_KeyDown eventhandler
The eventhandler textBox1_KeyDown works correct. If I hold down for
example control in the textbox field the text KeyDown CTRL is written in
the console window.

Now to my question eventhandler textBox1_KeyUp doesn't work.If I hold down
control in the textbox field this statement e.Control == true is still
false why ?

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.Control == true)
Console.WriteLine("keyUp CTRL");
if (e.Alt == true)
Console.WriteLine("KeyUp ALT");
if (e.Shift == true)
Console.WriteLine("KeyUp SHIFT");
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control == true)
Console.WriteLine("KeyDown CTRL");
if (e.Alt == true)
Console.WriteLine("KeyDown ALT");
if (e.Shift == true)
Console.WriteLine("KeyDown SHIFT");
}

You're misunderstanding the usage of the Control, Alt, and Shift properties
(but it's understandable because this is kind of confusing). They are there
to let you know if a modifier key is down in addition to another key. They
do not technically indicate the key that caused the KeyDown/Up event. This
makes it easy to determine if, for example, the user has pressed Ctrl+A
instead of just A.

When you press only the Ctrl key, the KeyDown event reports two things to
you: The physical Ctrl key is down AND the Control MODIFIER is active. When
you release the key, the KeyUp event reports that the physical Ctrl key is
what has gone up and that the Control modifier is not active (because, as we
just said, the Ctrl key is no longer down).

In other words, the system doesn't remember the state of the Control key
modifier and report it to you on KeyUp; instead it examines the state of the
modifier keys at that very moment and reports them to you.

This means that if you press Ctrl, then A, then release Ctrl, and finally
release A, you'll get the following events:

KeyDown: Key = Control, Modifier = Control
KeyDown: Key = A, Modifier = Control
KeyUp: Key = Ctrl, Modifier = None
KeyUp: Key = A, Modifier = None

This is important, because if you use the KeyUp event to decide that a key
sequence has taken place (which is sometimes the better thing to do, as
opposed to KeyDown), then you have to hope your users release the main key
before releasing the modifier key. Of course, this is what people normally
do anyways, but it's good to know that such a condition can occur. Normally
the key sequence would be Ctrl down, A down, A up, Ctrl up, and therefore on
KeyUp you'll know that the user just pressed (and released) Ctrl+A.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top