Mouse events
Events that occur when the user moves the mouse.
読み終わるまでの所要時間 7 分最終更新 15日前
Mouse events occur when you interact with the UI using a mouse. Touch, pens, or other pointing devices generate other events, not mouse events. In the Mouse event APIs and in this documentation, the term “mouse” refers only to a physical mouse or a virtual mouse that emulates a physical mouse.
Mouse events are always preceded by the corresponding .
PointerEventThe base class for all mouse events is MouseEventBase.
The following table describes the mouse events and their propagation phases:
Event | Description | Trickles down | Bubbles up | Cancellable |
|---|---|---|---|---|
| MouseDownEvent | Sent when the user presses a mouse button. | Yes | Yes | Yes |
| MouseUpEvent | Sent when the user releases a mouse button. | Yes | Yes | Yes |
| MouseMoveEvent | Sent when the user moves the mouse. | Yes | Yes | Yes |
| WheelEvent | Sent when the user activates the mouse wheel. | Yes | Yes | Yes |
| MouseEnterWindowEvent | Sent when the mouse enters a window. | Yes | ||
| MouseLeaveWindowEvent | Sent when the mouse leaves a window. | Yes | ||
| MouseEnterEvent | Sent when the mouse enters an element or one of its descendants. | Yes | Yes | |
| MouseLeaveEvent | Sent when the mouse leaves an element or one of its descendants. | Yes | Yes | |
| MouseOverEvent | Sent when the mouse enters an element. | Yes | Yes | Yes |
| MouseOutEvent | Sent when the mouse leaves an element. | Yes | Yes | Yes |
| ContextClickEvent (obsolete) | Sent when the user presses and releases the third mouse button. Exists for backwards compatibility with IMGUI. | Yes | Yes | Yes |
Unique Properties
buttonbuttonInteger | Button |
|---|---|
| 0 | Left button |
| 1 | Right button |
| 2 | Middle button |
pressedButtonspressedButtonThe number is the sum of the individual buttons’ integer value (see table below). For example, holding the right mouse button and the middle mouse button pressed at the same time will result in pressedButton having a value of 6.
Integer | Button |
|---|---|
| 1 | Left button |
| 2 | Right button |
| 4 | Middle button |
modifiersmodifiersShiftCtrlAltFor more information, see the Modifier keys section of the MDN documentation.
mousePositionmousePositionlocalMousePositionlocalMousePositionmouseDeltaEvent list
The following list provides the name, description, and target of each event in the event family. For more information on the event, see the UI Toolkit API.
MouseDownEvent
A MouseDownEvent is sent when the mouse button is pressed while the cursor is inside a visual element.
targetMouseUpEvent
The MouseUpEvent triggers when a mouse button releases while the cursor is within a visual element. is complimentary to .
MouseUpEventMouseDownEventtargetMouseMoveEvent
The MouseMoveEvent is sent when the cursor hotspot is moved within a visual element.
targetWheelEvent
The WheelEvent is sent when the mouse wheel is pressed.
targetMouseEnterWindowEvent
A MouseEnterWindowEvent triggers when the cursor is moved into an Editor window. Runtime panels don't receive this event when you enter the Game view window.
targetMouseLeaveWindowEvent
MouseLeaveWindowEvent fires when the cursor exits the space of an Editor window. The is the counterpoint to .
MouseLeaveWindowEventMouseEnterWindowEventtargetMouseEnterEvent
The MouseEnterEvent is sent when the cursor is moved into a visual element, or one of its descendants.
targetMouseLeaveEvent
MouseLeaveEvent triggers when the cursor moves outside of a visual element. This event differs from as this event is sent to each element the mouse exits. This event doesn't propagate.
MouseOutEventtargetMouseOverEvent
The MouseOverEvent is sent when the cursor enters an element. This differs from , because this event is only sent to the entered element.
MouseEnterEventtargetMouseOutEvent
The MouseOutEvent triggers when a pointing device moves the cursor outside the boundary of a visual element.
MouseOutEventMouseLeaveEventMouseOutEventMouseLeaveEventtargetContextualMenuPopulateEvent
Event sent by the when the contextual menu needs to be populated with menu items.
ContextualMenuManagertargetContextClickEvent (obsolete)
Event sent when the user presses and releases the third mouse button. This event only exists for backward compatibility with IMGUI.
Examples
Editor window example
The following code sample creates an Editor window with three buttons that prints messages to the console when the mouse moves over an element, or buttons are pressed on the mouse.
This code sample highlights the event firing of both and , and how to use the event parameters.
MouseDownEventMouseEnterEventTo see the example in action, do the following:
- Create a new C# script called .
MouseEventTestWindow.cs - Copy the example into the C# script.
- Open the example under Window > UI Toolkit > Mouse Event Test Window.
using UnityEditor;using UnityEngine;using UnityEngine.UIElements;// Open this in the Editor via the menu Window > UI ToolKit > Mouse Event Test Windowpublic class MouseEventTestWindow : EditorWindow{ [MenuItem("Window/UI Toolkit/Mouse Event Test Window")] public static void ShowExample() { MouseEventTestWindow wnd = GetWindow<MouseEventTestWindow>(); wnd.titleContent = new GUIContent("Mouse Event Test Window"); } public void CreateGUI() { // Add a few buttons for (int i = 0; i < 3; i++) { Button newElement = new Button { name = $"Button {i}", text = $"Button {i}" }; newElement.style.flexGrow = 1; rootVisualElement.Add(newElement); } // Register mouse event callbacks rootVisualElement.RegisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown); rootVisualElement.RegisterCallback<MouseEnterEvent>(OnMouseEnter, TrickleDown.TrickleDown); } private void OnMouseDown(MouseDownEvent evt) { bool leftMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.LeftMouse)); bool rightMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.RightMouse)); bool middleMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.MiddleMouse)); Debug.Log($"Mouse Down event. Triggered by {(MouseButton)evt.button}."); Debug.Log($"Pressed buttons: Left button: {leftMouseButtonPressed} Right button: {rightMouseButtonPressed} Middle button: {middleMouseButtonPressed}"); } private void OnMouseEnter(MouseEnterEvent evt) { VisualElement targetElement = (VisualElement)evt.target; Debug.Log($"Mouse is now over element '{targetElement.name}'"); }}
Runtime example
The following code sample prints messages to the console when any mouse buttons are pressed, showing which button triggered the event, and which buttons are currently pressed.
This code sample highlights registering a callback to the and how to use the event parameters. To see the example in action, do the following:
MouseDownEvent- Create a GameObject with a valid UIDocument.
- Under Assets > Scripts, create a C# script called MouseEventTestRuntime.
- Copy the example into the C# script.
- Attach the MouseEventTestRuntime script to the GameObject with the UIDocument.
- Enter Play mode.
- Move the mouse over the game view and press or hold buttons on the mouse.
using UnityEngine;using UnityEngine.UIElements;public class MouseEventTestRuntime : MonoBehaviour{ void Start() { var root = GetComponent<UIDocument>().rootVisualElement; var newLabel = new Label("Move the mouse or press buttons to see the log output"); newLabel.style.flexGrow = 1; root.Add(newLabel); root.RegisterCallback<MouseDownEvent>(OnMouseDown, TrickleDown.TrickleDown); } private void OnMouseDown(MouseDownEvent evt) { bool leftMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.LeftMouse)); bool rightMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.RightMouse)); bool middleMouseButtonPressed = 0 != (evt.pressedButtons & (1 << (int)MouseButton.MiddleMouse)); VisualElement targetElement = (VisualElement)evt.target; Debug.Log($"Mouse Down event. Triggered by {(MouseButton)evt.button} over element '{targetElement.name}'"); Debug.Log($"Pressed buttons: Left button: {leftMouseButtonPressed} Right button: {rightMouseButtonPressed} Middle button: {middleMouseButtonPressed}"); }}