Full Version: Global Hotkey
Dream.In.Code > Programming Tutorials > VB.NET Tutorials
CodingIlliterate
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. biggrin.gif

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! smile.gif

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 biggrin.gif
juunas
Didn't get it to work with the media buttons.
FallenDreamL7
Thank you very much for your very simple and effective tutorial.

But is there anyway reduce the sensitivity when the hotkeys are being pressed ?? Or perhaps letting the program know that I only pressed the button once, everytime I pressed the hotkey ??

Because it's sorta difficult to use the hotkeys when I wanted to open 1 particular application instead of, 6 of that same particular application and I had to close the other 5 (or more) everytime I used the hotkeys to open the application.

Thank you for your attention.
ceasley
QUOTE(FallenDreamL7 @ 20 Aug, 2009 - 07:43 AM) *

Thank you very much for your very simple and effective tutorial.

But is there anyway reduce the sensitivity when the hotkeys are being pressed ?? Or perhaps letting the program know that I only pressed the button once, everytime I pressed the hotkey ??

Because it's sorta difficult to use the hotkeys when I wanted to open 1 particular application instead of, 6 of that same particular application and I had to close the other 5 (or more) everytime I used the hotkeys to open the application.

Thank you for your attention.



I'm not sure it's the best way, but I used an "Exit Sub" after the event fired. Code is below:

CODE
Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim alt As Boolean
        Dim endkey As Boolean

        alt = GetAsyncKeyState(Keys.Menu)
        endkey = GetAsyncKeyState(Keys.End)
        If alt And endkey = True And Me.WindowState = FormWindowState.Minimized Then
            Me.WindowState = FormWindowState.Normal
            Me.ShowInTaskbar = True
            Me.Activate()
            Exit Sub
        End If

        If alt And endkey = True And Me.WindowState = FormWindowState.Normal Then
            writedatabase()
            ComboBox1.Text = ""
            TextBox1.Text = ""
            Me.WindowState = FormWindowState.Minimized
            Me.ShowInTaskbar = False
            Exit Sub
        End If


    End Sub


superbarnie
you wrote :
Dim crtlkey As Boolean

then u used:
ctrlkey = GetAsyncKeyState(Keys.ControlKey)


i think u did something wrong. i like ur tutorial but i think that u have a typo.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.