Detecting right-clicks on ButtonGadgets

Share your advanced PureBasic knowledge/code with the community.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Detecting right-clicks on ButtonGadgets

Post 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)
Last edited by PB on Sat Mar 18, 2006 1:20 am, edited 6 times in total.
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Neat. Thanks!
@}--`--,-- A rose by any other name ..
DominiqueB
Enthusiast
Enthusiast
Posts: 103
Joined: Fri Apr 25, 2003 4:00 pm
Location: France

Great

Post 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
Dominique

Windows 10 64bits. Pure basic 32bits
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Great

Post 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?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post 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!
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
DominiqueB
Enthusiast
Enthusiast
Posts: 103
Joined: Fri Apr 25, 2003 4:00 pm
Location: France

Oh, i see !

Post 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
Dominique

Windows 10 64bits. Pure basic 32bits
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Oh, i see !

Post 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")
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Detecting right-clicks on ButtonGadgets

Post by NoahPhense »

Nice api ..


- np
User avatar
DoubleDutch
Addict
Addict
Posts: 3219
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post 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
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
paleozord
User
User
Posts: 12
Joined: Wed Mar 15, 2006 8:30 pm

Post 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.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Post by Joakim Christiansen »

:(
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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 
Last edited by netmaestro on Sat Mar 18, 2006 12:14 am, edited 1 time in total.
BERESHEIT
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

@netmaestro: Not so good -- If I LEFT click a button, it generates a RIGHT click event. ;)
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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 
BERESHEIT
Post Reply