Opening a PopupMenu correctly on MacOS

Mac OSX specific forum
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Opening a PopupMenu correctly on MacOS

Post by Shardik »

Andre wrote:Is there any possibility on MacOS to recognize a right-mouseclick in an empty (!) window, means without adding (dummy) imagegadgets or similar?

Code: Select all

#kEventClassMouse = 'mous'
#kEventMouseButtonPrimary = 1
#kEventMouseButtonSecondary = 2
#kEventMouseButtonTertiary = 3
#kEventMouseDown = 1
#kEventParamKeyModifiers = 'kmod'
#kEventParamMouseButton = 'mbtn'

#typeMouseButton = 'mbtn'
#typeUInt32 = 'magn'

ImportC ""
  GetEventClass(Event)
EndImport

Structure EventTypeSpec
  EventClass.L
  EventKind.L
EndStructure

Procedure EventHandler(*NextEventHandler, Event, UserData)
  Select GetEventClass(Event)
    Case #kEventClassMouse
      Select GetEventKind_(Event)
        Case #kEventMouseDown
          If GetEventParameter_(Event, #kEventParamMouseButton, #typeMouseButton, 0, 4, 0, @ButtonType) = 0
            If GetEventParameter_(Event, #kEventParamKeyModifiers, #typeUInt32, 0, 4, 0, @KeyModifier) = 0
              Select ButtonType
                Case #kEventMouseButtonPrimary
                  If KeyModifier = $00001000
                    Debug "Right mouse button"
                  Else
                    Debug "Left mouse button"
                  EndIf
                Case #kEventMouseButtonSecondary
                  Debug "Right mouse button"
                Case #kEventMouseButtonTertiary
                  Debug "Middle mouse button"
              EndSelect
            EndIf
          EndIf
      EndSelect
  EndSelect

  If *NextEventHandler
    CallNextEventHandler_(*NextEventHandler, Event)
  EndIf
EndProcedure

OpenWindow(0, 100, 100, 400, 200, "Detect left, middle and right mouse button click")

EventHandlerUPP = NewEventHandlerUPP_(@EventHandler())
EventCount = 1
Dim EventTypes.EventTypeSpec(EventCount - 1)
EventTypes(0)\EventClass = #kEventClassMouse
EventTypes(0)\EventKind = #kEventMouseDown
InstallEventHandler_(GetWindowEventTarget_(WindowID(0)), EventHandlerUPP, EventCount, @EventTypes(), UserData, @EventHandlerRef)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Last edited by Shardik on Thu Nov 25, 2010 8:52 pm, edited 1 time in total.
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2058
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Opening a PopupMenu correctly on MacOS

Post by Andre »

Thanks Shardik, that works! :D

Even if it would be better, if that event handling would native in PB... :twisted:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Opening a PopupMenu correctly on MacOS

Post by WilliamL »

Odd, if I use my trackball with a right button Shardik's code works but if I use the 'ctrl'+(mousepad) button I get a left-click. In other words, if I use my MBP without an external mouse and try to right click.. I get only left-clicks. This limitation may be a problem for some users.
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Opening a PopupMenu correctly on MacOS

Post by Shardik »

WilliamL wrote:Odd, if I use my trackball with a right button Shardik's code works but if I use the 'ctrl'+(mousepad) button I get a left-click.
You are right William, I didn't take care in my code example for using a left click in
combination with the Ctrl key as a right mouse click (presumably for those Apple users
with a one button mouse) although I had already read a posting from David McLeod in
the apple mailing list where he presented C code which demonstrates how to analyse
modifier keys and mouse clicks.

I have therefore updated my above code example. You are now free to use the Ctrl or Alt
or Cmd key together with the left mouse button to produce a right click... :wink:

I have tested my above example with an iMac and Mac OS X 10.6.5, a mighty mouse and
an USB Wheel mouse on which pressing the wheel down is correctly interpreted as a middle
button click.
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Opening a PopupMenu correctly on MacOS

Post by WilliamL »

Thanks Shardik,

Works perfectly on my MBP in 10.5.8. :D

(added to API list)
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
Post Reply