Mouse events occur when you interact with the UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary 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 PointerEvent
.
The base class for all mouse events is MouseEventBase.
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 |
button
: The button
property returns an integer that identifies the mouse button pressed to trigger an event. The following table lists the integer and associated mouse button:
Integer | Button |
---|---|
0 | Left button |
1 | Right button |
2 | Middle button |
pressedButtons
: The pressedButton
property returns an integer that identifies which combination of mouse buttons are currently pressed.
The 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 |
modifiers
: The modifiers
property returns the modifier key pressed during a keyboard event. Some examples of modifiers are the Shift
, Ctrl
, or Alt
keys.
For more information, see the Modifier keys section of the MDN documentation.
mousePosition
: The mousePosition
property returns the mouse position within the panel, also known as the screen coordinate system. For more information on panel coordinates, see the Visual Tree page.
localMousePosition
: The localMousePosition
property returns the coordinates relative to the target visual elementA node of a visual tree that instantiates or derives from the C# VisualElement
class. You can style the look, define the behaviour, and display it on screen as part of the UI. More info
See in Glossary.
mouseDelta
: The difference between the pointer’s position during the previous mouse event and its position during the current mouse event.
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.
A MouseDownEvent is sent when the mouse button is pressed while the cursor is inside a visual element.
target
: The visual element that receives the mouse capture. Otherwise, it’s the topmost selectable element under the cursor.
The MouseUpEvent triggers when a mouse button releases while the cursor is within a visual element. MouseUpEvent
is complimentary to MouseDownEvent
.
target
: The visual element that receives the mouse capture. Otherwise, it’s the topmost selectable element under the cursor.
The MouseMoveEvent is sent when the cursor hotspot is moved within a visual element.
target
: The visual element that receives the mouse capture. Otherwise, it’s the topmost selectable element under the cursor.
The WheelEvent is sent when the mouse wheel is pressed.
target
: The visual element that receives the mouse capture. Otherwise, it’s the topmost selectable element under the cursor.
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.
target
: The visual element that receives the mouse capture. Otherwise, it’s the topmost selectable element under the cursor.
MouseLeaveWindowEvent fires when the cursor exits the space of an Editor window. The MouseLeaveWindowEvent
is the counterpoint to MouseEnterWindowEvent
.
target
: The visual element that receives the mouse capture. Otherwise it returns null, as the cursor isn’t over an element.
The MouseEnterEvent is sent when the cursor is moved into a visual element, or one of its descendants.
target
: The visual element under the mouse cursor, or one of its descendants.
MouseLeaveEvent triggers when the cursor moves outside of a visual element. This event differs from MouseOutEvent
as this event is sent to each element the mouse exits. This event doesn’t propagate.
target
: The visual element (or one of its descendants) that the mouse cursor exits.
The MouseOverEvent is sent when the cursor enters an element. This differs from MouseEnterEvent
, because this event is only sent to the entered element.
target
: The visual element that’s under the mouse cursor.
The MouseOutEvent triggers when a pointing device moves the cursor outside the boundary of a visual element.
MouseOutEvent
is different from MouseLeaveEvent
in that MouseOutEvent
is sent when leaving the visual element to any other element, while MouseLeaveEvent
isn’t sent when transitioning from a visual element to descendant elements.
target
: The visual element that the mouse cursor exited.
Event sent by the ContextualMenuManager
when the contextual menu needs to be populated with menu items.
target
: The visual element for which the contextual menu is being built.
Event sent when the user presses and releases the third mouse button. This event only exists for backward compatibility with IMGUI.
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 MouseDownEvent
and MouseEnterEvent
, and how to use the event parameters.
To see the example in action, do the following:
MouseEventTestWindow.cs
.using UnityEditor; using UnityEngine; using UnityEngine.UIElements; // Open this in the Editor via the menu Window > UI ToolKit > Mouse Event Test Window public 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}'"); } }
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 MouseDownEvent
and how to use the event parameters.
To see the example in action, do the following:
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}"); } }
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.