Libraries.Interface.Events.MouseEvent Documentation

The MouseEvent class represents an event where the user interacted with the mouse by clicking or releasing a mouse button, moving or dragging the mouse, or scrolling the mouse wheel.

Inherits from: Libraries.Language.Object

Summary

Variable Summary Table

VariablesDescription
integer SCROLLED_MOUSEA code used for eventType to indicate that the mouse wheel was scrolled.
integer mouseButtonWhat mouse buttons, if any, are involved in this event. This value is a bitmask. If only a single button is involved in the event, the value will equal the constant value of that mouse button. If multiple buttons are involved, this value will equal the sum of all involved buttons.
integer RELEASED_MOUSEA code used for eventType to indicate that a mouse button was released.
integer DRAGGED_MOUSEA code used for eventType to indicate that the mouse was dragged.
number scrollAmountThe amount the scroll wheel moved for this event. Positive numbers indicate the wheel scrolled up, and negative numbers indicate scroll down.
integer CLICKED_MOUSEA code used for eventType to indicate that the mouse was clicked.
integer ANYA code used for mouseButton to indicate any mouse button.
integer BACKA code used for mouseButton to indicate the back mouse button.
integer NONEA code used for mouseButton to indicate no mouse buttons are clicked.
integer LEFTA code used for mouseButton to indicate the left mouse button.
integer MIDDLEA code used for mouseButton to indicate the middle mouse button.
integer clickCountHow many clicks have occurred in a short period of time, including this event (if it is a CLICKED_MOUSE event).
integer MOVED_MOUSEA code used for eventType to indicate that the mouse was moved.
integer RIGHTA code used for mouseButton to indicate the right mouse button.
integer eventTypeWhat caused the event, e.g., CLICKED_MOUSE, MOVED_MOUSE, etc.
integer FORWARDA code used for mouseButton to indicate the forward mouse button.

Actions Summary Table

ActionsDescription
Compare(Libraries.Language.Object object)This action compares two object hash codes and returns a CompareResult.
Equals(Libraries.Language.Object object)This action determines if two objects are equal based on their hash code values.
GetClickCount()This action returns how many times the mouse button described by this event has been recently clicked.
GetHashCode()This action gets the hash code for an object.
GetMovementX()This action returns how much the mouse has moved on the x-axis since the last movement event.
GetMovementY()This action returns how much the mouse has moved on the y-axis since the last movement event.
GetSource()This action returns the source item for this event, which is the the target of mouse event which has occurred (or is otherwise relevant to it).
GetX()This action returns the x coordinate of where the event was on the screen.
GetY()This action returns the y coordinate of where the event was on the screen.
IsBackButtonEvent()The IsBackButtonEvent action checks if the back mouse button was involved in the event.
IsButtonEvent(integer button)The IsButtonEvent action checks if a particular button was involved in the event.
IsClicked()This action checks if this event was triggered by a mouse button being clicked.
IsDragged()This action checks if this event was triggered by the mouse being dragged.
IsForwardButtonEvent()The IsBackButtonEvent action checks if the forward mouse button was involved in the event.
IsLeftButtonEvent()The IsLeftButtonEvent action checks if the left mouse button was involved in the event.
IsMiddleButtonEvent()The IsMiddleButtonEvent action checks if the middle mouse button was involved in the event.
IsMoved()This action checks if this event was triggered by the mouse being moved.
IsReleased()This action checks if this event was triggered by a mouse button being released.
IsRightButtonEvent()The IsRightButtonEvent action checks if the right mouse button was involved in the event.
IsScrolled()This action checks if this event was triggered by the mouse wheel being scrolled.
SetEventHandled(boolean handled)This action sets whether this event should be considered handled by any event listeners.
SetSource(Libraries.Interface.Item item)This action sets the source item for this event, which is the the target of mouse event which has occurred (or is otherwise relevant to it).
WasEventHandled()This action returns whether or not this event has already been handled.

Actions Documentation

Compare(Libraries.Language.Object object)

This action compares two object hash codes and returns a CompareResult. The compare result is either larger if this hash code is larger than the object passed as a parameter, smaller, or equal.

