0

So I have a program set up so if checkbox 1 is checked, it starts me.keypreview and starts timer3. Then I have timer 3 checking if Spacebar is held down and if it is, it starts Timer1. For some reason, the code doesnt pick up that spacebar is held down and it detects left click instead, When I tried clicking left click it started timer1.

Here is my code:

  Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
    hotkey = GetAsyncKeyState(Keys.Space)
    If CBool(hotkey) = True Then
        Timer1.Start()
    Else
        Timer1.Stop()
    End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If CheckBox1.Checked = True Then
        Me.KeyPreview = True
        Timer3.Start()
    Else
        Me.KeyPreview = False
        Timer3.Stop()
    End If
End Sub

End Class

Can anybody help?

Inzample
  • 9
  • 5
  • Not related to your issue, but changing `KeyPreview` is not necessary if you check keys using `GetAsyncKeyState` as it checks the state of _the keyboard_ rather than react to Window messages. – Visual Vincent Jan 24 '17 at 00:22
  • @VisualVincent I removed the KeyPreview and it still doesn't work, any ideas on how to fix it? – Inzample Jan 24 '17 at 00:43
  • In the beginning of my comment I said `Not related to your issue`. -- I don't know what could possibly be wrong, no. I've never experienced that it would react to the wrong key. – Visual Vincent Jan 24 '17 at 07:53
  • What you could do is to try out the code in [**this answer of mine**](http://stackoverflow.com/a/38154826/3740093) (you'll have to modify the timer to check for the spacebar: `If (GetAsyncKeyState(Keys.Space) And KeyDownBit) = KeyDownBit Then`). **And be sure to use the same `GetAsyncKeyState` declaration aswell.** – Visual Vincent Jan 24 '17 at 07:59
  • @Visual Vincent I feel like the GetAsyncKeyState declaration is wrong, please tell me if this is right or wrong: Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Boolean) As Boolean Dim hotkey As Boolean – Inzample Jan 25 '17 at 05:30
  • @VisualVincent nvm, it works now, thank you so much! – Inzample Jan 25 '17 at 05:41
  • @Visual Vincent how do I make a sendkey thing that sends a spacebar down and then spacebar up keystroke? because SendKeys.Send("{SPACE}") doesn't work – Inzample Jan 25 '17 at 06:31
  • The declaration was incorrect, yes (a `Boolean` can only be True or False). You shouldn't use `Declare Function Lib` or `Declare Sub Lib` as they exist purely for backwards compatibility with VB6. Thus, most of them you found online is not fully compatible with VB.NET. Instead look for the ones using `DllImport` (like I used in the answer I linked to earlier). -- A good site to find such is [**pinvoke.net**](http://pinvoke.net/). – Visual Vincent Jan 25 '17 at 15:55
  • As for holding down/releasing space, my `SendInput` wrapper could be helpful: http://stackoverflow.com/a/39811061/3740093 – Visual Vincent Jan 25 '17 at 16:06

0 Answers0