This tutorial will show you how to make a hotkey which works even when the form is unfocused.
We are going to use the GetAsyncKeyState API to do this.
So first we need to call it.
CODE
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
OK,
Now what our program does is check frequently (very frequently) whether the keys we want are being pressed.
To do this we will use a timer, so drag one out onto your form.
Lets just asume that it is called Timer1.
Now add this code to the Form1.load event.
CODE
Timer1.enabled = true
Timer1.interval = 1
This code is just setting the Timer1's properties "Interval" to 1 and "Enabled" to true when the form opens.
Now, count how many keys are pressed when your hotkey is pressed, for this example we will say there are three.
Ctrl - Shift - K.
So declare 3 booleans in the Timer1.Tick event, they can be called whatever you like;
E.g. Poobah or walalalah or one.
CODE
Dim crtlkey As Boolean
Dim shiftkey As Boolean
Dim k As Boolean
When that is done we need to set the value of the booleans. This is the part where the keys that need to be pressed are specified.
Remeber, this example uses Ctrl - Shift - K.
CODE
ctrlkey = GetAsyncKeyState(Keys.ControlKey)
shiftkey = GetAsyncKeyState(Keys.ShiftKey)
k = GetAsyncKeyState(Keys.K)
You can initialise them if you like, but if you don't know what initialising variables is, don't worry, this way's good.
Now we use an If statement to check if the keys are down.
CODE
If ctrlkey And shiftkey And k = True Then
'here is the code that runs when the hotkey is pressed'
end if
So heres our final code.
CODE
Public Class Form1
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.enabled = true
Timer1.interval = 1
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim crtlkey As Boolean
Dim shiftkey As Boolean
Dim k As Boolean
ctrlkey = GetAsyncKeyState(Keys.ControlKey)
shiftkey = GetAsyncKeyState(Keys.Shiftkey)
k = GetAsyncKeyState(Keys.K)
If ctrlkey And shiftkey And k = True Then
'Here is the code that runs when the hotkey is pressed'
end if
End Class
Hopefully it worked!
What this code does is everytime the timer ticks, which is every millisecond, it checks to see whether the keys you specified are down. If they are it runs the code in the If statement.
Please note that this code was just an example, your hotkey can be as long or as short as you like and the booleans can be called anything.
CODE
Dim blerty As Boolean
Dim wadas As Boolean
blerty= GetAsyncKeyState(Keys.S)
wadas = GetAsyncKeyState(Keys.L)
if blerty and wadas = true then
'Here is the code that runs again'
end if
*A Handy Few Notes
Any Button on your keyboard like Control, Shift etc. is
(Keys.ControlKey)
not
(Keys.Control)
I also spent lots of time searching for the Alt Key.
The Alt Key is
(Keys.Menu)
Enjoy