How to trap Control+Key on KeyDown

J

james

I am trying to trap Ctrl+A in my custom editor by handling
the KeyDown event. But what I have found is that the
key down event only contains the ControlKey on the
key down and the letter A is never passed as a KeyDown
as long as the Control key is held down. It does come through
as a KeyUp when released but that doesn't help me
because I need to handle the key immediatley when it
goes down. So is this a problem in .Net or is there some
other way to get what I want ?

thanks,

JIM
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi james,

What kind of custom editor are you developing. Is it some kind of special
type of editor e.g. UITypeEditor.

Or it is just a normal user control.

Anyways, it depends on what property of KeyEventArgs you check.

Bare in mind that Keys.ControlKey is the key "Ctrl", but Keys.Control is the
modifier 'Control'

When you get an KeyDown event object of type KeyEventArgs is passed along
with the event

That object has one property called KeyCode and one KeyData (KeyValue is the
same as KeyData, but casted to int).
KeyCode is the code of the pressed key without any modifiers (CTRL, ALT or
SHIFT); KeyData, though, is the same key but combined with the modifiers if
any.

So, for Ctrl+A you can do the following checkings

if(e.KeyCode == Keys.A && e.Control) //you may include the other modifier
properties if you want to catch exactly Ctrl+A
{
......
}

or

if(e.KeyCode == Keys.A && e.Modifiers == Keys.Control) //Note: Keys.Control,
not Keys.ControlKey
{
}

or

if(e.KeysData == (Keys.Control | Keys.A)) //Note: Keys.Control, not
Keys.ControlKey
{
}

Ofcourse there is great possibility that something may filters the key
events and swallows your KeyDown event for Ctrl+A before it reaches the
control.
 
J

james

Stoitcho ,

Great, thanks. I thought it was possible but just couldn't quite get the
right
syntax figured out. Your explanation is SO much better than the doc's !

thanks again

JIM

Stoitcho Goutsev (100) said:
Hi james,

What kind of custom editor are you developing. Is it some kind of special
type of editor e.g. UITypeEditor.

Or it is just a normal user control.

Anyways, it depends on what property of KeyEventArgs you check.

Bare in mind that Keys.ControlKey is the key "Ctrl", but Keys.Control is the
modifier 'Control'

When you get an KeyDown event object of type KeyEventArgs is passed along
with the event

That object has one property called KeyCode and one KeyData (KeyValue is the
same as KeyData, but casted to int).
KeyCode is the code of the pressed key without any modifiers (CTRL, ALT or
SHIFT); KeyData, though, is the same key but combined with the modifiers if
any.

So, for Ctrl+A you can do the following checkings

if(e.KeyCode == Keys.A && e.Control) //you may include the other modifier
properties if you want to catch exactly Ctrl+A
{
......
}

or

if(e.KeyCode == Keys.A && e.Modifiers == Keys.Control) //Note: Keys.Control,
not Keys.ControlKey
{
}

or

if(e.KeysData == (Keys.Control | Keys.A)) //Note: Keys.Control, not
Keys.ControlKey
{
}

Ofcourse there is great possibility that something may filters the key
events and swallows your KeyDown event for Ctrl+A before it reaches the
control.

