-
Easy one for somebody (Control Key + "C" key)
Here's an easy one for somebody, except for me.
How do you trap a control+C keydown? I've tried the following.
1. If e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.C Then
2. If e.KeyCode = Keys.ControlKey AndAlso e.KeyCode = Keys.C Then
3. If e.KeyCode = Keys.ControlKey Then
If e.KeyCode = Keys.C Then
<My Code Here>
End if
End if
Thanks for any help you can provide.
-
After lots of searching, here's the answer to my own question.
If e.Control And e.KeyCode = Keys.C Then
Thanks to all that read my question.
-
You really should leave things like CTRL+C, CTRL+V, CTRL+X alone as they are using by the system for cut copy paste. If your app does something else with them it could result in confusion for your users. However, if you simply MUST use the letter C, then I would suggest throwing a Shift in there so that CTRL+C is left alone.
Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If ((e.KeyCode = Keys.C) AndAlso (e.Modifiers = (Keys.Control Or Keys.Shift))) Then
'do something if CTRL+Shift+C has been hit
End If
End Sub
For CTRL and other keys, here is another way of doing it
Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If ((e.KeyCode = Keys.T) AndAlso (e.Modifiers = (Keys.Control))) Then
'do something if CTRL+T has been hit
End If
End Sub
-
Thanks Hack. You've have always helped me with my problems and I value your advice. Thanks.
-
Hack is pushing a VERY IMPORTANT point when stating that you should leave standard key combinations alone.
However, I would go a step further. Completely leave out CTRL in combination with A to Z. This might stand from my situation here in the Montreal area, were we have a lot of mix-up between English and French. But my understanding is that you are starting to get the same problems on your side of the border (if your names are representative of the side I am thinking of) between English and Spanish.
Here in Quebec, some keyboard standards, such as those mentioned by Hack still old in French applications. But shortcuts such as CTRL-B for Bold becomes CTRL-G for Gras. Heck, there are even standard differences between applications written in France and Quebec. You might well have the same between English applications written on different sides of the Atlantic.
Personally, I would rather use the F1-F12 keys, with all the possible combinations of CTRL, ALT and SHIFT than letters of the alphabet. Yes, they are not as easy to remember, but there are fewer standards on those, and they tend to be less strict. As an example, F5 is usually used to "Refresh" in most Microsoft applications, but it is used to start Debug in Visual Studio.
Those keys will also correct a problem that most of you American programmers (I suppose that most of those who read these threads are American) do not think about. Because Microsoft design everything with English defaults, there are a few details that often skip unnoticed in front of you. The one described in this thread is one of those.
e.KeyCode = Keys.C does not detect that the C key was depressed. It tells you that the keyboard key with the code 67 was called. Just put your cursor over the Keys.C in the editor, and you will get the value.
When the user press the C key on an English keyboard, it does not send a C to the computer, it sends 67. Before displaying the character on the screen, the computer sends the 67 to the Control Panel to know what that values means for the language that is set in the Regional settings.
As far as I am concerned, here in Quebec, 67 is C as well as it is for you.
I am actually typing this on an English keyboard, and running all my software, including Windows, in English. But my Control Panel is set for French Canadian. If I click on the / key, my application receives 191, which translates to é on the screen.
If I press the a key (Keys.A or 81), no difference between me and most of you guys, we all get a. But somebody in France will probably receive Q, because Q and A are inverted between the keyboards used in France and those used in Quebec.
Even if I stick with Quebec, people use 3 different keyboard configuration. The old one that was used on the old typewriters, a newer one that was designed for computers, and a third one that is easier to use for someone who writes both in French and English. Some KeyCodes will trigger different characters depending on which of the 3 keyboards was chosen in the configuration panel.
OK, you all have English keyboard, but you have a lot of users that come from Mexico, India and who knows. Their Control Panel might not be set to English. Keys.C might fail for those
If you want to really detect a character, you do not use the KeyCode, you use the KeyChar in the KeyPress event. Unfortunately, although they work hard to try to "internationalize" their applications, Microsoft very often miss a few points. You cannot detect the CTRL key in the KeyPress. So if I wanted to detect CTRL-C, I would have to resort to other ways. One being to record the character received by KeyPress in a variable and use that variable against Ctrl in the KeyUp.
But the easiest way to design my own shortcut keys is to trap the functions keys. F1 to F12 are not defined by the Control Panel and can be trapped alongside Ctrl-Alt-Shift in the KeyDown or KeyUp events.
Who wants to conquer the world should know the world . And all the world is using computers now.
Jacques Bourgeois
JBFI
http://www3.sympatico.ca/jbfi/homeus.htm
-
Thanks JBourgeois for your reply. I have often wondered what my key strokes would be somewhere else. I find your post very interesting. Thanks again for replying.
Similar Threads
-
By Actipro Software Support in forum dotnet.announcements
Replies: 0
Last Post: 02-19-2002, 09:13 PM
-
By Actipro Software Support in forum dotnet.announcements
Replies: 0
Last Post: 02-11-2002, 02:33 PM
-
By Birhanu in forum VB Classic
Replies: 0
Last Post: 11-09-2001, 04:26 PM
-
By Birhanu in forum VB Classic
Replies: 0
Last Post: 11-09-2001, 04:26 PM
-
By Glenn in forum ASP.NET
Replies: 0
Last Post: 12-13-2000, 08:55 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|