Detecting Mouse-Clicks Globally

This code sample shows how to detect a mouse click anywhere on the desktop and on any window.

screen-shot

The code works by utilising the GetAsyncKeyState API call that can check the status of keyboard and mouse input at any point in a programs processing.

A form is used with a timer that fires every 10th of a second to check for the VK_LBUTTON and VK_RBUTTON keystates (the left and right mouse buttons).


option Explicit

private Declare Function GetAsyncKeyState Lib "user32" _
        (byval vKey as Long) as Integer
private Const VK_LBUTTON = &H1
private Const VK_RBUTTON = &H2

private Sub Form_Load()
    Timer1.Interval = 100
End Sub

private Sub Timer1_Timer()
    If GetAsyncKeyState(VK_LBUTTON) then
        Label2.Caption = "Left Click"
    ElseIf GetAsyncKeyState(VK_RBUTTON) then
        Label2.Caption = "Right Click"
    else
        Label2.Caption = ""
    End If
End Sub
'

Download Zipped Project (3k)



Comments

  • Stopping the fired event

    Posted by Legacy on 09/14/2003 12:00am

    Originally posted by: Gil Groman


    Great Utility!

    I would like to add to it the capability to stop the mouse click event.
    For example when clicking on the task bar the task button will not restore an application if my VB program will not
    allow it.
    And what about the Title bar? could it be supported too?

    Reply
  • Mouse Co-Ordinates

    Posted by Legacy on 08/23/2003 12:00am

    Originally posted by: Ram


    Hi,

    Good work1.But how to get the mouse coordinates when the user clicks on the window in this case(VK_LBUTTON)

    Thnks

    Reply
  • Keyboard status?

    Posted by Legacy on 07/10/2003 12:00am

    Originally posted by: Phil Johnstone

    You mentioned in your tutorial that 'GetAsyncKeyState API call that can check the status of keyboard and mouse input' I was just wondering how you get the status of the keyboard using that API. Thanks in advance.

    Reply
  • Awesome stuff

    Posted by Legacy on 01/08/2003 12:00am

    Originally posted by: Anthony Gill

    Thanks for the code! I was dreading the task of digging through my Win32 API tome. :) Works perfect!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds