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 Public Class Hotkey
Implements IMessageFilter Implements IMessageFilter
@ -10,7 +12,7 @@ Public Class Hotkey
''' <summary> ''' <summary>
''' Diesem Event wird immer die zugewiesene HotKeyID übergeben, wenn eine HotKey Kombination gedrückt wurde. ''' Diesem Event wird immer die zugewiesene HotKeyID übergeben, wenn eine HotKey Kombination gedrückt wurde.
''' </summary> ''' </summary>
Public Event HotKeyPressed(ByVal HotKeyID As String) Public Event HotKeyPressed(HotKeyID As String)
''' <summary> ''' <summary>
''' Definiert verfügbare Modfier Keys ''' Definiert verfügbare Modfier Keys
@ -22,7 +24,7 @@ Public Class Hotkey
MOD_WIN = 8 MOD_WIN = 8
End Enum End Enum
Sub New(ByVal pOwnerForm As Form) Sub New(pOwnerForm As Form)
_OwnerForm = pOwnerForm _OwnerForm = pOwnerForm
Application.AddMessageFilter(Me) Application.AddMessageFilter(Me)
End Sub End Sub
@ -33,13 +35,12 @@ Public Class Hotkey
''' <param name="pKeyCode">Den KeyCode für die Taste</param> ''' <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="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> ''' <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 If _HotkeyIDList.ContainsKey(pHotKeyID) = True Then
Exit Sub Exit Sub
End If End If
Dim oHotkeyId As Short = NativeMethods.GlobalAddAtom(pHotKeyID) Dim oHotkeyId As Short = NativeMethods.GlobalAddAtom(pHotKeyID.ToString())
_HotkeyIDList.Add(pHotKeyID, oHotkeyId)
_HotkeyList.Add(oHotkeyId, New HotKeyObject(pKeyCode, pModifiers, pHotKeyID)) _HotkeyList.Add(oHotkeyId, New HotKeyObject(pKeyCode, pModifiers, pHotKeyID))
NativeMethods.RegisterHotKey(_OwnerForm.Handle, oHotkeyId, _HotkeyList(oHotkeyId).Modifier, _HotkeyList(oHotkeyId).HotKey) 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) RaiseEvent HotKeyPressed(_HotkeyList(CShort(m.WParam)).HotKeyID)
End If End If
End If End If
Return False
End Function End Function
Public Class HotKeyObject Public Class HotKeyObject
Public Property HotKey() As Keys Public Property HotKey As Keys
Public Property Modifier() As ModfierKey Public Property Modifier As ModfierKey
Public Property HotKeyID() As String Public Property HotKeyID As Integer
Public Property AtomID() As Short 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 HotKey = NewHotKey
Modifier = NewModifier Modifier = NewModifier
HotKeyID = NewHotKeyID HotKeyID = NewHotKeyID