ctrl-c keyup not triggered when ctrl is released first

R

reasonman

I have a DataGridView that catches the KeyUp event. I use it to
capture a keyboard copy(ctrl-c) and then perform my own formatting on
the copied text. Everything works as it should when I release the 'C'
key first, but if I release 'ctrl' first while still holding 'C', it
doesn't trigger the event. Below is the code for the event. I'm
already handling it on a copy context menu item but I know i'll have
users using the keyboard shortcuts and I'd prefer to have everything
work as expected. Any ideas are appreciated.

private void dgOrders_KeyUp(object sender, KeyEventArgs e)
{
//trap a ctrl+c(copy) and call the copy method
if (e.KeyData == (Keys.C | Keys.Control))
CopyData();
}
 
R

reasonman

I have a DataGridView that catches the KeyUp event. I use it to
capture a keyboard copy(ctrl-c) and then perform my own formatting on
the copied text. Everything works as it should when I release the 'C'
key first, but if I release 'ctrl' first while still holding 'C', it
doesn't trigger the event. Below is the code for the event. I'm
already handling it on a copy context menu item but I know i'll have
users using the keyboard shortcuts and I'd prefer to have everything
work as expected. Any ideas are appreciated.

private void dgOrders_KeyUp(object sender, KeyEventArgs e)
{
//trap a ctrl+c(copy) and call the copy method
if (e.KeyData == (Keys.C | Keys.Control))
CopyData();
}

Actually, i'm noticing that it still copies my text, but it totally
bypasses my CopyData method and the event and it looks like it does
the default DataGridView.GetClipboardContent(). I guess on a ctrl-c
it falls back to the default if the event isn't triggered.
 
R

reasonman

Actually, i'm noticing that it still copies my text, but it totally
bypasses my CopyData method and the event and it looks like it does
the default DataGridView.GetClipboardContent(). I guess on a ctrl-c
it falls back to the default if the event isn't triggered.
Sorry, it IS triggering the event, just not matching. If I release
ctrl first the KeyData is "LButton | ShiftKey".
 
J

Jeff Johnson

I have a DataGridView that catches the KeyUp event. I use it to
capture a keyboard copy(ctrl-c) and then perform my own formatting on
the copied text. Everything works as it should when I release the 'C'
key first, but if I release 'ctrl' first while still holding 'C', it
doesn't trigger the event. Below is the code for the event. I'm
already handling it on a copy context menu item but I know i'll have
users using the keyboard shortcuts and I'd prefer to have everything
work as expected. Any ideas are appreciated.

A few ideas:

1) I used Spy++ to watch the messages for the text box inside an inside of
Notepad. When I hit Ctrl+C, the text box got a WM_COPY on key down, so for
what it's worth, Windows uses key down instead of key up for copy/paste
operations. I think in general it's better to use the down event for keys
and the up event for the mouse to decide when to execute an action.

2) Please please please be considerate of those of us who still use the CUA
key sequences for copy/paste and support Shift+Del, Ctrl+Ins, and Shift+Ins
(that's cut/copy/paste). If I hit Ctrl+Ins in your grid and nothing happens
I, as a user, will say bad things about you and your ancestors.

3) I haven't really tried, but I'm willing to bet that in those programs
that DO use the key up event to handle a shortcut that you're required to
release the non-modifier key first. I'd be surprised if I could press, say,
Crtl+Shift+A and then release Shift or Control first and have that program
react properly. You certainly can't press C before Control and expect any
Windows program to perform a copy.
 
R

reasonman

A few ideas:

1) I used Spy++ to watch the messages for the text box inside an inside of
Notepad. When I hit Ctrl+C, the text box got a WM_COPY on key down, so for
what it's worth, Windows uses key down instead of key up for copy/paste
operations. I think in general it's better to use the down event for keys
and the up event for the mouse to decide when to execute an action.

2) Please please please be considerate of those of us who still use the CUA
key sequences for copy/paste and support Shift+Del, Ctrl+Ins, and Shift+Ins
(that's cut/copy/paste). If I hit Ctrl+Ins in your grid and nothing happens
I, as a user, will say bad things about you and your ancestors.

3) I haven't really tried, but I'm willing to bet that in those programs
that DO use the key up event to handle a shortcut that you're required to
release the non-modifier key first. I'd be surprised if I could press, say,
Crtl+Shift+A and then release Shift or Control first and have that program
react properly. You certainly can't press C before Control and expect any
Windows program to perform a copy.
Keydown doesn't match in either case, and if the expected behavior is
to ignore the copy when ctrl is released first then i'm not going to
waste any time on it. It never really registered that when I release
ctrl in other apps it doesn't do anything either.

I had never heard of those other shortcuts and didn't know that they
were active in other programs(tested in VS2010). This is an internal
application that's geared towards some supervisors and these are the
type of people who search yahoo for google.com and search from there,
I doubt they'll know these other shortcuts. But it's a trivail matter
to drop them in there so what the hell.
 
J

Jeff Johnson

Keydown doesn't match in either case

Can you clarify the above statement? Could you post the code where you're
trying to use the KeyDown event? I've handled keystrokes in code before
without problems.
 
