Results 1 to 2 of 2

Thread: How to Detect whether the Scroll Button in a Mouse is Scrolled...

  1. #1
    Lively Member
    Join Date
    Dec 00
    Location
    India, Chennai
    Posts
    121

    Exclamation How to Detect whether the Scroll Button in a Mouse is Scrolled...

    Hi Guys,
    How can I detect whether the Scroll Button in a Mouse is Scrolled or Not. I tried with the MouseMouse, Up and Down events. But it's of no use. Is there any other way to check the Button is scrolled or not. Thnx in advance.

  2. #2
    Matthew Gates
    Guest
    Try this:


    VB Code:
    1. 'Author: Yonatan
    2. 'Origin: [url]http://www.vbforums.com/[/url]
    3. 'Purpose: Detect Mouse Wheel
    4. 'Version: VB5+
    5. 'Message from Yonatan:
    6. '-Here's some way to use the mouse Wheel Button.
    7. '-It uses Subclassing, so be extra careful...  
    8. '-It also uses CopyMemory to quickly calculate LoWords and
    9. '-HiWords where needed.
    10. '-You need a Module and a Form called Form1 for this code.
    11. '-To test it, rotate and click the Wheel button on the Form, and
    12. '-watch the Immediate window for results! (If it's not there, hit
    13. '-Ctrl+G and it will appear.)
    14. '-The entire code is in the module, if you need to change it.
    15.  
    16.  
    17. 'Module Code:
    18.  
    19. Option Explicit
    20.  
    21. Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    22. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    23. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSrc As Any, ByVal cbSrc As Long)
    24.  
    25. Private Const GWL_WNDPROC = (-4)
    26. Private Const WM_MOUSEWHEEL = &H20A
    27. Private Const WHEEL_DELTA = 120
    28. Private Const MK_LBUTTON = &H1
    29. Private Const MK_RBUTTON = &H2
    30. Private Const MK_SHIFT = &H4
    31. Private Const MK_CONTROL = &H8
    32. Private Const MK_MBUTTON = &H10
    33. Private Const WM_MBUTTONDOWN = &H207
    34. Private Const WM_MBUTTONUP = &H208
    35. Private Const WM_MBUTTONDBLCLK = &H209
    36.  
    37. Private lpPrevWndProc As Long
    38.  
    39. Function LoWord(ByVal dwDoubleWord As Long) As Integer
    40.     Call CopyMemory(LoWord, dwDoubleWord, 2)
    41. End Function
    42.  
    43. Function HiWord(ByVal dwDoubleWord As Long) As Integer
    44.     Call CopyMemory(HiWord, ByVal VarPtr(dwDoubleWord) + 2, 2)
    45. End Function
    46.  
    47. Private Function WindowProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    48.     Dim fwKeys As Integer, zDelta As Integer, sMsg As String
    49.     Select Case uMsg
    50.     Case WM_MBUTTONDOWN
    51.         sMsg = "The Wheel button is down!"
    52.     Case WM_MBUTTONUP
    53.         sMsg = "The Wheel button isn't down anymore!"
    54.     Case WM_MBUTTONDBLCLK
    55.         sMsg = "The Wheel button has been double-clicked!"
    56.     Case WM_MOUSEWHEEL
    57.         fwKeys = LoWord(wParam)
    58.         zDelta = HiWord(wParam) / WHEEL_DELTA
    59.         sMsg = "Wheel rotated " & Abs(zDelta) & " ticks " & IIf(zDelta > 0, "forward!", "backward!")
    60.         If (fwKeys And MK_LBUTTON) = MK_LBUTTON Then sMsg = sMsg & vbNewLine & "The left button was down!"
    61.         If (fwKeys And MK_RBUTTON) = MK_RBUTTON Then sMsg = sMsg & vbNewLine & "The right button was down!"
    62.         If (fwKeys And MK_SHIFT) = MK_SHIFT Then sMsg = sMsg & vbNewLine & "The shift key was down!"
    63.         If (fwKeys And MK_CONTROL) = MK_CONTROL Then sMsg = sMsg & vbNewLine & "The ctrl key was down!"
    64.         If (fwKeys And MK_MBUTTON) = MK_MBUTTON Then sMsg = sMsg & vbNewLine & "The Wheel button was down!"
    65.     End Select
    66.     If Len(sMsg) > 0 Then Debug.Print sMsg
    67.     WindowProc = CallWindowProc(lpPrevWndProc, hWnd, uMsg, wParam, lParam)
    68. End Function
    69.  
    70. Public Sub Hook()
    71.     lpPrevWndProc = SetWindowLong(Form1.hWnd, GWL_WNDPROC, AddressOf WindowProc)
    72. End Sub
    73.  
    74. Public Sub Unhook()
    75.     Call SetWindowLong(Form1.hWnd, GWL_WNDPROC, lpPrevWndProc)
    76. End Sub
    77.  
    78.  
    79. 'Form Code:
    80.  
    81. Private Sub Form_Load()
    82.     Call Hook
    83. End Sub
    84.  
    85. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    86.     Call Unhook
    87. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •