Use the KeyboardHook in the MPGH SDK, then build a class around it to manage your hotkeys.
EDIT: Okay I wrote it for you
[highlight=vb.net]
Imports MPGH
Public Class HotkeyManager : Implements IDisposable
Public Delegate Sub HotkeyAction(ByVal params As Object())
Private Basehook As SDK.KeyboardHook
Private Hotkeys As Dictionary(Of Integer, Hotkey)
Private _isactive As Boolean
Public ReadOnly Property IsActive As Boolean
Get
Return Me._isactive
End Get
End Property
Public Sub New()
Me.Hotkeys = New Dictionary(Of Integer, Hotkey)()
Me.Basehook = New SDK.KeyboardHook(New SDK.KeyboardHook.OnKeyPressed(AddressOf OnKeyPressed))
Me._isactive = Me.Basehook.IsHooked
End Sub
Private Sub OnKeyPressed(ByVal nKey As Integer)
For Each k As Integer In Me.Hotkeys.Keys
If nKey = k Then
Dim curHotkey As Hotkey = Me.Hotkeys(k)
curHotkey.Callback.Invoke(curHotkey.FunctionParams )
Exit For
End If
Next
End Sub
Public Function RegisterHotkey(ByVal key As Integer, ByVal action As HotkeyAction, Optional ByVal params As Object() = Nothing) As Boolean
If Not Me.Hotkeys.ContainsKey(key) AndAlso Not IsNothing(action) Then
Me.Hotkeys.Add(key, New Hotkey(key, action, params))
Return True
Else
Return False
End If
End Function
Public Function UnregisterHotkey(ByVal key As Integer) As Boolean
If Me.Hotkeys.ContainsKey(key) Then
Me.Hotkeys.Remove(key)
Return True
Else
Return False
End If
End Function
Public Structure Hotkey
Public ReadOnly Key As Integer
Public ReadOnly Callback As HotkeyAction
Public ReadOnly FunctionParams As Object()
Public Sub New(ByVal key As Integer, ByVal callback As HotkeyAction, ByVal params As Object())
Me.Key = key
Me.Callback = callback
Me.FunctionParams = params
End Sub
End Structure
#Region "IDisposable Support"
Private disposedValue As Boolean ' To detect redundant calls
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
Me.Basehook.Dispose()
Me._isactive = Me.Basehook.IsHooked
Me.Hotkeys = Nothing
Me.Basehook = Nothing
End If
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
#End Region
End Class
[/highlight]
Now, this is really easy to use, add the class to your project then create a new global variable to hold a HotkeyManager instance, after that you simply have to write out some method handlers (as you would for buttons, etc) that match the delegate signature
which is just a sub, which accepts an object() as a parameter.
So, let's have a look at an example:
[highlight=vb.net]
Public Class Form1
Private hKeyMgr As New HotkeyManager() 'initialize a hotkeymanager
'when I click the button, it will register the hotkeys "D" and "A". Whenever D is pressed, the "OnDPressed" event will fire. Whenever "A" is pressed, the OnAPressed event will fire.
Private Sub btnRegisterHotkey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
hKeyMgr.RegisterHotkey(Keys.D, AddressOf OnDPressed, New Object() {"hello", "world", "cool"}) '3 params to be passed to the function.
hKeyMgr.RegisterHotkey(Keys.A, AddressOf OnAPressed) 'no params to be used.
End Sub
Private Sub OnDPressed(ByVal params As Object())
Me.Text = String.Format("D was pressed with params: {0} - {1} - {2}", params) 'show that the parameters we wanted were passed successfully.
End Sub
Private Sub OnAPressed(ByVal params As Object())
Me.Text = "A was pressed, GG." 'we didn't pass any params to this function, so we just ignore them.
End Sub
[/highlight]
Okay, so that's pretty straightforward really, right? Just don't forget to Dispose the hotkeymanager class when you're done:
[highlight=vb.net]
If Me.hKeyMgr.IsActive Then Me.hKeyMgr.Dispose()
[/highlight]