Example Code

use Libraries.Language.Support.CompareResult
        Object o
        Object t
        CompareResult result = o:Compare(t)

Parameters

Return

Libraries.Language.Support.CompareResult: The Compare result, Smaller, Equal, or Larger.

Equals(Libraries.Language.Object object)

This action determines if two objects are equal based on their hash code values.

Example Code

use Libraries.Language.Object
        use Libraries.Language.Types.Text
        Object o
        Text t
        boolean result = o:Equals(t)

Parameters

Return

boolean: True if the hash codes are equal and false if they are not equal.

GetClickCount()

This action returns how many times the mouse button described by this event has been recently clicked. The time considered to be "recent" is defined by the ApplicationConfiguration (if mouse input is relevant to the application type). The value returned includes the button being clicked as part of this event, if the event is a CLICKED_MOUSE event - e.g., if the mouse is clicked exactly once, the value returned is 1. If a mouse button is clicked twice in rapid succession, the value returned will be 2.

Return

integer: How many times the mouse button has been recently clicked.

GetHashCode()

This action gets the hash code for an object.

Example Code

Object o
        integer hash = o:GetHashCode()

Return

integer: The integer hash code of the object.

GetMovementX()

This action returns how much the mouse has moved on the x-axis since the last movement event.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener
    use Libraries.Interface.Events.MouseMovementListener

    // To try this example, click anywhere on the screen, drag the mouse, and release.
    class Main is Game, MouseListener, MouseMovementListener

        // Variables used to store the sum of the mouse movement between mouse
        // click and mouse release.
        integer sumX = 0
        integer sumY = 0

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
            AddMouseMovementListener(me)
        end

        action ClickedMouse(MouseEvent event)
            sumX = 0
            sumY = 0
        end

        action DraggedMouse(MouseEvent event)
            sumX = sumX + event:GetMovementX()
            sumY = sumY + event:GetMovementY()
        end

        action ReleasedMouse(MouseEvent event)
            output "The total distance moved between mouse clicked and released was " + sumX + ", " + sumY
        end
    end

Return

integer: How much the mouse has moved on the x-axis since the last movement event.

GetMovementY()

This action returns how much the mouse has moved on the y-axis since the last movement event.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener
    use Libraries.Interface.Events.MouseMovementListener

    // To try this example, click anywhere on the screen, drag the mouse, and release.
    class Main is Game, MouseListener, MouseMovementListener

        // Variables used to store the sum of the mouse movement between mouse
        // click and mouse release.
        integer sumX = 0
        integer sumY = 0

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
            AddMouseMovementListener(me)
        end

        action ClickedMouse(MouseEvent event)
            sumX = 0
            sumY = 0
        end

        action DraggedMouse(MouseEvent event)
            sumX = sumX + event:GetMovementX()
            sumY = sumY + event:GetMovementY()
        end

        action ReleasedMouse(MouseEvent event)
            output "The total distance moved between mouse clicked and released was " + sumX + ", " + sumY
        end
    end

Return

integer: How much the mouse has moved on the y-axis since the last movement event.

GetSource()

This action returns the source item for this event, which is the the target of mouse event which has occurred (or is otherwise relevant to it). In the Game engine, if an Item is clicked directly, it will be stored here. If a MouseEvent is triggered with no such Item available, this will be undefined.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener
    use Libraries.Interface.Item
    use Libraries.Game.Graphics.Drawable

    class Main is Game, MouseListener

        Drawable square

        action Main
            StartGame()
        end

        action CreateGame
            square:LoadFilledRectangle(200, 200)
            square:SetPosition(300, 200)
            Add(square)

            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            Item source = event:GetSource()

            if source = undefined
                output "The background was clicked!"
            elseif source:Equals(square)
                output "The square was clicked!"
            end
        end
    end

Return

Libraries.Interface.Item: The source Item for this MouseEvent.

GetX()

This action returns the x coordinate of where the event was on the screen.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            integer x = event:GetX()
            integer y = event:GetY()
            output "The mouse was clicked at " + x + ", " + y
        end
    end

