How to handle the CTRL+A keys?

G

Guest

I’m using the DataGrid control. The DataGrid allow select all by clicking on
CTRL+A keys.
I need to catch this keys event, so I tried using the KeyDown event, but
only when the CTRL is pressed I get the event, but if the A key is pressed
while the CTRL is pressed – The event is not fired to my KeyDown handler.

How do I get the event for the CTRL + A keys?
 
K

Kevin Yu [MSFT]

Hi Sharon,

You cannot catch the ctrl+A event in the DataGrid level because it was
fired inside the TextBox of the cell. So in this case, you have to handle
the TextBox.KeyDown events to catch this or, you can use sub-class the
DataGrid to achieve this. Here is a thread similar to your requirements. I
think it will be helpful to you.

http://groups.google.com/group/microsoft.public.dotnet.framework.windowsform
s.controls/browse_frm/thread/9f53c61f3e729ceb/bcdc1ec769d30da1?lnk=st&q=%22J
effrey+Tan%22+datagrid+processcmdkey&rnum=1&hl=zh-CN#bcdc1ec769d30da1

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
J

Justin Creasy

Hey Sharon, if you're using the KeyDown event try this.

private void yourKeyDownEvent(object sender, KeyEventArgs e)
{
if(e.Control && e.Equals(Keys.A))
{
// your code here
}
}
 
G

Guest

Thanks for try Justin,
But when the CTRL key is pressed down --> the event is fired, but the A key
event is not firing the event because the CTRL key is still pressed.

I event changed the code to:
private void OnDataGridKeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if( e.Equals(Keys.A) )
{
if( e.Control ) // It never gets in here !!!
{
// Some code...
}
}
}

I hope there is a solution for that.
 
J

Justin Creasy

Hey Sharon, sorry about that, I thought I saw it work but I'm doing it
now and it's not working.

I did some code awhile ago to do something like this but I can't seem
to find it. What you have to do is override some system commands
because you won't be able to use KeyDown or KeyPress. Once the control
key is it, pressing another key will not fire the keyDown event. I'll
look around some more, but for now I would focus on looking for the
lower level functions that get called when system keys (control, alt,
shift, etc.) are pressed. I'll post again if I find what I'm talking
about. good luck
 
R

rossum

Thanks for try Justin,
But when the CTRL key is pressed down --> the event is fired, but the A key
event is not firing the event because the CTRL key is still pressed.

I event changed the code to:
private void OnDataGridKeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if( e.Equals(Keys.A) )
{
if( e.Control ) // It never gets in here !!!
{
// Some code...
}
}
}

I hope there is a solution for that.

Try overriding the ProcessDialogKey event:

bool ProcessDialogKey(Keys keyData)

For Ctrl-A use
if (keyData == Keys.Control | Keys.A) ...
or else
switch (keyData) { case Keys.Control | Keys.A: ...

Return true if you have processed the key, otherwise return
base.ProcessDialogKey(keyData) to give the rest of the system access
to the keystrokes you are not handling.

rossum
 

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