|
-
Feb 9th, 2003, 11:09 AM
#1
Thread Starter
Hyperactive Member
How to detect a mouse click???
Hello every1,
i wanna know if there is any API which will let me know if any of the mouse button was pressed. I wanna know which button was pressed and this must be global, i.e., i must be able to press anywhere, my app must report whether a mouse click was detected. Thanx in advance. I also want to know if it was a left or right click.
If Not VB Then Exit
------------------------------------------------
visit me @ http://mzubair.50g.com/
-
Feb 9th, 2003, 02:30 PM
#2
To do this you would need to install a windows hook. If you are using only windows NT/2000/XP then the WH_MOUSE_LL hook is the most appropriate otherwise a WH_JOURNALRECORD hook can be used but you need to skip all the non-mouse messages it recieves.
-
Feb 12th, 2003, 12:50 AM
#3
Addicted Member
The WH_GETMESSAGE hook would work too for any OS seeing
as mouse clicks are posted.
-
Feb 12th, 2003, 01:40 AM
#4
Thread Starter
Hyperactive Member
hey guyz,
u'd b doing me a favor if u posted some code. I dont know much abt message hooking. a sample code will be gr8ly appreciated. thanx
If Not VB Then Exit
------------------------------------------------
visit me @ http://mzubair.50g.com/
-
Feb 12th, 2003, 01:52 AM
#5
Addicted Member
Use the 'search' functionality of the forums to find hooking code.
There are MANY examples.
-
Feb 12th, 2003, 04:35 AM
#6
Frenzied Member
Using the EventVB.dll you could do this thus:-
VB Code:
Option Explicit
Dim WithEvents vbLink As EventVB.APIFunctions
Dim WithEvents vbHook As EventVB.ApiSystemHook
Private Sub Form_Load()
Set vbLink = New APIFunctions
Set vbHook = vbLink.System.Hooks
vbHook.StartHook WH_JOURNALRECORD, HOOK_GLOBAL
End Sub
Private Sub vbHook_JournalMouseMessage(ByVal msg As EventVB.WindowMessages, ByVal x As Long, ByVal y As Long, ByVal time As Long, ByVal TargetWindow As EventVB.ApiWindow)
If msg = WM_LBUTTONUP Then
If Not TargetWindow Is Nothing Then
'\\ Click was over a window...is it a button?
If Not TargetWindow.Button Is Nothing Then
Debug.Print TargetWindow.WindowText & " button pressed"
End If
End If
End If
End Sub
-
Feb 13th, 2003, 02:27 PM
#7
Hyperactive Member
Or if you don't have the vb event.dll..
VB Code:
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Const VK_LBUTTON = &H1
Private Const VK_RBUTTON = &H2
'
' following is only needed if you want to identify the window
' in which the mouse is being clicked
'
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As Long, ByVal yPoint As Long) As Long
Private Sub Timer1_Timer()
Static lastvalL%, lastvalR%
Dim thisvalL%, thisvalR%
Dim cpoint As POINTAPI
thisvalL = GetAsyncKeyState(VK_LBUTTON)
If thisvalL <> lastvalL Then
lastvalL = thisvalL
If thisvalL <> 0 Then
GetCursorPos cpoint
List1.AddItem "Left Click - " & Hex(WindowFromPoint(cpoint.X, cpoint.Y))
End If
End If
thisvalR = GetAsyncKeyState(VK_RBUTTON)
If thisvalR <> lastvalR Then
lastvalR = thisvalR
If thisvalR <> 0 Then
GetCursorPos cpoint
List1.AddItem "Right Click - " & Hex(WindowFromPoint(cpoint.X, cpoint.Y))
End If
End If
End Sub
-
Feb 14th, 2003, 04:05 AM
#8
Frenzied Member
In general polling the mouse key state with a timer is not a great idea as it will only fire if the state changed when the timer fires - if the mouse is clicked down then up before the timer fires this event will be missed.
If you don't want to use the dll (which is free and includes full source) I recommend you read up MSDN on installing a WH_JOURNALRECORD hook...
-
Feb 14th, 2003, 08:44 AM
#9
Hyperactive Member
Really? I couldn't find that dll for free with full source code. Where did you get it from?
-
Feb 14th, 2003, 09:14 AM
#10
Frenzied Member
Last edited by MerrionComputin; Mar 29th, 2003 at 09:35 AM.
-
Feb 14th, 2003, 09:27 AM
#11
Hyperactive Member
Thank You Very Much!!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|