-1

I try to toggle button with a key I can't find the solution. Can Someone help me? Here is the code of button.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        toggle = 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
CroatiaGM
  • 5
  • 2

3 Answers3

3

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.

Sebastian Brosch
  • 42,106
  • 15
  • 72
  • 87
  • Do i need to add Me.KeyPreview = True to Form_Load – CroatiaGM Apr 04 '19 at 12:07
  • You can set this directly on the properties of the form. – Sebastian Brosch Apr 04 '19 at 12:10
  • It's working now thanks but when an application is in the background then it's not working how can I have it working in background. – CroatiaGM Apr 04 '19 at 12:12
  • https://stackoverflow.com/questions/5065817/listen-for-a-key-when-the-application-is-not-focused/5065906#5065906 (but be careful, other applications can use global or local hotkeys too). – Sebastian Brosch Apr 04 '19 at 12:14
  • That is c#? but I need VB – CroatiaGM Apr 04 '19 at 12:18
  • @CroatiaGM : To make it work in the background the best approach in my opinion is to use a Low-level keyboard hook (mentioned in the second bullet in Sebastian's link). My library called [InputHelper](https://github.com/Visual-Vincent/InputHelper) includes a fairly easy-to-use implementation of it (more details can be found [here](https://github.com/Visual-Vincent/InputHelper/wiki/Low-level-keyboard-hook)). – Visual Vincent Apr 04 '19 at 16:44
0

I would probably learn from the other answers - regarding KeyUp/Down events, but just to enhance your existing code, try this;

Private toggle As Boolean

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If Not toggle Then
        Timer1.Start()
        Button1.Text = "Status: ON"
    Else
        Timer1.Stop()
        Button1.Text = "Status: OFF"
    End If
    toggle = Not toggle
End Sub

What I've done is; Change the toggle integer to a boolean - since it's being used as a flag Use that flag to toggle the timer on or off for every button click Invert the flag at the end of every button click

I hope that helps - but as noted above - there are better ways to achieve this!

Grim
  • 672
  • 4
  • 17
0

Use an accelerator key, i.e. add an ampersand (&) to the Text property and you can use the subsequent character's key with the Alt key.

Also, instead of a Button, use a Checkbox with its Appearance set to Button.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46