Hallo com,
hier mal meine Wrapper-Klasse für Hotkeys.
Wenn ich demnächst meine FARunTime release, wird sie Bestandteil sein.
Sie ist Option Stict On-gültig (wurde mit default an geschrieben; wie das geht, steht hier unter punkt 7)
Bei nachfragen zu speziellen Code-Abschnitten einfach Fragen
Die Deklarationen gibt es für .NET am besten bei P/Invoke.NET
Im Anhang noch die Datei dazu.
hier mal meine Wrapper-Klasse für Hotkeys.
Wenn ich demnächst meine FARunTime release, wird sie Bestandteil sein.
Sie ist Option Stict On-gültig (wurde mit default an geschrieben; wie das geht, steht hier unter punkt 7)
Bei nachfragen zu speziellen Code-Abschnitten einfach Fragen
Die Deklarationen gibt es für .NET am besten bei P/Invoke.NET
VB.NET-Quellcode
- Imports System.Windows.Forms
- Imports System.Runtime.InteropServices
- Namespace WinServices
- Public Class HotKeyProvider
- Implements IMessageFilter
- #Region "WinAPI"
- <DllImport("user32.dll")> Private Shared Function RegisterHotKey(ByVal Hwnd As IntPtr, ByVal ID As Integer, ByVal Modifiers As Integer, ByVal Key As Integer) As Integer
- End Function
- <DllImport("user32.dll")> Private Shared Function UnregisterHotKey(ByVal Hwnd As IntPtr, ByVal ID As Integer) As Integer
- End Function
- <DllImport("kernel32.dll")> Private Shared Function GlobalAddAtomA(ByVal IDString As String) As Short
- End Function
- <DllImport("kernel32.dll")> Private Shared Function GlobalDeleteAtom(ByVal Atom As Short) As Short
- End Function
- Private Const WM_HOTKEY As Integer = &H312
- #End Region
- ''' <summary>
- ''' Raised when a Hooked Hotkey is pressed. Gives the ID of the Hotkey for identification.
- ''' </summary>
- ''' <param name="hkID">The ID of the Hotkey, wich was pressed</param>
- ''' <remarks></remarks>
- Public Event HotKeyPressed(ByVal hkID As Short)
- Private parent As Form
- Private hotkeys As List(Of HK_DATA)
- Private Function GetHKByID(ByVal hkID As Short) As HK_DATA
- For Each hkd As HK_DATA In hotkeys
- If hkd.ID = hkID Then Return hkd
- Next
- Throw New HotKeyNotFoundException(CStr(hkID), "A Hotkey with the given System ID did not exist or was already Unhooked.")
- End Function
- Private Function GetHKByName(ByVal hkName As String) As HK_DATA
- For Each hkd As HK_DATA In hotkeys
- If hkd.AssociatedName = hkName Then Return hkd
- Next
- Throw New HotKeyNotFoundException(hkName, "A Hotkey with the given Name did not exist or was already Unhooked.")
- End Function
- ''' <summary>
- ''' Unhooks a Hotkey
- ''' </summary>
- ''' <param name="hotkeyID">ID of the Hotkey</param>
- ''' <remarks></remarks>
- Public Sub Unhook(ByVal hotkeyID As String)
- Dim iID As Integer = GetHKByName(hotkeyID).pInternalID
- hotkeys(iID).Destroy(parent)
- hotkeys.RemoveAt(iID)
- End Sub
- ''' <summary>
- ''' Hooks a global Hotkey
- ''' </summary>
- ''' <param name="hk">Structure describing the Hotkey with ID, Keys and Modifier Keys</param>
- ''' <remarks></remarks>
- Public Overloads Sub Hook(ByVal hk As Hotkey)
- hotkeys.Add(New HK_DATA(hk.HotKeyID, hotkeys.Count))
- RegisterHotKey(parent.Handle, hotkeys.Last.ID, hk.KeyModifier, hk.KeyCombo)
- End Sub
- ''' <summary>
- ''' Hooks a global Hotkey
- ''' </summary>
- ''' <param name="id">ID of the hotkey, given with the HotKeyPressed event</param>
- ''' <param name="keycombo">Key-combination for the Hotkey</param>
- ''' <param name="keymodifier"><c>Optional.</c> Additional keys to be pressed, Link them with [Or]</param>
- ''' <remarks></remarks>
- Public Overloads Sub Hook(ByVal id As String, ByVal keycombo As Keys, Optional ByVal keymodifier As Hotkey.Modifier = Hotkey.Modifier.None)
- Hook(New Hotkey(id, keycombo, keymodifier))
- End Sub
- ''' <summary>
- ''' Unhooks all hooked Hotkeys. Use this upon Application quitting
- ''' </summary>
- ''' <remarks></remarks>
- Public Sub UnHookAll()
- For i = hotkeys.Count - 1 To 0 Step -1
- hotkeys(i).Destroy(parent)
- hotkeys.RemoveAt(i)
- Next
- End Sub
- Public Sub New(ByVal parentForm As Form)
- parent = parentForm
- Application.AddMessageFilter(Me)
- hotkeys = New List(Of HK_DATA)
- End Sub
- Public Class HotKeyNotFoundException
- Inherits Exception
- Dim pM As String
- Dim pValue As String
- Public Sub New(ByVal paramValue As String, ByVal msg As String)
- pM = msg
- pValue = paramValue
- End Sub
- Public Overrides ReadOnly Property Message As String
- Get
- Return pM & " The actual value was:" & pValue
- End Get
- End Property
- End Class
- Private Structure HK_DATA
- Dim pID As Short
- Dim pAssocName As String
- Dim pInternalID As Integer
- Public Property ID As Short
- Get
- Return pID
- End Get
- Set(ByVal value As Short)
- pID = value
- End Set
- End Property
- Public Property AssociatedName As String
- Get
- Return pAssocName
- End Get
- Set(ByVal value As String)
- pAssocName = value
- End Set
- End Property
- Public Property InternalID As Integer
- Get
- Return pInternalID
- End Get
- Set(ByVal value As Integer)
- pInternalID = value
- End Set
- End Property
- Public Sub New(ByVal name As String, ByVal internalID As Integer)
- pAssocName = name
- pID = GlobalAddAtomA(pAssocName)
- pInternalID = internalID
- End Sub
- Public Sub Destroy(ByVal p As Form)
- UnregisterHotKey(p.Handle, CInt(pID))
- GlobalDeleteAtom(pID)
- pInternalID = -1
- End Sub
- End Structure
- Public Structure Hotkey
- Dim pHK As Keys
- Dim pMod As Modifier
- Dim pID As String
- Public Enum Modifier As Integer
- None = 0
- Alt = 1
- Control = 2
- Shift = 4
- WindowsKey = 8
- End Enum
- Public Property KeyCombo As Keys
- Get
- Return pHK
- End Get
- Set(ByVal value As Keys)
- pHK = value
- End Set
- End Property
- Public Property KeyModifier As Modifier
- Get
- Return pMod
- End Get
- Set(ByVal value As Modifier)
- pMod = value
- End Set
- End Property
- Public Property HotKeyID As String
- Get
- Return pID
- End Get
- Set(ByVal value As String)
- pID = value
- End Set
- End Property
- Public Sub New(ByVal ID As String, ByVal key As Keys, ByVal modkeys As Modifier)
- pID = ID
- pHK = key
- pMod = modkeys
- End Sub
- End Structure
- Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
- If m.Msg = WM_HOTKEY Then
- RaiseEvent HotKeyPressed(GetHKByID(CShort(m.WParam)).ID)
- End If
- Return False
- End Function
- End Class
- End Namespace
Im Anhang noch die Datei dazu.