well, basicly i just want to detect when the left mouse button goes from being DOWN (clicked) to UP(unclicked). and this will be hapening externaly, like, i may be clicking Freecell, not the VB form. I alredy have some code

Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer
Private Const VK_LBUTTON = &H1
Private Const VK_RBUTTON = &H2


If GetAsyncKeyState(VK_LBUTTON ) Then

but thse only detect left/right clicks, the down event, not the up

Thanks!

Dani AI

Generated

Summary and clear options (short): this is a system‑wide left-button release problem, not a form-local MouseUp. already tried GetAsyncKeyState and saw only the press; and pointed out MouseUp (works only inside the form); was right that hooks are an option but come with risks.

Practical, low-risk solution: poll the button state and detect the transition from down => up. GetAsyncKeyState’s high-order bit (0x8000) says “currently down”; keep the previous sample and trigger when previous was down and current is not. This works when the app is out of focus, is simple to implement in VB6, and avoids the complexity and stability pitfalls of global hooks.

Example (VB6-style logic — place a Timer on the form, interval ~20–50 ms):

' Timer1 Interval = 30
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Const VK_LBUTTON = &H1
Private prevDown As Boolean

Private Sub Form_Load()
    prevDown = (GetAsyncKeyState(VK_LBUTTON) And &H8000) <> 0
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Dim currDown As Boolean
    currDown = (GetAsyncKeyState(VK_LBUTTON) And &H8000) <> 0
    If prevDown And Not currDown Then
        ' Left button was released (system-wide)
        Debug.Print "Left button UP at " & Now
    End If
    prevDown = currDown
End Sub

Notes, caveats and alternatives: increase polling rate for very short clicks (cost vs. responsiveness tradeoff). For lower latency or if polling is unacceptable, a low‑level mouse hook (WH_MOUSE_LL via SetWindowsHookEx) gives immediate callbacks, but it’s more complex, needs correct unhooking, and can be fragile in native VB6 if implemented incorrectly. For most cases the timer + previous-state method is simplest and reliable.

Recommended Answers

All 7 Replies

Have u try ?

If GetAsyncKeyState(VK_LBUTTON ) Then
    If GetAsyncKeyState(VK_RBUTTON ) = 0 Then
..
    End If
End If

Hi friend try this code.
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
Form1.BackColor = RGB(111, 222, 225)
Else
Form1.BackColor = &H8000000F
End If
End Sub

Private Sub Timer1_Timer()
Form1.BackColor = &H8000000F
MsgBox "You released the left button"
End Sub

those all work to detect if the left buton has been pushed down, not the relese of it though. i've searched for days to no avail. so it may be imposible. thaks for helping though

choose the MouseUp event

Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MsgBox "Hello World"
End Sub

yes, but with that it is restricted to detecting mouse activity within the form, i want to detect mouse activity in general, like Freecell may be selected. not the vb form. Thanks though

If u want to receive the event even if ur application is not in focus, You may try the Hooking Concept (Mouse Hooking, Keyboard Hooking) .... But it is dangerous, if u r not careful. It can crash ur application..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.