?
Solved

Actually checking mouse state

Posted on 2003-03-05
5
Medium Priority
?
160 Views
Last Modified: 2010-05-01
Hi,
How can I check if the right or left mouse button is down. Even if the button is not clicked within the application.

Bye
0
Comment
Question by:adam8
5 Comments
 

Expert Comment

by:deadlyfluvirus
ID: 8072551
Use this code to set a True/False (BOOLEAN) if the mouse is down or not...You need to do this for each control that you want to check, but my code will check to see if the mouse is down in the form area...

Dim booLeftButtonDown As Boolean
Dim booRightButtonDown As Boolean


Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = vbLeftButton Then
        booLeftButtonDown = True
        booRightButtonDown = False
    ElseIf Button = vbRightButton Then
        booLeftButtonDown = False
        booRightButtonDown = True
    End If
End Sub

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        If Button = vbLeftButton Then
        booLeftButtonDown = False
        booRightButtonDown = False
    ElseIf Button = vbRightButton Then
        booLeftButtonDown = False
        booRightButtonDown = False
    End If
End Sub

That will set those boolean vars to True/False depending if the mouse was clicked down on the form and which button it was...

Thanks,
Christian
0
 

Accepted Solution

by:
zfiexpert earned 400 total points
ID: 8076159

To see if the Button is pressed no matter where you are on the desktop use the API function GetASyncKeyState.

In a module declare the API:

Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer

In your form load sub routine add this:

Form1.Show
Do
If GetAsyncKeyState(VbKeyLButton) Then End
DoEvents
Loop

You can use all the VbKey functions with this as well (including the keyboard), but specifically the mouse functions are:

VbKeyLButton - Left button
VbKeyRButton - Right Button
VbKeyMButton - Middle Button

Also - Instead of using the Do Doevents Loop process you can just put it in a timer, but the loop is much faster.

Hope this helps.

Simplify.

-Shane
ZFi Enterprises
0
 
LVL 1

Author Comment

by:adam8
ID: 8078970
zfiexpert,
this was exactly what I was after. You actually listened to the question, because if the mouse was clicked outside of the form then the code supplied by deadlyfluvirus wouldn't work.

I had no idea that the getasyckeystate worked with the mouse buttons.

You are a legend.
0
 
LVL 1

Author Comment

by:adam8
ID: 8079079
If GetAsyncKeyState(VbKeyLButton) = -32768 then the left mouse button is currently down.
0
 

Expert Comment

by:tfsln
ID: 23924641
How did you declare 'VbKeyLButton'

That is not available... im using VB.NET 2008
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question