Page 1 of 2

Detecting right-clicks on ButtonGadgets

Posted: Sat Feb 04, 2006 1:01 pm
by PB
Version 4.00 removed the AdvancedGadgetEvents() command, so to detect a
right-click on a ButtonGadget now you can use the code below (which should
also work for any gadget, actually).

But don't forget that most gadgets, after an event, can trigger an EventType()
of #PB_EventType_RightClick, so this code is really only useful for Buttons. :)

Code: Select all

; Original code by Danilo.
; Modified by PB, MLK and Trond.

If OpenWindow(0,200,200,200,200,"ButtonGadget Right-click",#PB_Window_SystemMenu)
  CreateGadgetList(WindowID(0))
  ButtonGadget(0,50,50,100,100,"Right-click me!")
  Repeat
    Select WaitWindowEvent()
      Case #WM_RBUTTONDOWN
        GetWindowRect_(GadgetID(0),@rect.RECT) : GetCursorPos_(@pt.POINT)
        If PtInRect_(@rect,pt\x,pt\y)
          Repeat : Sleep_(1) : Until GetAsyncKeyState_(#VK_RBUTTON)=0
          GetCursorPos_(@pt.POINT)
          If PtInRect_(@rect,pt\x,pt\y) : Debug "Right-click" : EndIf
        EndIf
      Case #PB_Event_CloseWindow
        End
    EndSelect
  ForEver
EndIf
(Edited again due to OpenWindow changing parameter order)

Posted: Sat Feb 04, 2006 1:47 pm
by Dare2
Neat. Thanks!

Great

Posted: Sat Feb 04, 2006 5:32 pm
by DominiqueB
Thank's for the code, but i see one can only click one time on the button !
No other click is detected . . .

Dominique

Re: Great

Posted: Sun Feb 05, 2006 1:08 am
by PB
> i see one can only click one time on the button

Weird! Works fine for me with 98se, Win2K and WinXP SP2. I can't see
anything in the code that would cause it to only work once. The program
flow is quite easy to follow, and logical. Maybe it's a SP1 issue? Dunno.
Are you using a clean v4.00 Beta 1 installation of PureBasic?

Posted: Sun Feb 05, 2006 12:10 pm
by Trond
That's not a right-click. That's a right release. This means that you don't have to CLICK (as in a right-click), it's enough to release the rmb over the button. So you can press the rmb outside the button and release the rmb over the button.

Posted: Sun Feb 05, 2006 1:13 pm
by PB
> That's not a right-click. That's a right release. This means that you don't
> have to CLICK (as in a right-click), it's enough to release the rmb over the
> button.

Good point, Trond -- I've edited the example to prevent that. ;)

@MLK: I also edited in your corrections, and deleted your post since it's now
incorporated into the tip. Hope you don't mind!

Oh, i see !

Posted: Sun Feb 05, 2006 8:15 pm
by DominiqueB
Well, if i click once on the button then the debugger window appears.
If i close the debugger, then click again on the button . . nothing happens !

Effectively, if i let the debugger window open i can see on each click on the button a new line in the debugger telling the click as been detected.

Is it normal or not, i don't now ?

Dominique

Re: Oh, i see !

Posted: Sun Feb 05, 2006 9:05 pm
by PB
> If i close the debugger, then click again on the button . . nothing happens !

That's because you chose to CLOSE the debugger! :roll: Not a bug at all.
Replace both "Debug" lines in the code with the following and you'll see:

Code: Select all

MessageRequester("test","click")

Re: Detecting right-clicks on ButtonGadgets

Posted: Thu Feb 09, 2006 2:24 am
by NoahPhense
Nice api ..


- np

Posted: Thu Feb 09, 2006 4:23 pm
by DoubleDutch
I don't understand why buttons can't always have the #PB_EventType_RightClick enabled too?

This would be far more consistant with the other gadgets.

Fred, is there a reason for this?

If its because the Mac only has one button, most people I know who have Macs go out and buy a two button mouse straight away (the Mac OS supports 2 buttons).

-Anthony

Posted: Fri Mar 17, 2006 10:43 pm
by paleozord
Why was the AvbancedGadgetEvents feature removed? Now you have to use a Windows-specific API call to detect a right click, which will not work under Linux.

Before you could just enable Advanced and use #PB_EventType_RightClick which would work for both Windows and Linux. Seems like a step backwards to me.

Posted: Fri Mar 17, 2006 11:36 pm
by Joakim Christiansen
:(

Posted: Fri Mar 17, 2006 11:50 pm
by netmaestro
[edit] There's a goof in this one, check post after next for correct code

Code: Select all

Procedure ButtonCallback(hWnd, Message, wParam, lParam)
  Select message
    Case #WM_PARENTNOTIFY 
      Select ChildWindowFromPoint_(WindowID(0),WindowMouseX(0),WindowMouseY(0))
        Case GadgetID(0) 
          Debug "Right button down on button 0"
        Case GadgetID(1)
          Debug "Right button down on button 1"
      EndSelect
    Case #WM_CONTEXTMENU 
      Select ChildWindowFromPoint_(WindowID(0),WindowMouseX(0),WindowMouseY(0))
        Case GadgetID(0) 
          Debug "Right button up on button 0"
        Case GadgetID(1)
          Debug "Right button up on button 1"
      EndSelect
  EndSelect
  Result = #PB_ProcessPureBasicEvents 
  ProcedureReturn Result 
EndProcedure 

OpenWindow(0,0,0,400,300,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
CreateGadgetList(WindowID(0)) 
ButtonGadget(0,120,80,160,46," Button 0") 
ButtonGadget(1,120,140,160,46," Button 1") 
SetWindowCallback(@ButtonCallback()) 
Repeat    
  ev=WaitWindowEvent() 
   If ev=#PB_Event_Gadget 
      ; Do something useful 
  EndIf 
Until ev=#PB_Event_CloseWindow  
End 

Posted: Sat Mar 18, 2006 12:05 am
by PB
@netmaestro: Not so good -- If I LEFT click a button, it generates a RIGHT click event. ;)

Posted: Sat Mar 18, 2006 12:10 am
by netmaestro
Sorry! I forgot a step :oops:

Code: Select all

Procedure ButtonCallback(hWnd, Message, wParam, lParam) 
  Select message 
    Case #WM_PARENTNOTIFY 
      If GetAsyncKeyState_(#VK_RBUTTON)
        Select ChildWindowFromPoint_(WindowID(0),WindowMouseX(0),WindowMouseY(0)) 
        Case GadgetID(0) 
          Debug "Right button down on button 0" 
        Case GadgetID(1) 
          Debug "Right button down on button 1" 
        EndSelect 
      EndIf  
    Case #WM_CONTEXTMENU 
      Select ChildWindowFromPoint_(WindowID(0),WindowMouseX(0),WindowMouseY(0)) 
        Case GadgetID(0) 
          Debug "Right button up on button 0" 
        Case GadgetID(1) 
          Debug "Right button up on button 1" 
      EndSelect 
  EndSelect 
  Result = #PB_ProcessPureBasicEvents 
  ProcedureReturn Result 
EndProcedure 

OpenWindow(0,0,0,400,300,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered) 
CreateGadgetList(WindowID(0)) 
ButtonGadget(0,120,80,160,46," Button 0") 
ButtonGadget(1,120,140,160,46," Button 1") 
SetWindowCallback(@ButtonCallback()) 
Repeat    
  ev=WaitWindowEvent() 
   If ev=#PB_Event_Gadget 
      ; Do something useful 
  EndIf 
Until ev=#PB_Event_CloseWindow  
End