We’re working on a custom tool that already has a primary action bound to left-click and now we decided to bind a secondary action to the right-click button being pressed (instead of showing the context menu).
According to the API documentation, we should use a combination of getMenu/onRButtonDown/Up to disable the default context menu and react to the event. However, we are not able to make this work.
The context menu still shows up
If getMenu is defined but empty, the default menu appears (from the API doc, we expected it not to)
if getMenu adds some dummy items to the menu, a custom menu does show up (which shows that the method is called)
onRButtonDown/onRButtonUp events are not called
but this might be due to the menu showing up?
What’s a bit worrying is that it works fine with SketchUp 2017 but it does not with recent versions (we tried SU 2020 and 2021.1, on Windows, on two computers).
You can paste this in the Ruby console to reproduce the issue:
class TestTool
def onLButtonDown(_flags, _x, _y, view)
puts 'LEFT DOWN'
end
def onLButtonUp(_flags, _x, _y, view)
puts 'LEFT UP'
end
def onRButtonDown(_flags, _x, _y, view)
puts 'RIGHT DOWN'
# Doesn't get called
end
def onRButtonUp(_flags, _x, _y, view)
puts 'RIGHT UP'
# Doesn't get called
end
def getMenu(menu, flags, x, y, view)
# Implemented to prevent a context menu appearing on right click
# ... But the default menu still shows up
# We also tried both signatures, just in case:
# - getMenu(menu)
# - getMenu(menu, flags, x, y, view)
# but it doesn't change the issue
end
end
Sketchup.active_model.tools.push_tool(TestTool.new)
Are we using the API in an appropriate manner?
Thanks a lot for any help, we’re a bit stuck right now
I don’t have time to look this up until tomorrow, but some events will use the return value to determine if it will propagate back to SketchUp (triggering default action). I don’t recall if that’s only the key events, or also the mouse events.
According to the documentation it is only for a key events.
for the #onKeyUp or the #onKeyDown Return true to prevent SketchUp from processing the event.
But it is interesting that in SU2017 and SU2019 the above snippets is works (returning ‘RIGHT UP’ and ‘RIGHT DOWN’) But on SU2020.2 and above is not.
I’ve tried return true in onRButtonUp and onRButtonDown even in getMenu method without success.
@tt_su At this point, do you think that this is a SketchUp bug or an issue in our code?
It would be quite surprising that nobody ever noticed the issue if this right-click API just never worked, so I’m still leaning towards our code being wrong!