Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports Microsoft.WindowsCE.Forms
Imports System.Runtime.InteropServices
Public Class HotKeys
#Region "Enums"
'list of modifier keys to use
Public Enum KeyModifiers As Integer
None = 0
Alt = 1
Control = 2
Shift = 4
Windows = 8
Modkeyup = &H1000
End Enum
#End Region
#Region "DLL Imports"
"coredll.dll", Entrypoint:="RegisterHotKey", setLastError:=True)> _
Private Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, ByVal Modifiers As KeyModifiers, ByVal key As Integer) As Boolean
End Function
"coredll.dll", Entrypoint:="UnregisterHotKey", setLastError:=True)> _
Private Shared Function UnRegisterHotKey(ByVal hWnd As IntPtr, ByVal key As Integer) As Boolean
End Function
#End Region
#Region "Vars"
'message listener
Private wnd As New HotKeyMessageWindow(Me)
'public keypressed event, for the outside world to listen to
Public Event KeyPressed(ByVal key As Keys)
#End Region
#Region "Constructors"
Public Sub New()
MyBase.New()
End Sub
#End Region
#Region "Methods"
Public Sub Register(ByVal Key As System.Windows.Forms.Keys, Optional ByVal Modifier As KeyModifiers = KeyModifiers.None)
'register the key
RegisterHotKey(wnd.Hwnd, CInt(Key), Modifier, CInt(Key))
End Sub
Public Sub UnRegister(ByVal Key As System.Windows.Forms.Keys)
'unregister any previous keys
UnRegisterHotKey(wnd.Hwnd, CInt(Key))
End Sub
'handle the message listener's keypress
Public Sub OnKeyPressed(ByVal key As Keys)
'forward the keypress event to the outside world.
RaiseEvent KeyPressed(key)
End Sub
#End Region
#Region "MessageWindow"
'internal message listener.
Private Class HotKeyMessageWindow
Inherits MessageWindow
Private Const WM_HOTKEY = &H312
Dim parent As HotKeys
Public Sub New(ByVal h As HotKeys)
parent = h
End Sub
Protected Overrides Sub WndProc(ByRef msg As Message)
'look at the message type
Select Case msg.Msg
Case WM_HOTKEY
'it's a hot key, so raise the event
parent.OnKeyPressed(msg.WParam.ToInt32)
Case Else
'all other messages go to the base class
MyBase.WndProc(msg)
End Select
End Sub
End Class
#End Region
End Class