Cannot use right-click event handlers in custom tool

Hi all,

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'

  def onLButtonUp(_flags, _x, _y, view)
    puts 'LEFT UP'

  def onRButtonDown(_flags, _x, _y, view)
    puts 'RIGHT DOWN'
    # Doesn't get called

  def onRButtonUp(_flags, _x, _y, view)
    puts 'RIGHT UP'
    # Doesn't get called
  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

Are we using the API in an appropriate manner?
Thanks a lot for any help, we’re a bit stuck right now :slight_smile:

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.

I guess it is a bug.

1 Like

I also tried to return different combinations of truthy/falsy/nil values from those event handlers but it doesn’t seem to change the outcome.

Actually I think it doesn’t work in SU2017 either!

I just noticed that in recent SU versions, right click always creates a context menu but in SU2017 there is no menu if nothing is selected.

So if an entity is already selected in SU2017 and we right click on it with this TestTool active, then the same issue occurs (no “RIGHT UP/DOWN” log).

Probably yes. The context handler might be stopping further event propagation.

@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!

I’m not sure. It could relate to platform behaviour, or be due to SketchUp’s own logic. I’ve not had time to dig into it.

1 Like

If behavior is contrary to the documentation, then please open an issue in the API issue tracker.

Thomas can assign labels to the issue later as time permits.

@DanRathbun Issue opened, thanks for the suggestion.

1 Like

Thanx. Just for common information …

In documentation, Ruby instance methods are conventionally referred to with a # prefix, and class methods with the :: scope operator.

As getMenu is an instance callback method it should be referred to as Tool#getMenu().

This is only for documentation purposes. ie, Sketchup::Tool is an abstract class that does not really exist.