Short cut keys dont work

gate7cy

Well-known member
Joined
May 11, 2009
Messages
119
Programming Experience
3-5
I have formA.vb and I added some shortcut keys using the key_up event handler. Before coding the handler I change the keypreview property of the form to true. I added this code :

VB.NET:
    Private Sub IntRMA_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.C Then
            modcustomer.Show()
        ElseIf e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.T Then
            Me.Button6.PerformClick()
        ElseIf e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.I Then
            Me.Button5.PerformClick()
        ElseIf e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.S Then
            Me.Button1.PerformClick()
        ElseIf e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.P Then
            Me.Button9.PerformClick()
        ElseIf e.KeyCode = Keys.ControlKey And e.KeyCode = Keys.X Then
            Me.Close()
        ElseIf e.KeyCode = Keys.Escape Then
            Me.Close()
        End If
    End Sub

..... Only the last option of the Escape works. Thanks for the time and replies. Waiting for the sun between the rain
 
Hello.

Have a look around the web to learn something about Bit-Flags.
Also, I'd suggest a different code.

VB.NET:
Select Case E.KeyCode
      Case Key.T
            If e.Ctrl Then Me.Button6.PerformClick()
      ...
End Select

Bobby
 
You can also use this code structure since these shortcuts have same modifier:
VB.NET:
If e.KeyCode = Keys.Escape Then

ElseIf e.Control Then
    Select Case e.KeyCode
        Case Keys.A

        Case Keys.B

    End Select
End If
For more advanced shortcut keys you can use the e.KeyData bitflags.

Since these all perform button clicks it may be better to use access keys (mnemonics) for keyboard access. Let's say there is a 'Info' button, you would then set the text for this button to "&Info". The access key for this would be Alt+I. When user presses the Alt key the I letter on button text is underlined, when I key is then pressed the button click event is raised automatically. Here's a related help topic How to: Create Access Keys for Windows Forms Controls
 
change my code to this and now it works fine

VB.NET:
        Select Case e.KeyData
            Case Keys.Control Or Keys.C
                modcustomer.Show()
            Case Keys.Control Or Keys.T
                Me.Button6.PerformClick()
            Case Keys.Control Or Keys.I
                Me.Button5.PerformClick()
            Case Keys.Control Or Keys.S
                Me.Button1.PerformClick()
            Case Keys.Control Or Keys.P
                Me.Button9.PerformClick()
            Case Keys.Control Or Keys.X
                Me.Close()
            Case Keys.Escape
                Me.Close()
        End Select


thanks for the replies
 
Back
Top