Results 1 to 9 of 9

Thread: [RESOLVED] Shortcut keys do not work

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    168

    Resolved [RESOLVED] Shortcut keys do not work

    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 :

    Code:
    
        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

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Shortcut keys do not work

    Try this :

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

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Shortcut keys do not work

    That's because you're testing whether e.KeyCode is two different values at the same time, which it can't possibly be. If you don't care about modifier keys then just test e.KeyCode for a single key. If you do care about modifier keys then test e.KeyData for a key combination. Your code should be:
    vb.net Code:
    1. Private Sub IntRMA_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    2.     Select Case e.KeyData
    3.         Case Keys.Control Or Keys.C
    4.             modcustomer.Show()
    5.         Case Keys.Control Or Keys.T
    6.             Me.Button6.PerformClick()
    7.         Case Keys.Control Or Keys.I
    8.             Me.Button5.PerformClick()
    9.         Case Keys.Control Or Keys.S
    10.             Me.Button1.PerformClick()
    11.         Case Keys.Control Or Keys.P
    12.             Me.Button9.PerformClick()
    13.         Case Keys.Control Or Keys.X
    14.             Me.Close()
    15.         Case Keys.Escape
    16.             Me.Close()
    17.     End Select
    18. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    168

    Re: Shortcut keys do not work

    thanks jmcilhinney. that did the work. great

  5. #5
    Frenzied Member
    Join Date
    Jul 2009
    Posts
    1,103

    Re: [RESOLVED] Shortcut keys do not work

    I am a beginner of .net framework and the visual basic language
    I added the following code:

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.Control = True And e.KeyCode = Keys.P Then
    Me.Button1.PerformClick()
    End If
    End Sub
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    MessageBox.Show("This is a message box")
    End Sub

    but this is not working for me.....
    can you tell me why its not working?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    168

    Re: [RESOLVED] Shortcut keys do not work

    What exactly are trying to do? Get the button1 to perform click? What keys you want to use to enable this shortcut? the control button and P? If, yes the try this:

    Code:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyData = Keys.Control And e.KeyData = Keys.P Then
    Me.Button1.PerformClick()
    End If
    End Sub

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Shortcut keys do not work

    Quote Originally Posted by gate7cy View Post
    What exactly are trying to do? Get the button1 to perform click? What keys you want to use to enable this shortcut? the control button and P? If, yes the try this:

    Code:
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyData = Keys.Control And e.KeyData = Keys.P Then
    Me.Button1.PerformClick()
    End If
    End Sub
    How can e.KeyData be equal to Keys.Control and equal to Keys.P at the same time? As has been established by post #3, you have to compare e.KeyData to (Keys.Control Or Keys.P), but this has already been explained to gautamshaw in their own thread on this topic, where they asked this exact same question.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    May 2009
    Posts
    168

    Re: [RESOLVED] Shortcut keys do not work

    my bad

    drop the 'and' and replace it with an 'or'.
    how on earth would I know of the anwer he got on his own thread.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Shortcut keys do not work

    Quote Originally Posted by gate7cy View Post
    drop the 'and' and replace it with an 'or'.
    That still won't work because you're then testing for one value or the other, not both at the same time. You have to compare e.KeyData to a single value and that value has to be a combination of Keys.Control and Keys.P, which you create with a bitwise OR operation.
    Quote Originally Posted by gate7cy View Post
    how on earth would I know of the anwer he got on his own thread.
    You wouldn't, which is exactly why people shouldn't be posting the same question in mutliple threads. That was directed at gautamshaw, not you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width