'Set "KEY TO BE PRESSED" to the key you want to be pressed as the hotkey.
Dim HOTKEY1 As Boolean = GetAsyncKeyState(Keys.KEY TO BE PRESSED)
If HOTKEY1 = True Then
'Insert what you want your application to do when they hotkey is pressed.
End If
Dim HOTKEY2 As Boolean = GetAsyncKeyState(Keys.KEY TO BE PRESSED)
If HOTKEY2 = True Then
'Insert what you want your application to do when they hotkey is pressed.
End If
In all instances where it says "Keys.KEY TO BE PRESSED" change KEY TO BE PRESSED to a key.
Ex:
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> Public Shared Function GetAsyncKeyState(ByVal vkey As Integer) As Short
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
KeyPreview = True
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Escape Then
Me.Close()
End If
End Sub
NOTE: Make sure you apply the hotkey to the button, or menu as well, while applying this code, otherwise it'll only work as a hotkey.
<DllImport("user32.dll")> Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As UInteger, ByVal vk As UInteger) As Boolean
End Function
<DllImport("user32.dll")> Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
End Function
Private Sub btnSet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSet.Click
Dim modifiers As UInteger = 0
'modifiers += 1 = alt
'+= 2 = ctrl
'+= 4 = shift
'+= 8 = win
'can be added 2x maximum; e.g. 3 for ctrl + alt
RegisterHotKey(Me.Handle, 0, modifiers, Keys.A)
End Sub
Const WM_HOTKEY As Integer = &H312
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_HOTKEY Then
MsgBox("hotkey with ID " & m.WParam.ToInt32() & " pressed")
End If
MyBase.WndProc(m)
End Sub
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
UnregisterHotKey(Me.Handle, 0)
End Sub