Preventing editor deselect on MacOS

I’m currently working on a tile-map custom editor, which renders a colour-coded representation of the finished product in the scene view. The editor is working perfectly on my work computer, but as soon as I brought it home in the afternoon one very important function stopped working.

The main interface in the editor is selecting a ‘tiletype’ from a palette in the inspector, and ‘drawing’ with the mouse on the grid drawn in the 3D view. The problem is, on the Mac, there seems to be no way to interrupt the default ‘deselect’ behaviour in the 3D view! So, as soon as you release or drag the mouse, it deselects the current object, and disables the custom editor.

On windows, it was possible to ‘eat’ the deselect commands with this bit of code (inside OnSceneGUI)-

if((Event.current.type == EventType.MouseDrag || Event.current.type == EventType.MouseDown) && !Event.current.alt)
{
    Event.current.Use();
}

This allowed me to rotate the view using ‘alt-click’, but prevented it from deselecting my objects.

However, on my home computer, it doesn’t work! Even using just the raw line

Event.current.Use();

to attempt to swallow every single event doesn’t stop the selection behaviour. I’ve tried reassigning Event.current, like this-

Event.current.type = EventType.Ignore;

to disable the event, but even that doesn’t work.

At this point I’ve decided to work on a different part of the project and not worry about it- after all, it’s only an editor, and if I can make all of my maps at work it doesn’t matter if it’s not functional at home. However, the idea of leaving something like this without understanding how to fix it, or even what’s going wrong, just seems reprehensible to me.

If anyone else has experience writing complicated custom editors like this, could you please give me some suggestions as to any possible workarounds or fixes for this? Remember, the problem seems only to occur in the MacOS editor- it doesn’t happen at all on Windows.

You surely got the answer now, but if you’re still looking for it, here’s the trick : you have to get a control ID for your Custom Editor, then set the default control as this one, see the example below.
Be sure that you do that only when the Event is used by your script, otherwise it prevent any action in the scene view (for ex. the scene view gizmo).

void OnSceneGUI() {
  int controlID = GUIUtility.GetControlID (FocusType.Passive);
  // ... gui stuff
  if (Event.current.type == EventType.Layout) {
    HandleUtility.AddDefaultControl (controlID);
  }
}