How can I find out When and Where the Mouse Button is clicked Anywhere on the Screen, Reguardless of what is Under the Mouse Pointer?
Printable View
How can I find out When and Where the Mouse Button is clicked Anywhere on the Screen, Reguardless of what is Under the Mouse Pointer?
Hi,
Just a small clarification...
When u say "screen", do u mean the "form" or the screen which includes the titlebar and the control buttons?
Bye & regards
Ravi.
I mean the whole Screen, The form takes up the whole screen, there is no Title Bar or anything, just the form.
But I dont want to use a Form_click b/c I want to know when they click anywhere.
Bump.
Here is one way.
If you after other SubClassing examples, do a search using KeyWords: mouse hooks
Bruce.
Or if you dont want to screw with hooks and having to dowload extra DLLs and stuff you could do it with this code and these APIs.
This Code would go in a form with 2 labels and a timer named Label1, Label2 and Timer1
VB Code:
Private Sub Timer1_Timer() GetCursorPos mPoint Label1.Caption = "X: " & mPoint.x & " Y: " & mPoint.y If GetAsyncKeyState(1) Then Label2.Caption = "Left Mouse Button Down" Else Label2.Caption = "" End If End Sub
This code would go in a module
VB Code:
Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer Public Type POINTAPI x As Long y As Long End Type Global mPoint As POINTAPI
This would return the x and y based on screen coordinates and the getasynckeystate(1) is watching the left mouse button.. you would need a (2) and an (8) for right and middle.
The two faults to this code.. one clicking on window controls (minimize maximize and exit) are not registered.. but if your fullscreen without a titlebar you dont need to worry about that. The Second fault is you have a timer executing constantly whlie not a big issue with this small of code if you had a large amount of code in the timer it could cause perfomance issues.
i don't mean to sound stupid.. if i do.. but isn't that a hook?
No it isn't a hook, although it does the same thing as the hooking code that Bruce Fox linked to.
Hooking is telling Windows to send the messages to your code before it sends them on to the proper place. This gives you the chance to alter the messages (eg: when the left mouse button is pressed you could tell the target program that the right mouse button was pressed, or that no button was pressed).
StevenHickersons code just repeatedly checks the current state - which may or may not capture all of the events (depending on the timer interval and how busy the computer is). Hooking is guaranteed to get all events, but is a little harder to work with.