Visual Basic Help (Hotkeys)

Trill

Member
Jul 11, 2013
32
5
I am making my own personal auto typer. I have it all completed up to it spamming and stopping/starting when clicking the stop and go button. However, I am not understanding adding a hotekeys feature. I've looked at the basics and I've tried writing one out but it doesn't work what-so-ever.

Can someone help?

Thanks.
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
The first thing is that, if you wan't to work with global hotkeys (hotkeys that work outside your application window) you'd have to use the WinAPI (Windows' core API).

This can be done by doing this
Code:
Private Declare Function GetKeyPress Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Integer) As Integer

GetKeyPress is now a declared function using the user32 library and returns an integer - here you can use the Keys enum when available, since that will return a integer by name so to listen for when a key is pressed you can do something like this

Code:
If GetKeyPress(Keys.Enter)
    MsgBox("You pressed enter!");
End If

and if you wan't to have combinations of keys pressed on same time (like CTRL+ALT+ENTER) you can do this too

Code:
If GetKeyPress(Keys.Control) & GetKeyPress(Keys.Alt) & GetKeyPress(Keys.Enter)
    MsgBox("C-c-c-combo! CTRL+ALT+Enter");
End If

Good luck :)
 

Trill

Member
Jul 11, 2013
32
5
The first thing is that, if you wan't to work with global hotkeys (hotkeys that work outside your application window) you'd have to use the WinAPI (Windows' core API).

This can be done by doing this
Code:
Private Declare Function GetKeyPress Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Integer) As Integer

GetKeyPress is now a declared function using the user32 library and returns an integer - here you can use the Keys enum when available, since that will return a integer by name so to listen for when a key is pressed you can do something like this

Code:
If GetKeyPress(Keys.Enter)
    MsgBox("You pressed enter!");
End If

and if you wan't to have combinations of keys pressed on same time (like CTRL+ALT+ENTER) you can do this too

Code:
If GetKeyPress(Keys.Control) & GetKeyPress(Keys.Alt) & GetKeyPress(Keys.Enter)
    MsgBox("C-c-c-combo! CTRL+ALT+Enter");
End If

Good luck :)

Wouldn't it be user64 if I'm on a 32-Bit PC or does it matter?
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
Wouldn't it be user64 if I'm on a 32-Bit PC or does it matter?
Good question. But the answer is no. The user32 library is the name of the library, no matter what processor you have. I know it is strange, but that is one of the many strange things about Microsoft's naming conventions.
 

Trill

Member
Jul 11, 2013
32
5
Good question. But the answer is no. The user32 library is the name of the library, no matter what processor you have. I know it is strange, but that is one of the many strange things about Microsoft's naming conventions.

Right. I reviewed your code. Wouldn't that only be for using a hotkey and make it a pop up with the designated message? Or am I reading this wrong?
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
Right. I reviewed your code. Wouldn't that only be for using a hotkey and make it a pop up with the designated message? Or am I reading this wrong?
Yes, this exactly what it does.

The first line of code declares a function using the user32 lib and alias' the GetAsyncKeyState (which is the method, where the magic happens).

The other lines just checks if those key states is TRUE and if they are, they run the code. This is a very simple example, and could be improved further - but just so you got an idea of it.

Note: I am a (very) old VB/C# programmer about 4-5 years ago and have since moved on to other things.
 

Trill

Member
Jul 11, 2013
32
5
Yes, this exactly what it does.

The first line of code declares a function using the user32 lib and alias' the GetAsyncKeyState (which is the method, where the magic happens).

The other lines just checks if those key states is TRUE and if they are, they run the code. This is a very simple example, and could be improved further - but just so you got an idea of it.

Note: I am a (very) old VB/C# programmer about 4-5 years ago and have since moved on to other things.

I don't think that's what I'm wanting to accomplish. I'm wanting it to do the basics by pressing f4 to start and f5 to stop, or whichever keys I choose as the default hotkeys. That's basically where I'm stuck at.
 

Heaplink

Developer & Designer
Nov 9, 2011
510
173
I don't think that's what I'm wanting to accomplish. I'm wanting it to do the basics by pressing f4 to start and f5 to stop, or whichever keys I choose as the default hotkeys. That's basically where I'm stuck at.
You can do that too! Just switch the Keys.Enter with Keys.F1, Keys.F2 or what ever you wan't!
 

Trill

Member
Jul 11, 2013
32
5
Here's the code I'm using:

Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Enabled = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        SendKeys.Send(TextBox1.Text())
        SendKeys.Send("{Enter}")
    End Sub

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

End Class

Here's my design:
4hRRH.png


As you can see I've added the line you said above. Now, would I be adding it like this?

Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Enabled = True
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Timer1.Enabled = False
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        SendKeys.Send(TextBox1.Text())
        SendKeys.Send("{Enter}")
    End Sub

    Private Declare Function GetKeyPress Lib "user32" Alias "GetAsyncKeyState" (ByVal key As Integer) As Integer
        If GetKeyPress(Keys.F4)
        MsgBox("You pressed F4!");
End If
End Class

Or? Basically I'm wanting the top/start button to be stopped/started using F4 and F5.
 
Last edited:

Heaplink

Developer & Designer
Nov 9, 2011
510
173
The GetKeyPress should be declared at the top so all functions can see it.

Also the if condition checking for the key has to be inside a function.

Declare a function called Start and another Stop to start the and stop the timer. In your Timer1_Tick you will do the condition for what key is pressed.
 

Trill

Member
Jul 11, 2013
32
5
The GetKeyPress should be declared at the top so all functions can see it.

Also the if condition checking for the key has to be inside a function.

Declare a function called Start and another Stop to start the and stop the timer. In your Timer1_Tick you will do the condition for what key is pressed.

Do you have a quicker way to chat back and forth like Skype or something? I'm somewhat confused with this.
 

Users who are viewing this thread

Top