Capturing ctrl keys in form KeyDown

J

John

Hi

I am trying to capture Ctrl-C/X/V in a form's KeyDown event and am using the
below code but it is not working as pressing Ctrl-C for example returns a
e.KeyCode = CtrlKey and not Keys.C, Keys.X, Keys.V etc.

Select Case e.KeyCode
Case Keys.C
If e.Modifiers = Keys.Control Then

End If
Case Keys.X
If e.Modifiers = Keys.Control Then

End If
Case Keys.V
If e.Modifiers = Keys.Control Then

End If
End Select

How can I achieve this please?

Many Thanks

Regards
 
A

Armin Zingler

John said:
Hi

I am trying to capture Ctrl-C/X/V in a form's KeyDown event and am using the
below code but it is not working as pressing Ctrl-C for example returns a
e.KeyCode = CtrlKey and not Keys.C, Keys.X, Keys.V etc.

Select Case e.KeyCode
Case Keys.C
If e.Modifiers = Keys.Control Then

End If
Case Keys.X
If e.Modifiers = Keys.Control Then

End If
Case Keys.V
If e.Modifiers = Keys.Control Then

End If
End Select

How can I achieve this please?

Code works correctly. First I get Keys.CtrlKey, then Keys.C. That's what
I expect pressing these keys in that order.
 
G

Gregory A. Beamer

John said:
Hi

I am trying to capture Ctrl-C/X/V in a form's KeyDown event and am using
the below code but it is not working as pressing Ctrl-C for example
returns a e.KeyCode = CtrlKey and not Keys.C, Keys.X, Keys.V etc.

Private Sub Form1_KeyDown(ByVal sender as System.Object, ByVal e as
System.Windows.Forms.KeyEventArgs) Handles Form1.KeyDown

If e.Control Then
Select Case e.KeyCode
Case Keys.C
Case Keys.V
Case Keys.X
End Select
End If

End Sub

I saw where you had asked if you can capture both at the same time, not
Control first? The answer is no, but it does not matter, as pushing the keys
in the opposite direction does not work in any application. You have to
click the control key prior to the alpha key.

--
Peace and Grace,
Greg

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

************************************************
| Think outside the box! |
************************************************
 

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