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

Variables Table

VariablesDescription
integer RIGHTA code used for mouseButton to indicate the right mouse button.
integer SCROLLED_MOUSEA code used for eventType to indicate that the mouse wheel was scrolled.
integer ANYA code used for mouseButton to indicate any 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 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.
number scrollAmountThe amount the scroll wheel moved for this event. Positive numbers indicate the wheel scrolled up, and negative numbers indicate scroll down.
integer MOVED_MOUSEA code used for eventType to indicate that the mouse was moved.
integer FORWARDA code used for mouseButton to indicate the forward mouse button.
integer NONEA code used for mouseButton to indicate no mouse buttons are clicked.
integer BACKA code used for mouseButton to indicate the back mouse button.
integer MIDDLEA code used for mouseButton to indicate the middle mouse button.
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.
integer eventTypeWhat caused the event, e.g., CLICKED_MOUSE, MOVED_MOUSE, etc.
integer LEFTA code used for mouseButton to indicate the left mouse button.
integer CLICKED_MOUSEA code used for eventType to indicate that the mouse was clicked.

Actions Documentation

Compare(Libraries.Language.Object object)

This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.

Parameters

Return

integer: The Compare result, Smaller, Equal, or Larger.

Example

Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)

Equals(Libraries.Language.Object object)

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

Parameters

Return

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

Example

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

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.

Return

integer: The integer hash code of the object.

Example

Object o
integer hash = o:GetHashCode()

GetMovementX()

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

Return

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

Example

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

GetMovementY()

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

Return

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

Example

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

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.

Return

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

Example

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

GetX()

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

Return

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

Example

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

GetY()

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

Return

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

Example

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

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.

Return

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

Example

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

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.

Parameters

  • integer button: Which mouse button to check. Should be one of the mouse button constants in MouseEvent.

Return

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

Example

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

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.

Return

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

Example

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

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.

Return

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

Example

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

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.

Return

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

Example

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

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.

Return

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

Example

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

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.

Return

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

Example

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

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.

Return

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

Example

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

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.

Return

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

Example

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

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.

Return

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

Example

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

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.

Return

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

Example

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

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.

Parameters

  • boolean handled: Whether or not this event should be considered handled.

Example

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

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.

Parameters

Example

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

ToText(integer buttonCode)

The ToText action returns a textual representation of the provided mouse button code.

Parameters

  • integer buttonCode: A mouse button code, such as LEFT, RIGHT, or ANY.

Return

text: A text representation of the given button code, or "Unknown" if the code isn't recognized.

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.

Return

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

Example

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