R

reasonman

Can you clarify the above statement? Could you post the code where you're
trying to use the KeyDown event? I've handled keystrokes in code before
without problems.
I guess I had been at it too long yesterday. Honestly i'm not sure
what I was doing, one of those days I guess. After doing it again
with the same code but in the keydown event, it is matching the
condition and going to my CopyData method, but it's still the default
format for the html copy.

private void dgOrders_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == (Keys.C | Keys.Control))
CopyData();
}

CopyData executes and at the end before it gets pushed to the
clipboard the html i've generated is correct.
 
R

reasonman

[...] but it's still the default format for the html copy.

I don't understand what that means. Is there still a problem?
When I copy a cell or a group of cells from the DataGridView, I can
specify the format to put the data into the clipboard, one of them
being html. It generates a table with the contents of the cells but
the table is very basic, no borders, no header row shading, etc. To
correct this I rolled my own copy method(CopyData) and I format it how
I like, then push it to the clipboard.

On a keyup event CopyData is executed and the table that is put into
the clipboard is the custom table I generated myself. If I use
keydown, CopyData is executed and my custom table is put in the
clipboard, but it seems that immediately after that, the default table
that DataGridView generates makes its way into the clipboard,
overwritting what i've put in myself.
 
R

reasonman

[...]
On a keyup event CopyData is executed and the table that is put into
the clipboard is the custom table I generated myself. If I use
keydown, CopyData is executed and my custom table is put in the
clipboard, but it seems that immediately after that, the default table
that DataGridView generates makes its way into the clipboard,
overwritting what i've put in myself.

Without a concise-but-complete code example, it's impossible to answer
your question with 100% certainty. But it sounds like you are not doing
anything to interrupt the normal processing of the key-down event by the
DataGridView control itself.

Probably you want to set the SuppressKeyPress property of the
KeyEventArgs instance in your event handler, if and when your own code
handles the key event.

Pete
supSuppressKeyPress did it for me. I guess after the keydown my copy
method was being executed correctly, then the default copy for the
datagridview, whatever it is, was being triggered on the keyup which
i'm no longer handling. Is that what's happening or am I off base a
bit? If it matters much , relevant code below.

private void dgOrders_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == (Keys.C | Keys.Control))
{
CopyData();
e.SuppressKeyPress = true;
}
}
private void CopyData()
{
if (!_CurrentCell.IsInEditMode)
{
DataObject OrdersClipboard =
dgOrders.GetClipboardContent();

if (OrdersClipboard != null)
{
Stream MemStream =
(Stream)OrdersClipboard.GetData(DataFormats.Html);
MemStream.Seek(0, SeekOrigin.Begin);

byte[] Bytes = new byte[(int)MemStream.Length
- 1];
MemStream.Read(Bytes, 0, (int)MemStream.Length
- 1);
MemStream.Close();

string HtmlFromClipboard =
System.Text.Encoding.ASCII.GetString(Bytes);
//here my data comes out formatted the way it
should be
HtmlFromClipboard =
Helper.FormatHtml(HtmlFromClipboard);

DataObject HtmlDataObj =
Helper.FormatHtmlForClipboard(HtmlFromClipboard);
//and here it gets inserted into the clipboard
with no problem
Clipboard.SetDataObject(HtmlDataObj);
}
}
else
Clipboard.SetDataObject(dgOrders.EditingControl.Text,
true);
}
 
J

Jeff Johnson

supSuppressKeyPress did it for me. I guess after the keydown my copy
method was being executed correctly, then the default copy for the
datagridview, whatever it is, was being triggered on the keyup which
i'm no longer handling.

More than likely the default handling was happening in KeyDown, but only
after your code ran.

SuppressKeyPress is a new one on me. I normally just set the Handled
property, but the docs suggest that this is a better (as in "more thorough")
way to go, so I will begin using it.
 
R

reasonman

More than likely the default handling was happening in KeyDown, but only
after your code ran.

SuppressKeyPress is a new one on me. I normally just set the Handled
property, but the docs suggest that this is a better (as in "more thorough")
way to go, so I will begin using it.
Well as long as it's working as it should. Thanks for the help guys.
 
R

reasonman

To clarify: I agree that the copy is being handled in KeyDown, _not_
KeyUp. But your event handler gets called before the control itself
does any processing.
Ah, I see. Thanks for the clarification.

What was really throwing me off was when I tried to debug and set a
breakpoint at CopyData then step through, it would work perfectly. I'm
not entirely sure what caused it but it's happened before where I had
code that half worked when it ran by itself, but then debugging and
stepping through the offending parts would always give me the results
I was looking for.
 
J

Jeff Johnson

Ah, I see. Thanks for the clarification.

What was really throwing me off was when I tried to debug and set a
breakpoint at CopyData then step through, it would work perfectly. I'm
not entirely sure what caused it but it's happened before where I had
code that half worked when it ran by itself, but then debugging and
stepping through the offending parts would always give me the results
I was looking for.

Don't worry, you're not alone in experiencing different behavior in debug
vs. run....
 

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