--
HTH
B\rgds
100 [C# MVP]

james said:
I am trying to trap Ctrl+A in my custom editor by handling
the KeyDown event. But what I have found is that the
key down event only contains the ControlKey on the
key down and the letter A is never passed as a KeyDown
as long as the Control key is held down. It does come through
as a KeyUp when released but that doesn't help me
because I need to handle the key immediatley when it
goes down. So is this a problem in .Net or is there some
other way to get what I want ?

thanks,

JIM
 
J

james

Ooops, I take it back. I tried the code but it still doesn't come through.
Maybe it is being stripped out. Here is what I am doing. I have created
a Custom UserControl, in the designer I dropped a RichTextBox into
my control. I then handled the KeyDown event for the RichTextBox.
In the KeyDown event I have this code


// in KeyDown
Keys ctrlA = ( Keys.Control | Keys.A );
Console.WriteLine ( "ctrlA:: " + ctrlA.ToString());
Console.WriteLine ( "KeyData:: " + e.KeyData.ToString());


and my output is this

ctrlA:: A, Control
KeyData:: ControlKey, Control

Where is the A ????

Can you test this on your end to see what is wrong ?

thanks,

JIM


james said:
Stoitcho ,

Great, thanks. I thought it was possible but just couldn't quite get the
right
syntax figured out. Your explanation is SO much better than the doc's !

thanks again

JIM

Stoitcho Goutsev (100) said:
Hi james,

What kind of custom editor are you developing. Is it some kind of special
type of editor e.g. UITypeEditor.

Or it is just a normal user control.

Anyways, it depends on what property of KeyEventArgs you check.

Bare in mind that Keys.ControlKey is the key "Ctrl", but Keys.Control is the
modifier 'Control'

When you get an KeyDown event object of type KeyEventArgs is passed along
with the event

That object has one property called KeyCode and one KeyData (KeyValue is the
same as KeyData, but casted to int).
KeyCode is the code of the pressed key without any modifiers (CTRL, ALT or
SHIFT); KeyData, though, is the same key but combined with the modifiers if
any.

So, for Ctrl+A you can do the following checkings

if(e.KeyCode == Keys.A && e.Control) //you may include the other modifier
properties if you want to catch exactly Ctrl+A
{
......
}

or

if(e.KeyCode == Keys.A && e.Modifiers == Keys.Control) //Note: Keys.Control,
not Keys.ControlKey
{
}

or

if(e.KeysData == (Keys.Control | Keys.A)) //Note: Keys.Control, not
Keys.ControlKey
{
}

Ofcourse there is great possibility that something may filters the key
events and swallows your KeyDown event for Ctrl+A before it reaches the
control.

--
HTH
B\rgds
100 [C# MVP]

james said:
I am trying to trap Ctrl+A in my custom editor by handling
the KeyDown event. But what I have found is that the
key down event only contains the ControlKey on the
key down and the letter A is never passed as a KeyDown
as long as the Control key is held down. It does come through
as a KeyUp when released but that doesn't help me
because I need to handle the key immediatley when it
goes down. So is this a problem in .Net or is there some
other way to get what I want ?

thanks,

JIM
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi james,

I tried your example and it works just fine.
Here is the thing. You receive KeyDown event for Keys.ControlKey when you
press the Ctrl key. When then press A key it fires the KeyDown event for the
A key, but because the Ctrl key is still down it is combined with
Keys.Control modifier. Thus e.KeyCode is Keys.A, e.KeyData is Keys.Control |
Keys.A and e.Control is *true*.

In order to get Ctrl+A recognized you have to press first Ctrl and then A
(this is the way the OS works) if you do it in the other way around the key
combination won't be recognized and you will get only Keys.ControlKey.

The event handler I tested is

private void richTextBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyData == (Keys.Control|Keys.A))
Console.WriteLine(e.KeyData);

e.Handled = false;
}

--
HTH
B\rgds
100 [C# MVP]

james said:
Ooops, I take it back. I tried the code but it still doesn't come through.
Maybe it is being stripped out. Here is what I am doing. I have created
a Custom UserControl, in the designer I dropped a RichTextBox into
my control. I then handled the KeyDown event for the RichTextBox.
In the KeyDown event I have this code


// in KeyDown
Keys ctrlA = ( Keys.Control | Keys.A );
Console.WriteLine ( "ctrlA:: " + ctrlA.ToString());
Console.WriteLine ( "KeyData:: " + e.KeyData.ToString());


and my output is this

ctrlA:: A, Control
KeyData:: ControlKey, Control

Where is the A ????

Can you test this on your end to see what is wrong ?

thanks,

JIM


james said:
Stoitcho ,

Great, thanks. I thought it was possible but just couldn't quite get the
right
syntax figured out. Your explanation is SO much better than the doc's !

thanks again

JIM

is
the is
the
ALT
or
SHIFT); KeyData, though, is the same key but combined with the
modifiers
if
any.

So, for Ctrl+A you can do the following checkings

if(e.KeyCode == Keys.A && e.Control) //you may include the other modifier
properties if you want to catch exactly Ctrl+A
{
......
}

or

if(e.KeyCode == Keys.A && e.Modifiers == Keys.Control) //Note: Keys.Control,
not Keys.ControlKey
{
}

or

if(e.KeysData == (Keys.Control | Keys.A)) //Note: Keys.Control, not
Keys.ControlKey
{
}

