Tip: Check for single/double click on one gadget

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Tip: Check for single/double click on one gadget

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by PB.

This is a bit messy, but works. Only downside is that the single click takes a
moment to react, but hey, that's a small price to pay for achieving the goal.

Code: Select all

If OpenWindow(0, 200, 200, 200, 100, "Test", #PB_Window_SystemMenu)
  ButtonGadget(1, 50, 20, 80, 50, "click me!")
  Repeat
    ev = WaitWindowEvent()
    If ev = #PB_Event_Gadget
      start = GetTickCount_()   ;Note time of first click.
      GetAsyncKeyState_(#VK_LBUTTON)   ;Clear left mouse buffer.
      Repeat : Until GetAsyncKeyState_(#VK_LBUTTON) <> 0 Or
                     GetTickCount_() - start > GetDoubleClickTime_()
      If GetTickCount_() - start > GetDoubleClickTime_()   ;Timed out!
        MessageRequester("info", "single click")
      Else
        MessageRequester("info", "double click")
      EndIf
    EndIf
  Until ev = #PB_Event_CloseWindow
EndIf
PB - Registered PureBasic Coder