简体   繁体   中英

keypress event instead click in vb.net

I'm trying to implement some events using keys instead of clicks, I know that this code:

Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
    My.Computer.Audio.Play("D:\VisualStudio\JuegoDIF\sonidos\sonidoPerro.wav") 
End Sub

is playing the sound when i click the picturebox2, but how can you do this using a key? example: presing the key "f5" should play the same sound as the click event

so far i tried keyDown like this:

   Private Sub PictureBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles PictureBox2.KeyDown
    If e.KeyCode.Equals(Keys.F5) Then
        My.Computer.Audio.Play("D:\VisualStudio\JuegoDIF\sonidos\sonidoPerro.wav") ' 
    End If
End Sub

But it's not working, and the keypress im not sure how to implement it:

Private Sub PictureBox2_KeyPress(sender As Object, e As KeyPressEventArgs) Handles 
PictureBox2.KeyPress

End Sub

How can I do this implementation?

在此处输入图像描述

Set the form properties to Key Preview to True

Under the Form KeyDown Event

 Private Sub frm_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) 
Handles MyBase.KeyDown
    

    If e.KeyCode = Keys.F1 Then
        'Enter your Code here
    End If


    If e.KeyCode = Keys.F2 Then
         'Enter Code here
    End If
End Sub

This method should be the easiest one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM