You can use the KeyDown
or KeyUp
event of the Form
:
'bind the KeyUp event
AddHandler Me.KeyUp, AddressOf SubToPressButton
'the Sub which is triggered by the KeyUp event
Sub SubToPressButton(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
'click button on key "A"
If e.KeyCode = Keys.A Then
Button1.PerformClick()
End If
End Sub
'your Button1 click event
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
toggle += 1
If toggle = 1 Then
Timer1.Start()
Button1.Text = "Status: ON"
Else
Timer1.Stop()
toggle = 0
Button1.Text = "Status: OFF"
End If
End Sub
You also have to enable the KeyPreview
of the Form
:
Me.KeyPreview = True
You can set the above line to the constructor (New
) or Load
event of the Form
. You can also enable the KeyPreview
directly on the properties of the Form
.
You need to toggle the button outside the application?
In this case you need global keyboard hooks. You can find a solution on StackOverflow already:
But be careful, other applications can use global or local hotkeys too.