RegisterGlobalHotkey - Hotkeys, systemweit: Schlanke Wrapper-Klasse für komfortable Nutzung

    • VB.NET

    Es gibt 1 Antwort in diesem Thema. Der letzte Beitrag () ist von Agent.

      RegisterGlobalHotkey - Hotkeys, systemweit: Schlanke Wrapper-Klasse für komfortable Nutzung

      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

      VB.NET-Quellcode

      1. Imports System.Windows.Forms
      2. Imports System.Runtime.InteropServices
      3. Namespace WinServices
      4. Public Class HotKeyProvider
      5. Implements IMessageFilter
      6. #Region "WinAPI"
      7. <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
      8. End Function
      9. <DllImport("user32.dll")> Private Shared Function UnregisterHotKey(ByVal Hwnd As IntPtr, ByVal ID As Integer) As Integer
      10. End Function
      11. <DllImport("kernel32.dll")> Private Shared Function GlobalAddAtomA(ByVal IDString As String) As Short
      12. End Function
      13. <DllImport("kernel32.dll")> Private Shared Function GlobalDeleteAtom(ByVal Atom As Short) As Short
      14. End Function
      15. Private Const WM_HOTKEY As Integer = &H312
      16. #End Region
      17. ''' <summary>
      18. ''' Raised when a Hooked Hotkey is pressed. Gives the ID of the Hotkey for identification.
      19. ''' </summary>
      20. ''' <param name="hkID">The ID of the Hotkey, wich was pressed</param>
      21. ''' <remarks></remarks>
      22. Public Event HotKeyPressed(ByVal hkID As Short)
      23. Private parent As Form
      24. Private hotkeys As List(Of HK_DATA)
      25. Private Function GetHKByID(ByVal hkID As Short) As HK_DATA
      26. For Each hkd As HK_DATA In hotkeys
      27. If hkd.ID = hkID Then Return hkd
      28. Next
      29. Throw New HotKeyNotFoundException(CStr(hkID), "A Hotkey with the given System ID did not exist or was already Unhooked.")
      30. End Function
      31. Private Function GetHKByName(ByVal hkName As String) As HK_DATA
      32. For Each hkd As HK_DATA In hotkeys
      33. If hkd.AssociatedName = hkName Then Return hkd
      34. Next
      35. Throw New HotKeyNotFoundException(hkName, "A Hotkey with the given Name did not exist or was already Unhooked.")
      36. End Function
      37. ''' <summary>
      38. ''' Unhooks a Hotkey
      39. ''' </summary>
      40. ''' <param name="hotkeyID">ID of the Hotkey</param>
      41. ''' <remarks></remarks>
      42. Public Sub Unhook(ByVal hotkeyID As String)
      43. Dim iID As Integer = GetHKByName(hotkeyID).pInternalID
      44. hotkeys(iID).Destroy(parent)
      45. hotkeys.RemoveAt(iID)
      46. End Sub
      47. ''' <summary>
      48. ''' Hooks a global Hotkey
      49. ''' </summary>
      50. ''' <param name="hk">Structure describing the Hotkey with ID, Keys and Modifier Keys</param>
      51. ''' <remarks></remarks>
      52. Public Overloads Sub Hook(ByVal hk As Hotkey)
      53. hotkeys.Add(New HK_DATA(hk.HotKeyID, hotkeys.Count))
      54. RegisterHotKey(parent.Handle, hotkeys.Last.ID, hk.KeyModifier, hk.KeyCombo)
      55. End Sub
      56. ''' <summary>
      57. ''' Hooks a global Hotkey
      58. ''' </summary>
      59. ''' <param name="id">ID of the hotkey, given with the HotKeyPressed event</param>
      60. ''' <param name="keycombo">Key-combination for the Hotkey</param>
      61. ''' <param name="keymodifier"><c>Optional.</c> Additional keys to be pressed, Link them with [Or]</param>
      62. ''' <remarks></remarks>
      63. Public Overloads Sub Hook(ByVal id As String, ByVal keycombo As Keys, Optional ByVal keymodifier As Hotkey.Modifier = Hotkey.Modifier.None)
      64. Hook(New Hotkey(id, keycombo, keymodifier))
      65. End Sub
      66. ''' <summary>
      67. ''' Unhooks all hooked Hotkeys. Use this upon Application quitting
      68. ''' </summary>
      69. ''' <remarks></remarks>
      70. Public Sub UnHookAll()
      71. For i = hotkeys.Count - 1 To 0 Step -1
      72. hotkeys(i).Destroy(parent)
      73. hotkeys.RemoveAt(i)
      74. Next
      75. End Sub
      76. Public Sub New(ByVal parentForm As Form)
      77. parent = parentForm
      78. Application.AddMessageFilter(Me)
      79. hotkeys = New List(Of HK_DATA)
      80. End Sub
      81. Public Class HotKeyNotFoundException
      82. Inherits Exception
      83. Dim pM As String
      84. Dim pValue As String
      85. Public Sub New(ByVal paramValue As String, ByVal msg As String)
      86. pM = msg
      87. pValue = paramValue
      88. End Sub
      89. Public Overrides ReadOnly Property Message As String
      90. Get
      91. Return pM & " The actual value was:" & pValue
      92. End Get
      93. End Property
      94. End Class
      95. Private Structure HK_DATA
      96. Dim pID As Short
      97. Dim pAssocName As String
      98. Dim pInternalID As Integer
      99. Public Property ID As Short
      100. Get
      101. Return pID
      102. End Get
      103. Set(ByVal value As Short)
      104. pID = value
      105. End Set
      106. End Property
      107. Public Property AssociatedName As String
      108. Get
      109. Return pAssocName
      110. End Get
      111. Set(ByVal value As String)
      112. pAssocName = value
      113. End Set
      114. End Property
      115. Public Property InternalID As Integer
      116. Get
      117. Return pInternalID
      118. End Get
      119. Set(ByVal value As Integer)
      120. pInternalID = value
      121. End Set
      122. End Property
      123. Public Sub New(ByVal name As String, ByVal internalID As Integer)
      124. pAssocName = name
      125. pID = GlobalAddAtomA(pAssocName)
      126. pInternalID = internalID
      127. End Sub
      128. Public Sub Destroy(ByVal p As Form)
      129. UnregisterHotKey(p.Handle, CInt(pID))
      130. GlobalDeleteAtom(pID)
      131. pInternalID = -1
      132. End Sub
      133. End Structure
      134. Public Structure Hotkey
      135. Dim pHK As Keys
      136. Dim pMod As Modifier
      137. Dim pID As String
      138. Public Enum Modifier As Integer
      139. None = 0
      140. Alt = 1
      141. Control = 2
      142. Shift = 4
      143. WindowsKey = 8
      144. End Enum
      145. Public Property KeyCombo As Keys
      146. Get
      147. Return pHK
      148. End Get
      149. Set(ByVal value As Keys)
      150. pHK = value
      151. End Set
      152. End Property
      153. Public Property KeyModifier As Modifier
      154. Get
      155. Return pMod
      156. End Get
      157. Set(ByVal value As Modifier)
      158. pMod = value
      159. End Set
      160. End Property
      161. Public Property HotKeyID As String
      162. Get
      163. Return pID
      164. End Get
      165. Set(ByVal value As String)
      166. pID = value
      167. End Set
      168. End Property
      169. Public Sub New(ByVal ID As String, ByVal key As Keys, ByVal modkeys As Modifier)
      170. pID = ID
      171. pHK = key
      172. pMod = modkeys
      173. End Sub
      174. End Structure
      175. Public Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
      176. If m.Msg = WM_HOTKEY Then
      177. RaiseEvent HotKeyPressed(GetHKByID(CShort(m.WParam)).ID)
      178. End If
      179. Return False
      180. End Function
      181. End Class
      182. End Namespace


      Im Anhang noch die Datei dazu.
      Dateien