I am creating a program that I want to show when the user types a shortcut on the keyboard. The thing is, I want the user to be able to custom create the shortcut on the form.
As an example, if you right-mouse-click on a shortcut on the desktop and select properties, and select the shortcut tab, you can create a custom key stroke to run that shortcut. I want that textbox on my form.
I have everything working accept for ctrl+shift+E and a few others. For some reason just a handful won't work.
Here is the code I have:
Code:
Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.ControlKey Then
If e.Alt Then
If e.Shift Then
Me.TextBox1.Text = "Ctrl + Alt + Shift + "
Else
Me.TextBox1.Text = "Ctrl + Alt + "
End If
Else
If e.Shift Then
Me.TextBox1.Text = "Ctrl + Shift + "
Else
Me.TextBox1.Text = "Ctrl + "
End If
End If
Exit Sub
ElseIf e.KeyCode = Keys.ShiftKey Then
If e.Control Then
If e.Alt Then
Me.TextBox1.Text = "Ctrl + Alt + Shift + "
Else
Me.TextBox1.Text = "Ctrl + Shift + "
End If
Else
If e.Alt Then
Me.TextBox1.Text = "Alt + Shift + "
End If
End If
Exit Sub
ElseIf e.KeyCode = Keys.Menu Then
If e.Control Then
If e.Shift Then
Me.TextBox1.Text = "Ctrl + Alt + Shift + "
Else
Me.TextBox1.Text = "Ctrl + Alt + "
End If
Else
If e.Shift Then
Me.TextBox1.Text = "Alt + Shift + "
End If
End If
Exit Sub
Else
If e.Control Then
If e.Shift Then
If e.Alt Then
Me.TextBox1.Text = "Ctrl + Alt + Shift + " & UCase(Chr(e.KeyCode))
Else
Me.TextBox1.Text = "Ctrl + Shift + " & UCase(Chr(e.KeyCode))
End If
Else
If e.Alt Then
Me.TextBox1.Text = "Ctrl + Alt + " & UCase(Chr(e.KeyCode))
Else
Me.TextBox1.Text = "Ctrl + " & UCase(Chr(e.KeyCode))
End If
End If
Else
If e.Shift Then
If e.Alt Then
Me.TextBox1.Text = "Alt + Shift + " & UCase(Chr(e.KeyCode))
End If
Else
If e.Alt Then
Me.TextBox1.Text = "Alt + " & UCase(Chr(e.KeyCode))
End If
End If
End If
Exit Sub
End If
End Sub
My best guess would be... to create a Select Case list of all of the possible entries, compare them to what the user has entered, and then make the Short Cut from there.
Ctrl + Alt + A
Ctrl + Alt + B
Ctrl + Alt + C
Ctrl + Alt + D
...
Ctrl + Alt + Shift + A
Ctrl + Alt + Shift + B
Ctrl + Alt + Shift + C
Ctrl + Alt + Shift + D
...
Ctrl + Shift + A
Ctrl + Shift + B
Ctrl + Shift + C
Ctrl + Shift + D
...
That would be quite exhausting... there must be a better way!
tassa:
I already have a global keyboard hook built into my program, but I'm not sure how I would use it... I will try to work it out. In the meantime, do you know of an example that I can look at?
I am soooo close! Just a few shortcuts that don't don't work... I'm thinking that the main form is 'clearing' the keystrokes. Kind of like a the read only textbox doesn't register the Ctrl + E keystroke. Would there be any way to bypass that (if that could be a solution).
Ahhhhh... Success!!!! Thank you, tassa, for your key board hook idea. I checked to see if the focus was in the text box, then used my existing global hook to find the key strokes! Works perfectly now. I would never have thought to do that.
For those reading this, I have included the global keyboard hook that I used. I spent quite a bit of time searching for this online!
Ctrl + Alt + A
Ctrl + Alt + B
Ctrl + Alt + C
Ctrl + Alt + D
...
Ctrl + Alt + Shift + A
Ctrl + Alt + Shift + B
Ctrl + Alt + Shift + C
Ctrl + Alt + Shift + D
...
Ctrl + Shift + A
Ctrl + Shift + B
Ctrl + Shift + C
Ctrl + Shift + D
...
I'm glad you got it figured out. But, for future reference, no, that wasn't what I was talking about.
I suggested you make one list, A-Z, then allow the user to select which alter keys they wanted to use. Be that Ctrl, Alt, or Shift, or any other key you deemed necessary. Which is quick, easy and simple.
Ahh... I see. Yeah... I kind of wanted to save space since I will need it. That's not a bad idea. I have seen it in a few other programs, come to think of it. And it would be very easy to do.... Anyhow, thanks for the post!