?
Solved

Actually checking mouse state

Posted on 2003-03-05
5
Medium Priority
?
157 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month8 days, 20 hours left to enroll

765 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