Logo

INTERCETTARE LA PRESSIONE DI UN TASTO SU UN FORM (VB.net)


Per intercettare la pressione di un tasto (per esempio F1) su un form è necessario innanzitutto impostare a True la proprietà KeyPreview .
Per esempio, è possibile impostare la proprietà nell'evento Load del form

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.KeyPreview = True
End Sub                
Infine si intercetta l'evento KeyDown del form.
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    If e.KeyCode = Keys.F1 Then
        '... Tuo codice...
    End If
End Sub 


Fonte web: https://www.fdonet.com/programmazione/vbnet/intercettare-la-pressione-di-un-tasto-su-un-form-vb-net.aspx