Return

integer: The x coordinate of where the event was on the screen.

GetY()

This action returns the y coordinate of where the event was on the screen.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            integer x = event:GetX()
            integer y = event:GetY()
            output "The mouse was clicked at " + x + ", " + y
        end
    end

Return

integer: The y coordinate of where the event was on the screen.

IsBackButtonEvent()

The IsBackButtonEvent action checks if the back mouse button was involved in the event. For example, it indicates if the back button was clicked as part of a CLICKED_MOUSE event, if it was released as part of a RELEASED_MOUSE event, or if it was held down during a DRAGGED_MOUSE event.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            if event:IsBackButtonEvent()
                output "The back mouse button was clicked!"
            end
        end
    end

Return

boolean: Whether or not the back mouse button was involved in this event.

IsButtonEvent(integer button)

The IsButtonEvent action checks if a particular button was involved in the event. For example, it indicates if a particular button was clicked as part of a CLICKED_MOUSE event, if a button was released as part of a RELEASED_MOUSE event, or if a button was held down during a DRAGGED_MOUSE event. The given button parameter should be one of the mouse button constants in this class.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        integer lastButton = 0

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            if event:IsButtonEvent(lastButton)
                output "The same button was clicked twice in a row!"
            end

            lastButton = event:mouseButton
        end
    end

Parameters

Return

boolean: Whether or not the given button was involved in this event.

IsClicked()

This action checks if this event was triggered by a mouse button being clicked. If it was, the action returns true. Otherwise, it returns false.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            OutputEvent(event)
        end

        action ReleasedMouse(MouseEvent event)
            OutputEvent(event)
        end

        action OutputEvent(MouseEvent event)
            if event:IsClicked()
                output "The mouse button was clicked!"
            elseif event:IsReleased()
                output "The mouse button was released!"
            end
        end
    end

Return

boolean: Whether or not this event was triggered by a mouse click.

IsDragged()

This action checks if this event was triggered by the mouse being dragged. If it was, the action returns true. Otherwise, it returns false.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseMovementListener

    class Main is Game, MouseMovementListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseMovementListener(me)
        end

        action MovedMouse(MouseEvent event)
            OutputEvent(event)
        end

        action DraggedMouse(MouseEvent event)
            OutputEvent(event)
        end

        action OutputEvent(MouseEvent event)
            if event:IsMoved()
                output "The mouse was moved!"
            elseif event:IsDragged()
                output "The mouse was dragged!"
            end
        end
    end

Return

boolean: Whether or not this event was triggered by mouse dragging.

IsForwardButtonEvent()

The IsBackButtonEvent action checks if the forward mouse button was involved in the event. For example, it indicates if the forward button was clicked as part of a CLICKED_MOUSE event, if it was released as part of a RELEASED_MOUSE event, or if it was held down during a DRAGGED_MOUSE event.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            if event:IsForwardButtonEvent()
                output "The forward mouse button was clicked!"
            end
        end
    end

Return

boolean: Whether or not the forward mouse button was involved in this event.

IsLeftButtonEvent()

The IsLeftButtonEvent action checks if the left mouse button was involved in the event. For example, it indicates if the left button was clicked as part of a CLICKED_MOUSE event, if it was released as part of a RELEASED_MOUSE event, or if it was held down during a DRAGGED_MOUSE event.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            if event:IsLeftButtonEvent()
                output "The left mouse button was clicked!"
            end
        end
    end

Return

boolean: Whether or not the left mouse button was involved in this event.

IsMiddleButtonEvent()

The IsMiddleButtonEvent action checks if the middle mouse button was involved in the event. For example, it indicates if the middle button was clicked as part of a CLICKED_MOUSE event, if it was released as part of a RELEASED_MOUSE event, or if it was held down during a DRAGGED_MOUSE event.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            if event:IsMiddleButtonEvent()
                output "The middle mouse button was clicked!"
            end
        end
    end

Return

boolean: Whether or not the middle mouse button was involved in this event.

IsMoved()

