Hello

My sample program has many buttons. I want my two button's pausebtn and continuebtn will perform click when a letter on the keyboard will be pressed, let say letter C or P.

Private Sub pausebtn_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles pausebtn.KeyPress
    pausebtn.PerformClick()
End Sub

This are the statements i have. I think this are lacking.

Recommended Answers

All 8 Replies

Which control has the focus when you want to read the keypress? You should be using the keypress event for that control to trigger the click event for the button.

CountdownTextbox has the focus when want to read the keypress. In this case, if I want to perform the pausebtn the timer1 will stop.

You can do this from the Form level instead of a control like below. In the Form's properties you have to set KeyPreview to True for this to work

Public Class Form1

    Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.A Then
            Button1.PerformClick()
        ElseIf e.KeyCode = Keys.B Then
            Button2.PerformClick()
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        MsgBox("Hello from Button1")
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        MsgBox("Hello from Button2")

    End Sub
End Class

@ Ancient Dragon,
do this code automatically detects the pressed key? or it need to be refresh.

Greetings!

I've tried your code but it doesn't work.

Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
        If e.KeyCode = Keys.A Then
            'ContinueToolStripMenuItem.PerformClick
            Cancel.PerformClick()
        ElseIf e.KeyCode = Keys.B Then
            StartGameToolStripMenuItem.PerformClick()
        End If
End Sub

In my code, I put ByVal before the instance variable. Do this code make it malfuction or I'm just doing it wrong?

I am also try to code like this:

Private Sub keycapture(ByVal sender As System.Object, ByVal e As _ System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
    If KeyPreview = Keys.E Then 
       PauseToolStripMenuItem.PerformClick()
    ElseIf KeyPreview = Keys.R Then 
        ContinueToolStripMenuItem.PerformClick()
    Else
        StartGameToolStripMenuItem.PerformClick()
    End If
End Sub

There's no error but it won't function.

If I'm not mistaken, the form won't fire the keydown or keypress events unless it has focus, instead of any controls or menus, etc.. Since you've mentioned that a textbox has focus, you should probably try handling the keydown or keypress event for the textbox.

commented: Exactly +6

If I'm not mistaken, the form won't fire the keydown or keypress events unless it has focus,

I tested with with vb.net 2012, the code I posted works when a textbox has focus. My guess is that first the Form event handler gets called, then the TextBox.

I've tried your code but it doesn't work.

Probably because you forgot to set KeyPeview property to TRUE as I mentioned in my previous post.

@ Ancient Dragon,
yeah, my mistake. I forgot to set the KeyPreview to TRUE. It is finally working now.
Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.