Help with hotkeys GetAsyncKeyState

Posts 1–8 of 8 · Page 1 of 1
Help with hotkeys GetAsyncKeyState
Hey everyone I don't have much to say other then I'm making a trainer in Vb.Net everything works perfect! except I'm fed up of using buttons
to turn on and off the hacks. I'm trying to use GetAsyncKeyState to use hotkeys but I only succeed in turning on the hack.

Is it possible for somebody too show me an example of turning on and turning off.
__________________________________________________ ____________
EDITED:

Okay I will give you what I got so far.


Public Class Form1
#Region "Declarations"

Public Declare Function GetKeyPress Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Integer) As Integer
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short

#End Region

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
hotkey.Start()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles hotkey.Tick
Dim hotkey1 As Boolean

hotkey1 = GetAsyncKeyState(Keys.NumPad1)
If hotkey1 = True Then
Label1.Text = "Turned: ON"
' Enter action here
End If
End Sub

i want to toggle on and off
You are pretty close - all you need is the other half of the IF statmenet, to disable the hack. You have a couple options --> you could assign a 2nd key to be the "off" key...or you can use only 1 key and make it "toggle" the value back and forth. I went for the 2nd option.

Code:
Public Class Form1
#Region "Declarations"
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short

    Private hack1State As Boolean = False 
#End Region

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (GetAsyncKeyState(Keys.NumPad0) And 1) > 0 Then
            ''user pressed the key for 'toggle hack1' 

            hack1State = Not hack1State '' toggle the value

            If hack1State Then
                '' TODO: call Hack1Enable() here
                Me.Text = "Hack enabled"
            Else
                ''TODO: call Hack1Disable() here
                Me.Text = "Hack disabled"
            End If
        End If
    End Sub

End Class
This is with a default Timer delay of 100 ms ...if you make it go much faster, it may toggle the hack several times quickly (if the users holds the key down for a moment vs. pressing/releasing it quickly). If that's that case, you could create another variable "lastTimeToggled" and make sure it's at least been 1 second since the last toggle.
Quote Originally Posted by abuckau907 View Post
You are pretty close - all you need is the other half of the IF statmenet, to disable the hack. You have a couple options --> you could assign a 2nd key to be the "off" key...or you can use only 1 key and make it "toggle" the value back and forth. I went for the 2nd option.

Code:
Public Class Form1
#Region "Declarations"
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short

    Private hack1State As Boolean = False 
#End Region

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (GetAsyncKeyState(Keys.NumPad0) And 1) > 0 Then
            ''user pressed the key for 'toggle hack1' 

            hack1State = Not hack1State '' toggle the value

            If hack1State Then
                '' TODO: call Hack1Enable() here
                Me.Text = "Hack enabled"
            Else
                ''TODO: call Hack1Disable() here
                Me.Text = "Hack disabled"
            End If
        End If
    End Sub

End Class
This is with a default Timer delay of 100 ms ...if you make it go much faster, it may toggle the hack several times quickly (if the users holds the key down for a moment vs. pressing/releasing it quickly). If that's that case, you could create another variable "lastTimeToggled" and make sure it's at least been 1 second since the last toggle.



Hey abuckau907 I really appreciate the help and I just don't want you do lose your patience with me for this
I've been trying to get somebody to help me for quite some time now on several forums lol. Your the first to answer

I just want to start by asking I don't understand the following code in red, Am i suppose to declare that in some kind of way, I'm quite new in VB
and i'm making a trainer that will NOP the address when I activate the hack. But in our example it's Naming a Label to Label1.text = "Hack enabled". Anyway I just don't understand the error or like I said if I need to declare hack1State.

Code:
    Private Sub hotkey_Tick(sender As Object, e As EventArgs) Handles hotkey.Tick
    

        If (GetAsyncKeyState(Keys.NumPad0) And 1) > 0 Then
            ''user pressed the key for 'toggle hack1' 

            hack1State = Not hack1State '' toggle the value

            If hack1State Then
                '' TODO: call Hack1Enable() here
                lblChange.Text = "Hack enabled"
            Else
                ''TODO: call Hack1Disable() here
                lblChange.Text = "Hack disabled"
            End If
        End If

    End Sub


I also tried this but all it does is turn it on but i cant turn off.

Code:
    Private Sub hotkey_Tick(sender As Object, e As EventArgs) Handles hotkey.Tick
    
        Dim hotkey1 As Boolean

        If (GetAsyncKeyState(Keys.NumPad0) And 1) > 0 Then
            ''user pressed the key for 'toggle hack1' 

            hotkey1 = Not hotkey1 '' toggle the value

            If hotkey1 Then
                '' TODO: call Hack1Enable() here
                lblChange.Text = "Hack enabled"
            Else
                ''TODO: call Hack1Disable() here
                lblChange.Text = "Hack disabled"
            End If
        End If

    End Sub





Image of the error.








You also quoted this and I was wondering if you can show me how