Ofcourse there is great possibility that something may filters the key
events and swallows your KeyDown event for Ctrl+A before it reaches the
control.

--
HTH
B\rgds
100 [C# MVP]

I am trying to trap Ctrl+A in my custom editor by handling
the KeyDown event. But what I have found is that the
key down event only contains the ControlKey on the
key down and the letter A is never passed as a KeyDown
as long as the Control key is held down. It does come through
as a KeyUp when released but that doesn't help me
because I need to handle the key immediatley when it
goes down. So is this a problem in .Net or is there some
other way to get what I want ?

thanks,

JIM
 
J

james

Stoitcho ,

Hmmm, I have exactly the same code as you but is never goes into the if
statement. Yes, I do press control first, and then while holding it down
I press the A key, exactly like you would in Word to select all text,
but, it NEVER comes through as ( e.KeyData == (Keys.Control|Keys.A) )
It either comes through as Control, or as A but never together. I can
hold the control key in and see the writeline go by many many times,
and then when I add the A to it, still holding the control, it just doesn't
come through until I KeyUp, which isn't what I want.
Any posible ideas why that would be? I know my Form isn't
trapping it because I have code there that never fires. I'm at a loss.

thanks for the help

JIM



Stoitcho Goutsev (100) said:
Hi james,

I tried your example and it works just fine.
Here is the thing. You receive KeyDown event for Keys.ControlKey when you
press the Ctrl key. When then press A key it fires the KeyDown event for the
A key, but because the Ctrl key is still down it is combined with
Keys.Control modifier. Thus e.KeyCode is Keys.A, e.KeyData is Keys.Control |
Keys.A and e.Control is *true*.

In order to get Ctrl+A recognized you have to press first Ctrl and then A
(this is the way the OS works) if you do it in the other way around the key
combination won't be recognized and you will get only Keys.ControlKey.

The event handler I tested is

private void richTextBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyData == (Keys.Control|Keys.A))
Console.WriteLine(e.KeyData);

e.Handled = false;
}

