Detecting Mouse-Clicks Globally

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


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"
        Label2.Caption = ""
    End If
End Sub

Download Zipped Project (3k)


  • how to get navation name or links of mouse position/focus?

    Posted by janmark on 10/02/2015 06:17pm

    hi experts: i just want to ask questions. i want to know how to get the links or navigation name of my mouse position and printed it on textbox1 in Visual basic if i position my mouse arrow to facebook navagation bar then like this textbox1.text = "" i really appreciate if you can help me... thanks

  • Delete this post

    Posted by Patty Programmer on 09/04/2015 02:04pm

    *PLEASE* don't use awful code that polls 100000s of times just to "check for input". Ugh. Endless timer-loops are NOT the answer.

  • In C

    Posted by Enrique on 04/18/2015 09:28pm

    How could you do this same but in C?

  • 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?

  • Mouse Co-Ordinates

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

    Originally posted by: Ram


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


  • 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.

  • 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!

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

Top White Papers and Webcasts

  • Anthony Christie, the Chief Marketing Officer for Level Communications, is responsible for customer experience, worldwide marketing and product management. In this informative asset, he shares his insights into why a private network connection to cloud-bases applications is the right decision for your enterprise. Download now to find out more.

  • Moving from an on-premises environment to Office 365 does not remove the need to plan for disruptions or reduce the business risk requirements for protecting email services. If anything, some risks increase with a move to the cloud. Read how to ease the transition every business faces if considering or already migrating to cloud email. This white paper discusses: Setting expectations when migrating to Office 365 Understanding the implications of relying solely on Exchange Online security Necessary archiving …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date