Access the mouseover of a desktopmenuitem?

I have a contextual menu where I’d like to access the mouseover events of the menu items. I can recreate the whole menu system with canvases, but before I recreate the wheel I’m curious if it’s possible through the standard menu item.

You could monitor System.MouseX and System.MouseY in a timer, and trigger a MouseOver method when the cursor is over the menu item.

I wasn’t sure how to use the system coordinates to tie them to a menu item so I poked around and found the documentation for NSMenuItem and used a declare to that to make an extends method for desktopmenuitem:

Public Function is_highlighted(extends d as desktopmenuitem) As Boolean
  declare function isHighlighted lib "Cocoa" selector "isHighlighted" (obj_id as Ptr) as Boolean
  
  return isHighlighted(d.Handle)
End Function

I loop through an array of current menu items to see if any are highlighted. Works well.

1 Like

Nice tip, thanks for sharing. If I may make a suggestion to help you document this API, also adopting a system like this will help out tremendously in the future. Take it from someone with over 2 decades of experience with Xojo.

declare function NSMenuitem_isHighlighted lib "AppKit" selector "isHighlighted" ( NSMenuItemInstance as Ptr ) as Boolean // --- OSX 10.5 + setter = "highlighted".

What you do here is to include the classname which responds to the API, you include the framework of the API, it includes the propertyType in the propertyName (and if it requires an instance or class) and lastly, you include the minimum OS version this API requires and what the setter name is for future reference.

The more you use declares, the more you’re going to run into situations where Apple deprecate API, a specific API becomes buggy or worse just crashes the Mac. By keeping this reference it helps you to look up the specific Apple documentation (there’s an MVP who refuses to do this and has asked me for help multiple times, because they read the wrong documentation), it helps you to solve some simple problems, like passing a instance when it needs the class and also to make sure it’s compatible.

Over time, you’ll end up having to check OS versions and use different API for different versions of the macOS. I have one app that uses different API for almost all of the OS versions it supports, and it seems like there’s some edge cases on Ventura where it may need to use alternatives also.

1 Like

That’s a nice tip as well. I actually do that when naming objects. If I don’t remember the name of a bevel button I can at least start typing “bb_” and let autocomplete help me find it.

1 Like