--
HTH
B\rgds
100 [C# MVP]

james said:
Ooops, I take it back. I tried the code but it still doesn't come through.
Maybe it is being stripped out. Here is what I am doing. I have created
a Custom UserControl, in the designer I dropped a RichTextBox into
my control. I then handled the KeyDown event for the RichTextBox.
In the KeyDown event I have this code


// in KeyDown
Keys ctrlA = ( Keys.Control | Keys.A );
Console.WriteLine ( "ctrlA:: " + ctrlA.ToString());
Console.WriteLine ( "KeyData:: " + e.KeyData.ToString());


and my output is this

ctrlA:: A, Control
KeyData:: ControlKey, Control

Where is the A ????

Can you test this on your end to see what is wrong ?

thanks,

JIM
Keys.Control
(KeyValue
is
the
same as KeyData, but casted to int).
KeyCode is the code of the pressed key without any modifiers (CTRL,
ALT
or
SHIFT); KeyData, though, is the same key but combined with the modifiers
if
any.

So, for Ctrl+A you can do the following checkings

if(e.KeyCode == Keys.A && e.Control) //you may include the other modifier
properties if you want to catch exactly Ctrl+A
{
......
}

or

if(e.KeyCode == Keys.A && e.Modifiers == Keys.Control) //Note:
Keys.Control,
not Keys.ControlKey
{
}

or

if(e.KeysData == (Keys.Control | Keys.A)) //Note: Keys.Control, not
Keys.ControlKey
{
}

Ofcourse there is great possibility that something may filters the key
events and swallows your KeyDown event for Ctrl+A before it reaches the
control.

--
HTH
B\rgds
100 [C# MVP]

I am trying to trap Ctrl+A in my custom editor by handling
the KeyDown event. But what I have found is that the
key down event only contains the ControlKey on the
key down and the letter A is never passed as a KeyDown
as long as the Control key is held down. It does come through
as a KeyUp when released but that doesn't help me
because I need to handle the key immediatley when it
goes down. So is this a problem in .Net or is there some
other way to get what I want ?

thanks,

JIM
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi james,

Can you start one test project form scratch just one form and an UserControl
(like I did) and see if its happen again. I'm still suspecting that there is
something in your code that eats up that event.

If it is possible you can send me a test project that has the problem and I
can try to find the problem for you.
My email is guzzev_at_yahoo_dot_com.

--
B\rgds
100 [C# MVP]

james said:
Stoitcho ,

Hmmm, I have exactly the same code as you but is never goes into the if
statement. Yes, I do press control first, and then while holding it down
I press the A key, exactly like you would in Word to select all text,
but, it NEVER comes through as ( e.KeyData == (Keys.Control|Keys.A) )
It either comes through as Control, or as A but never together. I can
hold the control key in and see the writeline go by many many times,
and then when I add the A to it, still holding the control, it just doesn't
come through until I KeyUp, which isn't what I want.
Any posible ideas why that would be? I know my Form isn't
trapping it because I have code there that never fires. I'm at a loss.

thanks for the help

JIM



Stoitcho Goutsev (100) said:
Hi james,

I tried your example and it works just fine.
Here is the thing. You receive KeyDown event for Keys.ControlKey when you
press the Ctrl key. When then press A key it fires the KeyDown event for the
A key, but because the Ctrl key is still down it is combined with
Keys.Control modifier. Thus e.KeyCode is Keys.A, e.KeyData is
Keys.Control
|
Keys.A and e.Control is *true*.

In order to get Ctrl+A recognized you have to press first Ctrl and then A
(this is the way the OS works) if you do it in the other way around the key
combination won't be recognized and you will get only Keys.ControlKey.

The event handler I tested is

private void richTextBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyData == (Keys.Control|Keys.A))
Console.WriteLine(e.KeyData);

e.Handled = false;
}

--
HTH
B\rgds
100 [C# MVP]

james said:
Ooops, I take it back. I tried the code but it still doesn't come through.
Maybe it is being stripped out. Here is what I am doing. I have created
a Custom UserControl, in the designer I dropped a RichTextBox into
my control. I then handled the KeyDown event for the RichTextBox.
In the KeyDown event I have this code


// in KeyDown
Keys ctrlA = ( Keys.Control | Keys.A );
Console.WriteLine ( "ctrlA:: " + ctrlA.ToString());
Console.WriteLine ( "KeyData:: " + e.KeyData.ToString());


and my output is this

ctrlA:: A, Control
KeyData:: ControlKey, Control

Where is the A ????

Can you test this on your end to see what is wrong ?

thanks,

JIM


Stoitcho ,

Great, thanks. I thought it was possible but just couldn't quite
get
the
right
syntax figured out. Your explanation is SO much better than the
doc's
!
thanks again

JIM

Hi james,

What kind of custom editor are you developing. Is it some kind of
special
type of editor e.g. UITypeEditor.

Or it is just a normal user control.

Anyways, it depends on what property of KeyEventArgs you check.

Bare in mind that Keys.ControlKey is the key "Ctrl", but
Keys.Control
is
the
modifier 'Control'

When you get an KeyDown event object of type KeyEventArgs is passed
along
with the event

That object has one property called KeyCode and one KeyData
(KeyValue
is
the
same as KeyData, but casted to int).
KeyCode is the code of the pressed key without any modifiers
(CTRL,
ALT
or
SHIFT); KeyData, though, is the same key but combined with the modifiers
if
any.

So, for Ctrl+A you can do the following checkings

if(e.KeyCode == Keys.A && e.Control) //you may include the other
modifier
properties if you want to catch exactly Ctrl+A
{
......
}

or

if(e.KeyCode == Keys.A && e.Modifiers == Keys.Control) //Note:
Keys.Control,
not Keys.ControlKey
{
}

or

if(e.KeysData == (Keys.Control | Keys.A)) //Note: Keys.Control, not
Keys.ControlKey
{
}

Ofcourse there is great possibility that something may filters the key
events and swallows your KeyDown event for Ctrl+A before it
reaches
the
control.

--
HTH
B\rgds
100 [C# MVP]

I am trying to trap Ctrl+A in my custom editor by handling
the KeyDown event. But what I have found is that the
key down event only contains the ControlKey on the
key down and the letter A is never passed as a KeyDown
as long as the Control key is held down. It does come through
as a KeyUp when released but that doesn't help me
because I need to handle the key immediatley when it
goes down. So is this a problem in .Net or is there some
other way to get what I want ?

thanks,

JIM
 

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