Visual Basic Forum

Visual Basic Forum
für VB6 und VB.NET Programmierer
 
RegistrierenRegistrieren  LoginLogin

Neues Thema eröffnen   Neue Antwort erstellen    Visual Basic Forum Foren-Übersicht -> [VB.NET] Tipps & Tricks
Autor
Nachricht
^^kev####
Überflieger
Überflieger

Anmeldedatum: 01.05.2008
Beiträge: 476
Wohnort: Haan

Globale HotKeys (Tastenkombinationen)
Verfasst am: 29.06.2009, 14:11

Beschreibung:
Mit dieser Klasse kann man sehr leicht eine globale Hotkey funktionalität in seinem Programm einbinden.
Man muss nur diese Klasse mit WithEvents deklarieren und ihr eine Form zuweisen die gesubclassed werden soll.
Dann muss man nur noch ein paar eigene HotKey-Kombinationen registrieren (z.B. Strg+Alt+X) und diese
mit dem Event abfragen bzw, abfangen. Dazu muss man eine eigene HotKeyID angeben um einen bestimmte HotKey
Kombination später zu identifizieren wenn diese gedrückt wird. Wenn man z.B. eine Kombination registriert
und ihr z.B. die HotKeyID "TEST1" zugewiesen wird, dann kann man später im Event nach dieser ID "TEST1" fragen
und dann eine Funktion aufrufen die für diesen HotKey bestimmt wurde.

PS: Globale Hotkeys sind globale Tastenkombinationen wie z.B. Strg+Alt+Entf. Das heißt diese sind überall in Windows verfügbar, auch während eines Spiels etc. Man muss nicht unbedingt immer 3 Tasten zuweisen man brauch jedoch ein Modifier (Strg,Alt,Shift oder Win) und eine normale Taste wie z.B. Alt+X oder Shift+Y.

Achtung: wenn man mehrere Modifier Tasten oder normale Tasten verwenden will z.B. für eine solche Kombination: Strg+Alt+Shift+X dann muss man diese mit OR verknüpfen:
AddHotKey(Keys.X, MODKEY.MOD_CONTROL Or MODKEY.MOD_ALT Or MODKEY.MOD_SHIFT "HK_1")

Funktion:
Visual Basic: [code]Public Class clsHotKey
Implements IMessageFilter

Private Declare Function RegisterHotKey Lib "user32" ( _
ByVal Hwnd As IntPtr, _
ByVal ID As Integer, _
ByVal Modifiers As Integer, _
ByVal Key As Integer) _
As Integer

Private Declare Function UnregisterHotKey Lib "user32" ( _
ByVal Hwnd As IntPtr, _
ByVal ID As Integer) _
As Integer

Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" ( _
ByVal IDString As String) _
As Short

Private Declare Function GlobalDeleteAtom Lib "kernel32" ( _
ByVal Atom As Short) _
As Short

Public Class HotKeyObject
Private mHotKey As Keys
Private mModifier As MODKEY
Private mHotKeyID As String
Private mAtomID As Short

Public Property HotKey() As Keys
Get
Return mHotKey
End Get
Set(ByVal value As Keys)
mHotKey = value
End Set
End Property

Public Property Modifier() As MODKEY
Get
Return mModifier
End Get
Set(ByVal value As MODKEY)
mModifier = value
End Set
End Property

Public Property HotKeyID() As String
Get
Return mHotKeyID
End Get
Set(ByVal value As String)
mHotKeyID = value
End Set
End Property

Public Property AtomID() As Short
Get
Return mAtomID
End Get
Set(ByVal value As Short)
mAtomID = value
End Set
End Property

Sub New(ByVal NewHotKey As Keys, ByVal NewModifier As MODKEY, ByVal NewHotKeyID As String)
mHotKey = NewHotKey
mModifier = NewModifier
mHotKeyID = NewHotKeyID
End Sub
End Class

Private mForm As Form
Private Const WM_HOTKEY As Integer = &H312
Private mHotKeyList As New System.Collections.Generic.Dictionary(Of Short, HotKeyObject)
Private mHotKeyIDList As New System.Collections.Generic.Dictionary(Of String, Short)

'''
''' Diesem Event wird immer die zugewiesene HotKeyID übergeben wenn eine HotKey Kombination gedrückt wurde.
'''

Public Event HotKeyPressed(ByVal HotKeyID As String)

Public Enum MODKEY As Integer
MOD_ALT = 1
MOD_CONTROL = 2
MOD_SHIFT = 4
MOD_WIN = 8
End Enum

Sub New(ByVal OwnerForm As Form)
mForm = OwnerForm
Application.AddMessageFilter(Me)
End Sub

'''
''' Diese Funktion fügt einen Hotkey hinzu und registriert ihn auch sofort
'''

''' Den KeyCode für die Taste
''' Die Zusatztasten wie z.B. Strg oder Alt, diese können auch mit OR kombiniert werden
''' Die ID die der Hotkey bekommen soll um diesen zu identifizieren
Public Sub AddHotKey(ByVal KeyCode As Keys, ByVal Modifiers As MODKEY, ByVal HotKeyID As String)
If mHotKeyIDList.ContainsKey(HotKeyID) = True Then Exit Sub
Dim ID As Short = GlobalAddAtom(HotKeyID)
mHotKeyIDList.Add(HotKeyID, ID)
mHotKeyList.Add(ID, New HotKeyObject(KeyCode, Modifiers, HotKeyID))
RegisterHotKey(mForm.Handle, ID, mHotKeyList(ID).Modifier, mHotKeyList(ID).HotKey)
End Sub

'''
''' Diese Funktion entfernt einen Hotkey und deregistriert ihn auch sofort
'''

''' Gibt die HotkeyID an welche entfernt werden soll
Public Sub RemoveHotKey(ByVal HotKeyID As String)
If mHotKeyIDList.ContainsKey(HotKeyID) = False Then Exit Sub
Dim ID As Short = mHotKeyIDList(HotKeyID)
mHotKeyIDList.Remove(HotKeyID)
mHotKeyList.Remove(ID)
UnregisterHotKey(mForm.Handle, CInt(ID))
GlobalDeleteAtom(ID)
End Sub

Private 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(mHotKeyList(CShort(m.WParam)).HotKeyID)
End If
End Function
End Class[/code]

Quelle: dotnet-snippets.de
_________________
 
Neues Thema eröffnen   Neue Antwort erstellen    Visual Basic Forum Foren-Übersicht -> [VB.NET] Tipps & Tricks

Tags: visual basic, globale hotkey, globale hotkeys

 
 Verwandte Themen   Aufrufe   Letzter Beitrag 
Keine neuen Beiträge Problem mit always on top funktion 633 30.10.2009, 12:26
Keine neuen Beiträge IntStr()funktion 510 11.10.2007, 09:49
Keine neuen Beiträge TopMost Funktion unter DirectX 432 27.07.2007, 11:48
Keine neuen Beiträge Wie Funktion nutzen ? 540 29.06.2007, 21:59
Keine neuen Beiträge update funktion einbauen 741 29.01.2007, 00:11
 



[ Time: 0.1917s ][ Queries: 81 (0.0556s) ][ GZIP on - Debug on ]