[RESOLVED] Capture key press for hotkey
Hello, I'm making an application that will execute some functions on user defined key presses.
The user needs to set the keys they want to fire the function in a textbox. I've set the textbox to Read-Only and am trying to make the code that will specify the key combinations used that are displayed in the textbox. So for example; if a user presses Ctrl + Alt + Shift + E, the textbox will show this.
I'm using the KeyDown and KeyUp events that come with a textbox. The textbox will loose focus once a control key (Ctrl or Alt) AND an alphanumeric key (such as E) is pressed. Shift is optional.
I need to store this data, so that it can later be fed into the GetAsyncKeyState function that will listen in a timer.
I'm having some troubles getting the KeyDown and KeyUp detect whether is was the left or right Ctrl, Alt and Shift keys pressed as well.
Here's my code currently:
Code:
Private ctrlKeyDw As Boolean = False
Private altKeyDw As Boolean = False
Private shiftKeyDw As Boolean = False
Private Sub txtCaptureKey_KeyDown(sender As Object, e As KeyEventArgs) Handles txtCaptureKey.KeyDown
If e.KeyCode = Keys.ControlKey Then
ctrlKeyDw = True
End If
If e.KeyCode = Keys.Alt Then
altKeyDw = True
End If
If e.KeyCode = Keys.Shift Then
shiftKeyDw = True
End If
updateText()
End Sub
Private Sub updateText()
txtCaptureKey.Text = ""
If ctrlKeyDw Then
txtCaptureKey.Text = "Ctrl + " & txtCaptureKey.Text
End If
If altKeyDw Then
txtCaptureKey.Text = "Alt + " & txtCaptureKey.Text
End If
If shiftKeyDw Then
txtCaptureKey.Text = "Shift + " & txtCaptureKey.Text
End If
If txtCaptureKey.Text = "" Then
txtCaptureKey.Text = "None"
End If
End Sub
Private Sub txtCaptureKey_KeyUp(sender As Object, e As KeyEventArgs) Handles txtCaptureKey.KeyUp
If e.KeyCode = Keys.ControlKey Then
ctrlKeyDw = False
End If
If e.KeyCode = Keys.Alt Then
altKeyDw = False
End If
If e.KeyCode = Keys.Shift Then
shiftKeyDw = False
End If
updateText()
End Sub
At the moment it's only detecting when Ctrl is pressed. I can't figure out why Shift and Alt are not being detected.
Re: Capture key press for hotkey
Don't the KeyCode property to detect modifier keys in combination with other keys.
vb.net Code:
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
Dim depressedKeys As New List(Of String)
If e.Control Then
depressedKeys.Add("Ctrl")
End If
If e.Shift Then
depressedKeys.Add("Shift")
End If
If e.Alt Then
depressedKeys.Add("Alt")
End If
If e.KeyCode >= Keys.A AndAlso e.KeyCode <= Keys.Z Then
depressedKeys.Add(e.KeyCode.ToString())
End If
Me.TextBox1.Text = String.Join("+", depressedKeys)
End Sub
Re: Capture key press for hotkey
Thanks heaps for that jmcilhinney. Is there any reason that Lcontrol and RControl doesn't work when using KeyCode? Otherwise your code is working perfectly.
Re: Capture key press for hotkey
Quote:
Originally Posted by
Slyke
Thanks heaps for that jmcilhinney. Is there any reason that Lcontrol and RControl doesn't work when using KeyCode? Otherwise your code is working perfectly.
As far as I can tell from research on the web and my own testing, you can't detect the difference between the left and right Ctrl keys via Windows Forms keyboard events. I haven't checked but I'm guessing that the same is true of Shift and Alt keys. You would have to first detect a Ctrl key in the conventional way and then use the GetAsyncKeyState API to determine the state of the left and right keys.
Re: Capture key press for hotkey
That's a great idea. Will do just that.