Stop Scene from reading Ctrl + Z / Ctrl + Y if my Editor Window has focus?

Is it possible to stop the Scene from Undo’ing / Redo’ing things in the current scene if my EditorWindow has the focus, with Ctrl + Z / Ctrl + Y?

Ex:

           bool doesThisWinHaveFocus = EditorWindow.focusedWindow == win;
           bool wasUndoRedoPerformed = Event.current.type == EventType.ValidateCommand  Event.current.commandName == "UndoRedoPerformed";
           if( wasUndoRedoPerformed  doesThisWinHaveFocus == true )
           {
             Event.current.Use();
           }
 
           if( Event.current.type == EventType.keyUp  Event.current.control  Event.current.keyCode == KeyCode.Z )
           {
             bPerformUndo = true;
             Event.current.Use();
           }
           else if( Event.current.type == EventType.keyUp  Event.current.control  Event.current.keyCode == KeyCode.Y )
           {
             bPerformRedo = true;
             Event.current.Use();
           }

Thanks

bumpy

:frowning: No one has a workaround?

someone?

If

Event.current.Use();

Doesn’t work, it means the event trigger the editor menu’s shortcut because being consumed by internal event… Sadly, it means you may be out of luck.

Could you register your own undo/redo events with the Editor so that it integrates with the built-in stuff rather than clashes with it?

Dang It. :confused:

Well, thank you for the reply. :wink:

I was very pessimistic so i kinda started working on a way to let the user enter their own shortcuts, Undo, Redo, etc…

ex:

I had to go through the Unity Editor Menus to manually build a List of the Menus with shortcuts… which i’ll check against when the user tries to set a shortcut while in the TextField (light green).

ex:

So far this seems the less aggressive approach, having the user specify a NEW Shortcut for Undo/Redo for my EditorWindow, since i rather not get into .net Reflection and try and see if i can hack my way into Overloading the default Unity Editor Shortcuts. :confused:

ohh, i have to check if its the MAC, instead of Ctrl i think it uses Cmnd. :confused:

Thanks again for all your help. :slight_smile:

We are 8 years later, is there a way now ? I often press ctrl+Z by mistake in my application :sweat_smile:

There has always been a way.

You can always react to this particular combination inside of your editor, then consume the event.
For example

var e = Event.current;
if(e.isKey && e.control && e.keyCode == KeyCode.Z) e.Use();

Like the other people I can’t get this to work.
What I did was

private void OnPressCtrlZ(KeyDownEvent e)
        {
            if(e.modifiers == EventModifiers.Control && e.keyCode == KeyCode.Z)
            {
                Debug.Log("CtrlZ capture!");
                e.StopImmediatePropagation();
                e.PreventDefault();
            }
        }

And it doesn’t work either

No that can’t work, you need to capture that event en-route, not at some haphazard moment in time.

Here, try this.

// EventConsumeTest.cs
using UnityEngine;

public class EventConsumeTest : MonoBehaviour {

  [SerializeField] bool suppressKeyboard;

}
// EventConsumeTestEditor.cs

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(EventConsumeTest)]
public class EventConsumeTestEditor : Editor {

  void OnEnable() {
    SceneView.duringSceneGui += sceneCallback;
  }

  void OnDisable() {
    SceneView.duringSceneGui -= sceneCallback;
  }

  void OnDestroy() => OnDisable();

  void sceneCallback(SceneView view) {
    if(serializedObject.FindProperty("suppressKeyboard").boolValue) {
      var e = Event.current;
      if(e.isKey) e.Use();
    }
  }

}

This will work as long as your mouse pointer is in the scene. I don’t have time right now to look for the holistic approach, but it should work and serve as a proof of concept.

Edit:
though your approach might have worked, if only you called Use.
Btw I used this particular way of doing this because I could confirm it in my editor. The only caveat is that the callback is not called if mouse isn’t over the scene.

Thanks, I tried to make this work but I gave up. For unknown reasons, I never receive any callback from “SceneView.duringSceneGui” even though “OnGUI” is called every frame for this game object.

Maybe that’s why. Try to comment out OnGUI. As far as I can remember it’s deprecated.

I don’t use it usually.
I think the problem is that I use UI Toolkit and the new input system so I guess all the events are generated differently.

I’m not sure about that. Maybe. My impression is that nothing has changed fundamentally. The new input system is unrelated, and the new UI is basically a redesign of how the UI system is used on top of old stuff. But I should definitely investigate more when I get some time.

I removed the keyboard shortcut in unity settings so I no longer have the issue but I’m sure other people are looking for a solution