Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Shift-Clicking a Button

Discussion in 'UI Toolkit' started by Drakkith, Oct 18, 2022.

  1. Drakkith

    Drakkith

    Joined:
    Nov 29, 2016
    Posts:
    57
    Hey all. I've got a bunch of buttons that I'd like to do something when I shift-click on them, but I can't seem to figure out how. If I hold down shift (or control or alt) and click on a UI Button nothing happens. The button.clicked function doesn't seem to trigger at all.

    Any ideas?
     
  2. Drakkith

    Drakkith

    Joined:
    Nov 29, 2016
    Posts:
    57
    Update: Okay, managed to make it work, but it seems like kind of a weird fix. I have to use button.clicked to register just clicking on the button, but I have to use a RegisterCallBack<MouseDownEvent> to capture a shift-click. The code button.clicked won't work on modifier-clicking, and using a callback won't work for regular clicking. Here's the code:
    Code (CSharp):
    1. button.clicked += () => ButtonPressed();
    2. button.RegisterCallback<MouseDownEvent>(evt => ButtonPressed());
    So both methods call the same function and I have an If-Else check inside that function to check if a modifier key is being held down .

    How strange that two different pieces of codes are required for the two different clicks.
     
    Last edited: Oct 18, 2022
  3. Drakkith

    Drakkith

    Joined:
    Nov 29, 2016
    Posts:
    57
    Update 2: Ah, I figured out what's going on. The Button UI Element has a ManipulatorActivationFilter property that can be edited to add more functions and modifiers. So to add the ability to control, shift, or alt-click, use the following code:
    Code (CSharp):
    1. Button button = new Button();
    2. button.clickable.activators.Add(new ManipulatorActivationFilter { button = UnityEngine.UIElements.MouseButton.LeftMouse, modifiers = EventModifiers.Shift });
    3. button.clickable.activators.Add(new ManipulatorActivationFilter { button = UnityEngine.UIElements.MouseButton.LeftMouse, modifiers = EventModifiers.Alt });
    4. button.clickable.activators.Add(new ManipulatorActivationFilter { button = UnityEngine.UIElements.MouseButton.LeftMouse, modifiers = EventModifiers.Control });
    Reference the API for Buttons here: https://docs.unity3d.com/ScriptReference/UIElements.Button.html
    Link to the ManipulatorActivationFilter is near the beginning of that page.
     
    StefanWo and AngelGuzman like this.
  4. Oarcinae

    Oarcinae

    Joined:
    Mar 1, 2017
    Posts:
    1
    Hey, do you have any pointers for how to detect which modifier was used in code?
    I'm getting confused between UnityEngine.UI and UnityEngine.UIElements.

    (This is the top result for me in google and I'm struggling to find an answer to how to add modifier keys for clicking Unity UI elements. The other way I found is detect modifier keys separately... not sure I want to do that though?)

    Thanks!

    EDIT: I think I'm confusing Unity's UI system with the newer Unity UI Toolkit...
     
    Last edited: Dec 8, 2022
  5. Drakkith

    Drakkith

    Joined:
    Nov 29, 2016
    Posts:
    57
    Sorry for the late reply.
    I'm not sure what you mean when you ask about detecting which modifier was used in code.
    If you're just trying to add modifiers, it should be as simple as my example in post #3.

    Yes, it's all a bit confusing unfortunately. Unity currently has 3 different UI systems to choose from; UI Toolkit, The Unity UI package (GUI), and IMGUI. The first is the newest, and UIElements goes with the UI Toolkit I believe.