If that's that case, you could create another variable "lastTimeToggled" and make sure it's at least been 1 second since the last toggle.
Your method is but slow for Key - hot...
Good.
"Anyway I just don't understand the error or like I said if I need to declare hack1State."

Exactly : )

my code from above:
Public Class Form1
#Region "Declarations"
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short

Private hack1State As Boolean = False
#End Region
Your 2nd version doesn't work because you're declaring the variable INSIDE the timer function...which means each time the function executes, the value of 'hack1state' will get re-set to false. You must declare it "local to the class" (ie. at the top, like I showed) so it will retain it's value.
-----------------------------------------------------------------------------------
You also quoted this and I was wondering if you can show me how
Sure:
Code:
Public Class Form1

#Region "Declarations"
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short

    Private hack1State As Boolean = False
    Private lastTimeToggled As DateTime = Date.Now
#End Region

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (GetAsyncKeyState(Keys.NumPad0) And 1) > 0 Then
            ''user pressed the key for 'toggle hack1' 
            If (Date.Now.Subtract(lastTimeToggled).TotalMilliseconds > 500) Then
                hack1State = Not hack1State '' toggle the value

                If hack1State Then
                    '' TODO: call Hack1Enable() here
                    Me.Text = "Hack enabled"
                Else
                    ''TODO: call Hack1Disable() here
                    Me.Text = "Hack disabled"
                End If
                lastTimeToggled = Date.Now
            End If
        End If

    End Sub
End Class
^^ (note: 1000milliseconds per 1 second) - current maximum toggle speed of 1/2 second
I wouldn't go much faster than 100ms, or slower than 1000ms -- lower than 100 could cause multiple toggles when the use was only trying to press the key once, and higher than 1000 and the user won't be happy they have to wait to toggle the hack.
Yeah, abuckau's method works and is definitely neater but eh, i do it like this.
Code:
Public Class Form1
#Region "Declarations"
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short

    Private hack1State As Boolean = False 
    Private flipswitch As Boolean = False
    Private hackswitch As Boolean = False
    Dim hawtkey As Boolean = GetAsyncKeyState(Keys.Oemtilde)
#End Region

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If (hawtkey And 1) > 0 Then
            ''user pressed the key for 'toggle hack1' 


        If hawtkey = True Then
            If flipswitch = False Then flipswitch = True
        End If
        If hawtkey = False And flipswitch = True Then
            flipswitch = False
            If Hackswitch = true then
            hackswitch = false
            'Disable hackHere
            else
            hackswitch = true
            'Enable hack Here
            End If
        End If
            
        
    End Sub

End Class
This will make it so the hot key only works when the button (Numpad 0) is released. It'll work on a timer of 1 ms with no trouble.
Quote Originally Posted by R1PT View Post
Yeah, abuckau's method works and is definitely neater but eh, i do it like this.
Code:
Public Class Form1
#Region "Declarations"
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short

    Private hack1State As Boolean = False 
    Private flipswitch As Boolean = False
    Private hackswitch As Boolean = False
    Dim hawtkey As Boolean = GetAsyncKeyState(Keys.Oemtilde)
#End Region

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        hawtkey = GetAsyncKeyState(Keys.Oemtilde) 
        If (hawtkey And 1) > 0 Then  'No matching End-If. ? 
            ''user pressed the key for 'toggle hack1' 


        If hawtkey = True Then
            If flipswitch = False Then flipswitch = True
        End If
        If hawtkey = False And flipswitch = True Then
            flipswitch = False
            If Hackswitch = true then
            hackswitch = false
            'Disable hackHere
            else
            hackswitch = true
            'Enable hack Here
            End If
        End If
            
        
    End Sub

End Class
This will make it so the hot key only works when the button (Numpad 0) is released. It'll work on a timer of 1 ms with no trouble.
-With option strict on, which it should be, there is no automatic conversion from Short to Boolean.
" Dim hawtkey As Boolean = GetAsyncKeyState(Keys.Oemtilde)" won't compile (nor the missing line I added above, in red).


"Neat" is important for someone who is new to coding... : P
It would be better to go off of key-up, I agree - your way totally eliminates the problem of spamming the key.


inspired by @R1PT
Code:
Public Class Form1

#Region "Declarations"
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Integer) As Short
    Private hack1State As Boolean = False
    Private toggleHack1 As Boolean = False
#End Region

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If GetAsyncKeyState(Keys.NumPad1) <> 0 Then
            toggleHack1 = True
        ElseIf toggleHack1 = True Then
                toggleHack1 = False
                If hack1State = False Then
                    ''EnableHack1() here
                    Me.Text = "Hack Enabled"
                    hack1State = true
                Else
                    ''DisableHack1() here
                    Me.Text = "Hack Disabled"
                    hack1State = false
                End If
        End If
    End Sub

End Class
Private Declare Function getkeys Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Integer) As Integer
^ make sure thats over everything

Then put this on a fast timer
-
If getkeys(Keys.X) Then
msgbox("ON")
Else
msgbox("OFF")
End If
-
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us