Mausrad als Hotkey verwenden

  • VB.NET

Es gibt 8 Antworten in diesem Thema. Der letzte Beitrag () ist von SystemUnknow.

    Mausrad als Hotkey verwenden

    HI...
    Und zwar wollte ich mit:

    VB.NET-Quellcode

    1. Dim Hotkeys As String
    2. Hotkeys = GetAsyncKeyState(Keys.M)
    3. If Hotkeys = True Then
    4. MsgBox("geht")
    5. End If

    Überprüfen ob das Mausrad betätigt wurde bzw. wird.
    Nur weiß ich nicht was für ein Key. ich verwenden muss.
    Ich habe bereits nach Mwheel etc. geschaut doch dies funktioniert leider auch nicht.
    Ich hoffe mir kann jemand weiterhelfen.

    Lg
    Das hat mich jetzt leider nicht sehr weitergeholfen.
    Kennt vielleicht jemand eine simplere Lösung?
    ich habs bereits mit dem Mousewheel event probiert allerdings funktioniert dies nur wenn die Form focusiert ist, und
    nicht wenn es im Hintergrund läuft.

    Lg
    Mit GetKeyAsyncState kannst Du leider keine Mouserad Bewegungen entdecken, probiers selber aus.Mit folgender Methode werden alle zulässigen VKeys angezeigt die seit der letzten Sekunde gedrückt wurden:

    VB.NET-Quellcode

    1. Public Class Form1
    2. Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Int32) As Short
    3. Dim WithEvents tim As New Timer
    4. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    5. tim.Interval = 1000
    6. tim.Start()
    7. End Sub
    8. ' timer event jede Sekunde
    9. Sub timerTick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tim.Tick
    10. For Each key As Integer In [Enum].GetValues(GetType(Keys))
    11. Dim keyName As String = [Enum].GetName(GetType(Keys), key)
    12. If GetAsyncKeyState(key) = 1 Then
    13. Trace.WriteLine(keyName & " wurde seit letztem Aufruf gedrückt")
    14. End If
    15. Next
    16. End Sub
    17. End Class

    Einzige Möglichkeit die wohl funktionieren könnte wäre ein globaler Low Level Mousehook
    also bei keys finde ich nur MButon und der wird durch das drücken des Scrollrads ausgelöst aber nichts durchs scrollen.
    Ich habe hier im Forum diesen Code gefunden:

    VB.NET-Quellcode

    1. Private Sub Form1_MouseWheel(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseWheel
    2. If e.Delta < -1 Then
    3. ElseIf e.Delta > -1 Then
    4. End If
    5. End Sub

    Der ist in etwa das was ich suche nur muss hier eben die Form Fokusiert sein.
    Nun bin ich noch am probieren wie ich das event systemweit abfangen kann.
    Aber da komme ich eben nicht weiter, deswegen würde ich mirch über Tipps wie ich das verwirkliche freuen.

    Lg

    SystemUnknow schrieb:

    APISystemParameterInfo

    Alex, wenn ich noch nicht ganz eingeschlafen bin setzen diese API's nur die MouseRad Properties, fangen also keine Events ab. Aber lass mich gerne belehren ...

    Ein globaler MouseHook fängt zwar die Bewegung des Mausrads ab, aber leider weder die Richtung noch die "Klicks":

    MouseHook Klasse aus einer meiner Libs:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. '##############################################################################################################
    2. '##### CLASS MOUSEHOOK #######
    3. '##############################################################################################################
    4. Imports System.Runtime.InteropServices
    5. Public Class MouseHook
    6. '#########################################################
    7. '##### PROPERTIES ##########
    8. '#########################################################
    9. '-----------------------------------
    10. '----- Structures ----
    11. '-----------------------------------
    12. Private Const WH_MOUSE_LL As Int32 = 14
    13. Enum mouseParams
    14. LeftButtonDown = 513
    15. LeftButtonUp = 514
    16. RightButtonDown = 516
    17. RightButtonUp = 517
    18. MouseWheel = &H20A
    19. End Enum
    20. ' callback structure
    21. Public Structure MSLLHOOKSTRUCT
    22. Public pt As Point
    23. Public mouseData As Int32
    24. Public flags As Int32
    25. Public time As Int32
    26. Public extra As IntPtr
    27. End Structure
    28. '-----------------------------------
    29. '----- Members ----
    30. '-----------------------------------
    31. Private Shared _lpMouseHook As IntPtr
    32. Private Delegate Function CallBack(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
    33. <MarshalAs(UnmanagedType.FunctionPtr)> _
    34. Private Shared _mouseProc As CallBack
    35. Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Int32, ByVal HookProc As CallBack, ByVal hInstance As IntPtr, ByVal wParam As Int32) As IntPtr
    36. Private Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hook As IntPtr) As Boolean
    37. Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal idHook As Int32, ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
    38. 'Private Declare Function GetCurrentThreadId Lib "kernel32.dll" () As Integer
    39. Private Declare Function GetModuleHandleW Lib "kernel32.dll" (ByVal fakezero As IntPtr) As IntPtr
    40. '-----------------------------------
    41. '----- Event Handler ----
    42. '-----------------------------------
    43. Public Shared Event mEvent(ByVal mouseEvent As Integer, ByVal params As MSLLHOOKSTRUCT)
    44. '#########################################################
    45. '##### METHODS ##########
    46. '#########################################################
    47. '-----------------------------------
    48. '----- InstallHook ----
    49. '-----------------------------------
    50. Public Shared Function InstallHook() As Boolean
    51. If _lpMouseHook = IntPtr.Zero Then
    52. _mouseProc = New CallBack(AddressOf MouseHookProc)
    53. _lpMouseHook = SetWindowsHookExW(WH_MOUSE_LL, _mouseProc, GetModuleHandleW(IntPtr.Zero), 0)
    54. End If
    55. Return _lpMouseHook <> IntPtr.Zero
    56. End Function
    57. '-----------------------------------
    58. '----- RemoveHook ----
    59. '-----------------------------------
    60. Public Shared Sub RemoveHook()
    61. If _lpMouseHook = IntPtr.Zero Then Return
    62. UnhookWindowsHookEx(_lpMouseHook)
    63. _lpMouseHook = IntPtr.Zero
    64. End Sub
    65. '-----------------------------------
    66. '----- Callback MouseHookProc ----
    67. '-----------------------------------
    68. Private Shared Function MouseHookProc(ByVal nCode As Int32, ByVal wParam As IntPtr, ByRef lParam As MSLLHOOKSTRUCT) As Int32
    69. ' Debug.Print("Message = {0}, x={1}, y={2}", wParam.ToInt32, lParam.pt.X, lParam.pt.Y)
    70. RaiseEvent mEvent(wParam.ToInt32, lParam)
    71. Return CallNextHookEx(WH_MOUSE_LL, nCode, wParam, lParam)
    72. End Function
    73. End Class



    Beispiel zur Verwendung hier:
    Spoiler anzeigen

    VB.NET-Quellcode

    1. Public Class Form1
    2. Public Shared WithEvents mouseEv As New MouseHook
    3. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHook.Click
    4. MouseHook.InstallHook()
    5. End Sub
    6. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUnhook.Click
    7. MouseHook.RemoveHook()
    8. End Sub
    9. Private Shared Sub myevent(ByVal e As MouseHook.mouseParams, ByVal mouseData As MouseHook.MSLLHOOKSTRUCT) Handles mouseEv.mEvent
    10. If e = MouseHook.mouseParams.MouseWheel Then Trace.WriteLine("Mouserad wurde betätigt")
    11. End Sub
    12. End Class