Tastenkombination funktioniert nicht?

  • VB.NET

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von Strauss.

    Tastenkombination funktioniert nicht?

    Ich möchte, dass mein Programm etwas auslöst, wenn eine Tastenkombination gedrückt wird. Aber ich habe bis jetzt nichts gefunden. Das Ereignis soll auch ausgelöst werden, wenn das Programm nicht im Vordergrund ist. Hier ist mein bisheriger Code:

    VB.NET-Quellcode

    1. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    2. If GetAsyncKeyState(Keys.Alt And Keys.Space) Then
    3. MsgBox("hallo")
    4. End If
    5. End Sub


    Jedoch passiert nichts, wenn ich die Tastenkombination drücke. Auch bei anderen Tasten funktioniert das nicht. Wie muss ich das richtig machen? Ich bin noch nicht so gut in VB, darum würde ich gerne eine ausführliche Erklärung haben. :)
    Deine Code wird nicht nutzen !
    Du muß zuerst deine Tastenkombination regestrieren, dann kannst an deine Anwendung anbinden.
    Dazu brauchst WIN API:

    VB.NET-Quellcode

    1. Private Declare Function RegisterHotKey Lib "user32" ( _
    2. ByVal Hwnd As IntPtr, _
    3. ByVal ID As Integer, _
    4. ByVal Modifiers As Integer, _
    5. ByVal Key As Integer) _
    6. As Integer
    7. Private Declare Function UnregisterHotKey Lib "user32" ( _
    8. ByVal Hwnd As IntPtr, _
    9. ByVal ID As Integer) _
    10. As Integer
    11. Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" ( _
    12. ByVal IDString As String) _
    13. As Short
    14. Private Declare Function GlobalDeleteAtom Lib "kernel32" ( _
    15. ByVal Atom As Short) _
    16. As Short

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Alex2000“ ()

    Mit der WinAPI, die Alex 200 gepostet hat:

    VB.NET-Quellcode

    1. Private Declare Function RegisterHotKey Lib "user32" ( _
    2. ByVal Hwnd As IntPtr, _
    3. ByVal ID As Integer, _
    4. ByVal Modifiers As Integer, _
    5. ByVal Key As Integer) _
    6. As Integer


    um eine Tastatureingebe zu registrieren musst du dann warscheinlich das hier eingeben, nachdem du auch den Code von Alex bei dir eingefügt hast. Hab allerdings keine Ahnung, wofür die angegebenen Parameter gut sind.

    VB.NET-Quellcode

    1. RegisterHotKey(?,27837,828288,34) 'oder so
    Also iergendwie blicke ich da nicht ganz durch. Bei mir sieh das jetzt so aus:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Declare Function RegisterHotKey Lib "user32" (ByVal Hwnd As IntPtr, ByVal ID As Integer, ByVal Modifiers As Integer, ByVal Key As Integer) As Integer
    3. Private Declare Function UnregisterHotKey Lib "user32" (ByVal Hwnd As IntPtr, ByVal ID As Integer) As Integer
    4. Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" (ByVal IDString As String) As Short
    5. Private Declare Function GlobalDeleteAtom Lib "kernel32" (ByVal Atom As Short) As Short
    6. Private Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal nVirtKey As Long) As Integer
    7. Private Sub RegisterHotKeys()
    8. RegisterHotKey(Me.Handle, 1, Keys.Enter, Keys.Control)
    9. RegisterHotKey(?,27837,828288,34) 'Bei dem Fragezeichen sagt er: "Ausdruck erwartet."
    10. End Sub
    11. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    12. If GetAsyncKeyState(Keys.Enter) And GetAsyncKeyState(Keys.Control) Then
    13. MsgBox("hallo")
    14. End If
    15. End Sub
    16. End Class
    so wuerde es gehen

    VB.NET-Quellcode

    1. Public Class Form1
    2. Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
    3. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    4. Dim hotkey As Boolean
    5. Dim hotkey2 As Boolean
    6. hotkey = GetAsyncKeyState(Keys.A)
    7. hotkey2 = GetAsyncKeyState(Keys.B)
    8. If hotkey = True Then
    9. If hotkey2 = True Then
    10. MsgBox("es wurden die tasten A und B gedrueckt")
    11. End If
    12. End If
    13. End Sub
    14. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    15. Timer1.Start()
    16. Timer1.Interval = 100
    17. End Sub
    18. End Class



    GR :)
    Schäm dich nicht "Zu fragen", schäm dich "Nicht zu wissen". ?(
    Benutz am besten diese Klasse ist einfach Super.


    VB.NET-Quellcode

    1. ''' <summary>
    2. ''' Mit dieser Klasse kann man sehr leicht eine globale Hotkey funktionalität in seinem Programm einbinden.
    3. ''' Man muss nur diese Klasse mit WithEvents deklarieren und ihr eine Form zuweisen die gesubclassed werden soll.
    4. ''' Dann muss man nur noch ein paar eigene HotKey-Kombinationen registrieren (z.B. Strg+Alt+X) und diese
    5. ''' mit dem Event abfragen bzw, abfangen. Dazu muss man eine eigene HotKeyID angeben um einen bestimmte HotKey
    6. ''' Kombination später zu identifizieren wenn diese gedrückt wird. Wenn man z.B. eine Kombination registriert
    7. ''' und ihr z.B. die HotKeyID "TEST1" zugewiesen wird, dann kann man später im Event nach dieser ID "TEST1" fragen
    8. ''' und dann eine Funktion aufrufen die für diesen HotKey bestimmt wurde.
    9. ''' </summary>
    10. ''' <remarks>Tim Hartwig</remarks>
    11. Public Class clsHotKey
    12. Implements IMessageFilter
    13. Private Declare Function RegisterHotKey Lib "user32" ( _
    14. ByVal Hwnd As IntPtr, _
    15. ByVal ID As Integer, _
    16. ByVal Modifiers As Integer, _
    17. ByVal Key As Integer) _
    18. As Integer
    19. Private Declare Function UnregisterHotKey Lib "user32" ( _
    20. ByVal Hwnd As IntPtr, _
    21. ByVal ID As Integer) _
    22. As Integer
    23. Private Declare Function GlobalAddAtom Lib "kernel32" Alias "GlobalAddAtomA" ( _
    24. ByVal IDString As String) _
    25. As Short
    26. Private Declare Function GlobalDeleteAtom Lib "kernel32" ( _
    27. ByVal Atom As Short) _
    28. As Short
    29. Public Class HotKeyObject
    30. Private mHotKey As Keys
    31. Private mModifier As MODKEY
    32. Private mHotKeyID As String
    33. Private mAtomID As Short
    34. Public Property HotKey() As Keys
    35. Get
    36. Return mHotKey
    37. End Get
    38. Set(ByVal value As Keys)
    39. mHotKey = value
    40. End Set
    41. End Property
    42. Public Property Modifier() As MODKEY
    43. Get
    44. Return mModifier
    45. End Get
    46. Set(ByVal value As MODKEY)
    47. mModifier = value
    48. End Set
    49. End Property
    50. Public Property HotKeyID() As String
    51. Get
    52. Return mHotKeyID
    53. End Get
    54. Set(ByVal value As String)
    55. mHotKeyID = value
    56. End Set
    57. End Property
    58. Public Property AtomID() As Short
    59. Get
    60. Return mAtomID
    61. End Get
    62. Set(ByVal value As Short)
    63. mAtomID = value
    64. End Set
    65. End Property
    66. Sub New(ByVal NewHotKey As Keys, ByVal NewModifier As MODKEY, ByVal NewHotKeyID As String)
    67. mHotKey = NewHotKey
    68. mModifier = NewModifier
    69. mHotKeyID = NewHotKeyID
    70. End Sub
    71. End Class
    72. Private mForm As Form
    73. Private Const WM_HOTKEY As Integer = &H312
    74. Private mHotKeyList As New System.Collections.Generic.Dictionary(Of Short, HotKeyObject)
    75. Private mHotKeyIDList As New System.Collections.Generic.Dictionary(Of String, Short)
    76. ''' <summary>
    77. ''' Diesem Event wird immer die zugewiesene HotKeyID übergeben wenn eine HotKey Kombination gedrückt wurde.
    78. ''' </summary>
    79. Public Event HotKeyPressed(ByVal HotKeyID As String)
    80. Public Enum MODKEY As Integer
    81. MOD_ALT = 1
    82. MOD_CONTROL = 2
    83. MOD_SHIFT = 4
    84. MOD_WIN = 8
    85. End Enum
    86. Sub New(ByVal OwnerForm As Form)
    87. mForm = OwnerForm
    88. Application.AddMessageFilter(Me)
    89. End Sub
    90. ''' <summary>
    91. ''' Diese Funktion fügt einen Hotkey hinzu und registriert ihn auch sofort
    92. ''' </summary>
    93. ''' <param name="KeyCode">Den KeyCode für die Taste</param>
    94. ''' <param name="Modifiers">Die Zusatztasten wie z.B. Strg oder Alt, diese können auch mit OR kombiniert werden</param>
    95. ''' <param name="HotKeyID">Die ID die der Hotkey bekommen soll um diesen zu identifizieren</param>
    96. Public Sub AddHotKey(ByVal KeyCode As Keys, ByVal Modifiers As MODKEY, ByVal HotKeyID As String)
    97. If mHotKeyIDList.ContainsKey(HotKeyID) = True Then Exit Sub
    98. Dim ID As Short = GlobalAddAtom(HotKeyID)
    99. mHotKeyIDList.Add(HotKeyID, ID)
    100. mHotKeyList.Add(ID, New HotKeyObject(KeyCode, Modifiers, HotKeyID))
    101. RegisterHotKey(mForm.Handle, ID, mHotKeyList(ID).Modifier, mHotKeyList(ID).HotKey)
    102. End Sub
    103. ''' <summary>
    104. ''' Diese Funktion entfernt einen Hotkey und deregistriert ihn auch sofort
    105. ''' </summary>
    106. ''' <param name="HotKeyID">Gibt die HotkeyID an welche entfernt werden soll</param>
    107. Public Sub RemoveHotKey(ByVal HotKeyID As String)
    108. If mHotKeyIDList.ContainsKey(HotKeyID) = False Then Exit Sub
    109. Dim ID As Short = mHotKeyIDList(HotKeyID)
    110. mHotKeyIDList.Remove(HotKeyID)
    111. mHotKeyList.Remove(ID)
    112. UnregisterHotKey(mForm.Handle, CInt(ID))
    113. GlobalDeleteAtom(ID)
    114. End Sub
    115. Private Function PreFilterMessage(ByRef m As System.Windows.Forms.Message) As Boolean Implements System.Windows.Forms.IMessageFilter.PreFilterMessage
    116. If m.Msg = WM_HOTKEY Then
    117. RaiseEvent HotKeyPressed(mHotKeyList(CShort(m.WParam)).HotKeyID)
    118. End If
    119. End Function
    120. End Class


    So benutzt du es :

    VB.NET-Quellcode

    1. 'Deklarieren der Klasse in einer Form
    2. Dim WithEvents HotKey As New clsHotKey(Me)
    3. 'Registrieren der Hotkeys
    4. HotKey.AddHotKey(Keys.F11, clsHotKey.MODKEY.MOD_CONTROL, "SaveScreenShot")
    5. 'Eingang des Hotkey-Events abfragen
    6. Private Sub ReceiveHotKey(ByVal HotKeyID As String) Handles HotKey.HotKeyPressed
    7. Select Case
    8. Case "SaveScreenShot"
    9. MsgBox ("Hotkey SaveScreenShot gedrückt")
    10. End Select
    11. End Sub
    12. 'Entfernen der Hotkeys
    13. Hotkey.RemoveHotKey("SaveScreenShot")


    Quelle : dotnet-snippets.de/dns/globale…kombinationen-SID245.aspx