Windows: Improve Hotkey

This commit is contained in:
Jonathan Jenne 2021-03-03 10:32:36 +01:00
parent 1d315a15b8
commit b0da663756

View File

@ -1,4 +1,6 @@
Imports System.Windows.Forms
Option Explicit On
Imports System.Windows.Forms
Public Class Hotkey
Implements IMessageFilter
@ -10,7 +12,7 @@ Public Class Hotkey
''' <summary>
''' Diesem Event wird immer die zugewiesene HotKeyID übergeben, wenn eine HotKey Kombination gedrückt wurde.
''' </summary>
Public Event HotKeyPressed(ByVal HotKeyID As String)
Public Event HotKeyPressed(HotKeyID As String)
''' <summary>
''' Definiert verfügbare Modfier Keys
@ -22,7 +24,7 @@ Public Class Hotkey
MOD_WIN = 8
End Enum
Sub New(ByVal pOwnerForm As Form)
Sub New(pOwnerForm As Form)
_OwnerForm = pOwnerForm
Application.AddMessageFilter(Me)
End Sub
@ -33,13 +35,12 @@ Public Class Hotkey
''' <param name="pKeyCode">Den KeyCode für die Taste</param>
''' <param name="pModifiers">Die Zusatztasten wie z.B. Strg oder Alt, diese können auch mit OR kombiniert werden</param>
''' <param name="pHotKeyID">Die ID die der Hotkey bekommen soll um diesen zu identifizieren</param>
Public Sub AddHotKey(ByVal pKeyCode As Keys, ByVal pModifiers As ModfierKey, ByVal pHotKeyID As Integer)
Public Sub AddHotKey(pKeyCode As Keys, pModifiers As ModfierKey, pHotKeyID As Integer)
If _HotkeyIDList.ContainsKey(pHotKeyID) = True Then
Exit Sub
End If
Dim oHotkeyId As Short = NativeMethods.GlobalAddAtom(pHotKeyID)
_HotkeyIDList.Add(pHotKeyID, oHotkeyId)
Dim oHotkeyId As Short = NativeMethods.GlobalAddAtom(pHotKeyID.ToString())
_HotkeyList.Add(oHotkeyId, New HotKeyObject(pKeyCode, pModifiers, pHotKeyID))
NativeMethods.RegisterHotKey(_OwnerForm.Handle, oHotkeyId, _HotkeyList(oHotkeyId).Modifier, _HotkeyList(oHotkeyId).HotKey)
@ -67,15 +68,16 @@ Public Class Hotkey
RaiseEvent HotKeyPressed(_HotkeyList(CShort(m.WParam)).HotKeyID)
End If
End If
Return False
End Function
Public Class HotKeyObject
Public Property HotKey() As Keys
Public Property Modifier() As ModfierKey
Public Property HotKeyID() As String
Public Property AtomID() As Short
Public Property HotKey As Keys
Public Property Modifier As ModfierKey
Public Property HotKeyID As Integer
Public Property AtomID As Short
Sub New(ByVal NewHotKey As Keys, ByVal NewModifier As ModfierKey, ByVal NewHotKeyID As String)
Sub New(NewHotKey As Keys, NewModifier As ModfierKey, NewHotKeyID As Integer)
HotKey = NewHotKey
Modifier = NewModifier
HotKeyID = NewHotKeyID