Results 1 to 13 of 13

Thread: [RESOLVED] Global Mouse Hook

  1. #1

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Resolved [RESOLVED] Global Mouse Hook

    Ok ive been searching for 2 days now, .paul. that demo link you sent me i couldn't figure which bit went where so i gave up. All i need is a hook for when the left and right mouse button is clicked. AHhhhhhhhhhhhhhhhhhhhh please someone help me this is driving me insane, Im trying to find the shortest possible way. Thanks in advance.

  2. #2
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Global Mouse Hook

    If it is a form app... just handle the form's mouseclick event:

    vb Code:
    1. Public Sub Form1_MouseClick(sender as object, e as mouseeventargs)
    2.  
    3. if e.Button = Windows.Forms.MouseButtons.Left then
    4.  
    5. else if e.Button = Windows.Forms.MouseButtons.Right then
    6.  
    7.  
    8. end if
    9.  
    10. End Sub
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  3. #3

  4. #4
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Global Mouse Hook

    Oh well in that case:

    vb Code:
    1. Public Class Form1
    2.  
    3. Public Declare Function GetAsyncKeyState Lib "user32.dll" (ByVal vKey As Int32) As UShort
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.  
    7.  
    8.         t = New Threading.Thread(New Threading.ThreadStart(AddressOf threadfunc))
    9.         t.Start()
    10.  
    11.  
    12.     End Sub
    13.  
    14.     Public Sub threadfunc()
    15.         While running
    16.             Dim pressed As Boolean = KeyStatus(Keys.LButton)
    17.             If pressed Then
    18.                 MessageBox.Show("L mouse button pressed")
    19.             End If
    20.         End While
    21.     End Sub
    22.  
    23.     Public Shared ReadOnly Property KeyStatus(ByVal Key As Keys) As Boolean
    24.         Get
    25.             If Key = Keys.LButton AndAlso My.Computer.Mouse.ButtonsSwapped Then
    26.                 Key = Keys.RButton
    27.                 MessageBox.Show("right button pushed")
    28.             ElseIf Key = Keys.RButton AndAlso My.Computer.Mouse.ButtonsSwapped Then
    29.                 Key = Keys.LButton
    30.                 MessageBox.Show("Lbutton button pushed")
    31.             End If
    32.             Return GetAsyncKeyState(Key) And &H8000US
    33.         End Get
    34.     End Property
    35.  
    36.     Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
    37.         running = False
    38.         t.Join()
    39.     End Sub
    40.  
    41. End Class

    It will messagebox "L mouse button pressed" everytime you click the button and no matter where you click (even outside the form).

    Justin

    Oh and "Global" to one person may mean a totally different thing to another, without futher elaboration of course.
    Last edited by MonkOFox; Jun 29th, 2010 at 01:13 PM.
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  5. #5

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: Global Mouse Hook

    Thankyou for the reply, 1 problem i get an error on the While running part
    Code:
    Public Sub threadfunc()
    
            While running   <----- HERE
    
                Dim pressed As Boolean = KeyStatus(Keys.LButton)
    
                If pressed Then
    
                    MessageBox.Show("L mouse button pressed")
                End If
    
            End While
    
        End Sub
    The error is: running is not declared

  6. #6

  7. #7
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Global Mouse Hook

    No problem, just define the running variable at the top of the class:

    vb Code:
    1. Public Class Form1
    2.  
    3. Private running as Boolean = true
    4.  
    5. ...

    Just a variable I made up so I can make the thread end when the app/form is closed.

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  8. #8

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: Global Mouse Hook

    lol im stupid haha, also a new problem i didn't see before
    t is not declared
    What do i need to declare that as at the top because i know its not a boolean

  9. #9

    Thread Starter
    Fanatic Member Emcrank's Avatar
    Join Date
    Jan 2009
    Posts
    566

    Re: Global Mouse Hook

    Ok done that, declared it as a Threading.Thread
    It notices when i click the left mouse button but doesn't do anythin when i click the right mouse button

  10. #10

  11. #11
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Global Mouse Hook

    The thread I made keeps checking for just the LButton Click.

    Just declare and create a new thread and assign it to a new function that does the following:

    vb Code:
    1. Public Sub threadfunc2()
    2.         While running
    3.             Dim pressed As Boolean = KeyStatus(Keys.RButton)
    4.             If pressed Then
    5.                 MessageBox.Show("R mouse button pressed")
    6.             End If
    7.         End While
    8.     End Sub

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  12. #12
    Frenzied Member
    Join Date
    Feb 2008
    Location
    Texas
    Posts
    1,288

    Re: Global Mouse Hook

    Awesome, glad it worked out : )

    Justin
    You down with OOP? Yeah you know me!
    MCAD and MCMICKEYMOUSE (vb.net)

    ----

    If it even kinda helps... rate it : )

    Edit a Multi-page .tif file and save.

  13. #13

Posting Permissions

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



Click Here to Expand Forum to Full Width