This action checks if this event was triggered by the mouse being moved. If it was, the action returns true. Otherwise, it returns false.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseMovementListener

    class Main is Game, MouseMovementListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseMovementListener(me)
        end

        action MovedMouse(MouseEvent event)
            OutputEvent(event)
        end

        action DraggedMouse(MouseEvent event)
            OutputEvent(event)
        end

        action OutputEvent(MouseEvent event)
            if event:IsMoved()
                output "The mouse was moved!"
            elseif event:IsDragged()
                output "The mouse was dragged!"
            end
        end
    end

Return

boolean: Whether or not this event was triggered by mouse movement.

IsReleased()

This action checks if this event was triggered by a mouse button being released. If it was, the action returns true. Otherwise, it returns false.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            OutputEvent(event)
        end

        action ReleasedMouse(MouseEvent event)
            OutputEvent(event)
        end

        action OutputEvent(MouseEvent event)
            if event:IsClicked()
                output "The mouse button was clicked!"
            elseif event:IsReleased()
                output "The mouse button was released!"
            end
        end
    end

Return

boolean: Whether or not this event was triggered by a mouse button being released.

IsRightButtonEvent()

The IsRightButtonEvent action checks if the right mouse button was involved in the event. For example, it indicates if the right button was clicked as part of a CLICKED_MOUSE event, if it was released as part of a RELEASED_MOUSE event, or if it was held down during a DRAGGED_MOUSE event.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            if event:IsRightButtonEvent()
                output "The right mouse button was clicked!"
            end
        end
    end

Return

boolean: Whether or not the right mouse button was involved in this event.

IsScrolled()

This action checks if this event was triggered by the mouse wheel being scrolled. If it was, the action returns true. Otherwise, it returns false.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseWheelListener

    class Main is Game, MouseWheelListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseWheelListener(me)
        end

        action ScrolledMouse(MouseEvent event)
            OutputEvent(event)
        end

        action OutputEvent(MouseEvent event)
            if event:IsScrolled()
                output "The mouse wheel was scrolled!"
            end
        end
    end

Return

boolean: Whether or not this event was triggered by the mouse wheel scrolling.

SetEventHandled(boolean handled)

This action sets whether this event should be considered handled by any event listeners. In the Game engine, this is set to true just before sending the event to a listener. If the value is reset to false inside the listener, the engine will continue to send the event to other event listeners.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        integer totalClicks = 0

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            totalClicks = totalClicks + 1
            output "The mouse has been clicked " + totalClicks + " times."

            // By setting the event handled flag to false, we tell the Game engine
            // to keep sending the MouseEvent to any other MouseListeners we have.
            // In this example, since it's our only MouseListener, it does nothing.
            event:SetEventHandled(false)
        end
    end

Parameters

SetSource(Libraries.Interface.Item item)

This action sets the source item for this event, which is the the target of mouse event which has occurred (or is otherwise relevant to it). In the Game engine, if an Item is clicked directly, it will be stored here. If a MouseEvent is triggered with no such Item available, this will be undefined.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener
    use Libraries.Interface.Item2D

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            if event:GetSource() not= undefined
                return now
            end

            Item2D newItem
            event:SetSource(newItem)
            event:SetEventHandled(false)
        end
    end

Parameters

WasEventHandled()

This action returns whether or not this event has already been handled. In the Game engine, this is set to true just before sending the event to a listener. If the value is reset to false inside the listener, the engine will continue to send the event to other event listeners.

Example Code

use Libraries.Game.Game
    use Libraries.Interface.Events.MouseEvent
    use Libraries.Interface.Events.MouseListener

    class Main is Game, MouseListener

        action Main
            StartGame()
        end

        action CreateGame
            AddMouseListener(me)
        end

        action ClickedMouse(MouseEvent event)
            // The event handled flag is set to true when the engine finds a mouse
            // listener to send the event to, so this should always return true.
            boolean handled = event:WasEventHandled()
            output "After clicking the mouse, handled = " + handled
        end
    end

Return

boolean: Whether or not this event has already been handled.