InputEventMouse in Godot – Complete Guide

Understanding the interactions within a game is essential for creating a dynamic and responsive gameplay experience. One of the key components of this interactivity is how the game engine, such as Godot 4, handles inputs, particularly those coming from the mouse. In this tutorial, we will delve into the InputEventMouse class provided by Godot 4, examining what it is, how it works, and why mastering it can be crucial for your game development projects. Stay with us as we explore this foundational aspect of game input handling with practical examples that will enhance your skills and understanding.

What is InputEventMouse?

InputEventMouse is a class within the Godot 4 engine aimed at capturing and managing mouse-based events. As part of Godot’s input event system, it serves as the base class for other mouse event types like InputEventMouseButton and InputEventMouseMotion. Its purpose is to store general information related to mouse interactions, which can include positions, clicks, and movement.

What is it for?

This class plays a pivotal role in recognizing mouse actions and translating them into meaningful in-game behavior. Whether it’s moving a character, navigating a menu, or interacting with objects in your game, InputEventMouse is fundamental for interpreting these actions and providing feedback to the player.

Why Should I Learn It?

Learning about the InputEventMouse class will enable you to:
– Create more interactive and responsive games.
– Fine-tune your game’s control system.
– Handle complex input patterns with ease.

By grasping the intricacies of mouse input events, you are equipped with the skills to elevate the user experience by seamlessly integrating player input into your game’s mechanics. Let’s move forward and put this knowledge into action with some hands-on code examples!

CTA Small Image

Handling Mouse Button Events

To begin, we’ll look at how to handle mouse button events such as clicks, using the InputEventMouseButton class that extends from InputEventMouse. The following examples will demonstrate how to detect mouse button presses and releases within the Godot engine.

Detecting a Mouse Button Press

func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and event.pressed:
            print("Left mouse button pressed.")

This function listens for input events and checks if the event is a mouse button press. If it’s the left button and it’s pressed, we log a message to the console.

Detecting a Mouse Button Release

func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and not event.pressed:
            print("Left mouse button released.")

We handle the release of the left mouse button similarly, with the primary differentiator being the ‘not event.pressed’ condition.

Handling Right Mouse Button Clicks

func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_RIGHT:
            print("Right mouse button clicked.")

Similarly, to handle right mouse button clicks, we only need to change the button index to check for the right mouse button.

Working with Mouse Motion Events

Next, we’ll explore handling mouse motion events with the InputEventMouseMotion class, which is useful for detecting when the mouse is moving. This allows us to create mechanics like aiming or camera movement.

Basic Mouse Motion Detection

func _input(event):
    if event is InputEventMouseMotion:
        print("Mouse is moving")

This code snippet will log a message whenever the mouse is moved, helping us confirm that mouse motion is being detected.

Getting Mouse Movement Vector

func _input(event):
    if event is InputEventMouseMotion:
        print("Mouse moved by: ", event.relative)

In this example, we access the ‘relative’ property of the event to get the movement vector of the mouse, which indicates the mouse’s movement direction and magnitude.

Applying Mouse Movement to a Character

func _input(event):
    if event is InputEventMouseMotion:
        var character_rotation_speed = 0.05
        character.rotate(event.relative.x * character_rotation_speed)

This code rotates the character based on the relative x position of the mouse. We multiply the relative mouse movement by a speed modifier to control the rate of character rotation.

Using Mouse Motion for Camera Control

func _input(event):
    if event is InputEventMouseMotion:
        var camera_rotation_speed = 0.003
        camera.rotate_x(event.relative.y * camera_rotation_speed)
        camera.rotate_y(event.relative.x * camera_rotation_speed)

This snippet demonstrates how we can use the mouse’s motion to rotate a camera on both the X and Y axes, providing a first-person camera look mechanic.

With these foundations in place, we’re ready to build more sophisticated input systems, harness the full potential of mouse-based controls, and create compelling and interactive experiences for our players. In our upcoming examples, we will further refine these interactions and introduce advanced concepts for even more engaging gameplay.

Expanding on our understanding of mouse input, let’s dive deeper into more nuanced interactions. We’ll explore drag and drop functionality, scroll wheel usage, custom cursor settings, and combining mouse and keyboard inputs for a comprehensive control scheme.

Implementing Drag and Drop

To implement a basic drag-and-drop feature, we need to track both the movement and the button press/release of the mouse:

var is_dragging = false

func _input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
        if event.pressed:
            # When the left mouse button is pressed, dragging begins
            is_dragging = true
            # When released, dragging ends
            is_dragging = false
    elif event is InputEventMouseMotion and is_dragging:
        # While dragging, we move the object with the mouse

Scroll Wheel Interaction

Let’s utilize the mouse’s scroll wheel to zoom in and out of a camera’s perspective:

func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_WHEEL_UP:
            camera.zoom -= Vector2(0.1, 0.1)
        elif event.button_index == BUTTON_WHEEL_DOWN:
            camera.zoom += Vector2(0.1, 0.1)

This code reacts to the scroll wheel movements to adjust the zoom level of a 2D camera by modifying its zoom property, a Vector2(), with each scroll.

Custom Cursor Settings

Changing the cursor graphic enhances the user interface by providing visual feedback. Here’s how one might change the cursor to a custom image when hovering over an interactable object:

func _on_InteractableArea_mouse_entered():

func _on_InteractableArea_mouse_exited():

These functions set a custom cursor when the mouse enters an interactable area and reset it when the mouse leaves.

Combining Mouse and Keyboard Inputs

To create a more complex interaction, let’s combine mouse and keyboard inputs for an action like sprinting:

func _input(event):
    if event is InputEventKey:
        if event.scancode == KEY_SHIFT and event.pressed:
        elif event.scancode == KEY_SHIFT and not event.pressed:

func _input(event):
    if event is InputEventMouseMotion and player.is_sprinting:
        # Increase the turn speed while sprinting
        player.rotate(event.relative.x * sprint_turn_speed)

This example increases the turn speed of the player when they are sprinting (holding down the shift key) and moving the mouse.

Complex Click Patterns

Capturing double-clicks or other complex click patterns involves timed checks and state tracking:

var last_click_time = 0
var double_click_threshold = 0.3

func _input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
        var current_time = OS.get_system_time_secs()
        if current_time - last_click_time < double_click_threshold:
            last_click_time = current_time

func _on_double_click():
    # Handle double-click action

We register a double-click if two clicks occur within a short time interval, calling the `_on_double_click()` function to handle the specific action.

Through these sophisticated examples, we illustrate the dynamic capabilities at our fingertips when we fully utilize the mouse input events in Godot 4. By incorporating these techniques, developers can craft intuitive and immersive control schemes that elevate the player experience in their games. Remember, mastering input handling is just as critical as any other aspect of game development — it’s the direct line of communication between your game and its audience.

Furthering our mastery over mouse inputs, we will explore additional functionalities like creating tooltips on mouse hover, implementing a first-person shooter (FPS) style camera, and using the mouse for real-time strategy (RTS) game controls. Through these practical applications, you will learn to harness the full spectrum of mouse event capabilities within Godot 4.

Tooltips on Mouse Hover

Displaying tooltips when the mouse hovers over an item can be accomplished with signals and the `_process` function for real-time tracking:

var show_tooltip = false
var tooltip_timer = 0.0
var tooltip_delay = 1.0  # seconds

func _process(delta):
    if show_tooltip:
        tooltip_timer += delta
        if tooltip_timer > tooltip_delay:

func _on_Item_mouse_entered():
    show_tooltip = true

func _on_Item_mouse_exited():
    show_tooltip = false
    tooltip_timer = 0.0

Here, the tooltip is triggered if the mouse hovers over an item for more than a set delay, displaying valuable information to the player.

First-Person Shooter Camera Control

For an FPS camera, where mouse movement controls the player’s view direction, capturing mouse motion is critical:

func _input(event):
    if event is InputEventMouseMotion:
        var sensitivity = 0.002
        var rotation_degrees = Vector3(-event.relative.y * sensitivity, -event.relative.x * sensitivity, 0)

This code applies pitch and yaw rotations based on mouse motion, allowing for a first-person perspective camera control familiar to FPS players.

Real-Time Strategy Camera Movement

In an RTS game, mouse movement at the screen edges can be used to scroll the camera across the map:

func _process(delta):
    var scroll_speed = 100
    var mouse_position = get_viewport().get_mouse_position()
    if mouse_position.x  get_viewport().size.x - 10:
        camera.position.x += scroll_speed * delta
    if mouse_position.y  get_viewport().size.y - 10:
        camera.position.y += scroll_speed * delta

This snippet shifts the camera’s position when the mouse cursor approaches the edges of the viewport, allowing players to navigate the battlefield seamlessly.

Implementing Selection Boxes

Selection boxes in games are often used to select multiple units or objects. Here’s how to start drawing a selection rectangle on a mouse drag:

var drag_start_position = Vector2()
var dragging = false
var selection_rect = Rect2()

func _input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT:
        if event.pressed:
            drag_start_position = event.position
            dragging = true
            dragging = false
            # Perform selection based on `selection_rect`

func _process(delta):
    if dragging:
        var drag_current_position = get_viewport().get_mouse_position()
        selection_rect = Rect2(drag_start_position, drag_current_position - drag_start_position)

We capture the initial position where the drag starts and, upon release, create a `Rect2` to represent the selection box’s area.

Customizing Mouse Input Behavior

Last but not least, let’s consider how we can further customize mouse behavior. For instance, changing the click behavior based on the current game state:

enum GameState { Default, Building, Combat }

var game_state = GameState.Default

func _input(event):
    if event is InputEventMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
        match game_state:

Depending on the game’s current state, we execute different actions for the same left mouse click, making our controls context-sensitive and dynamic.

Through these examples, you begin to see the vast potential for innovation with mouse inputs in Godot 4. From tooltips to selection boxes, from RTS mechanics to FPS movement, mastering the use of the InputEventMouse class serves as a linchpin for engaging and responsive game development. We at Zenva are dedicated to empowering you with the knowledge to bring such interactions to life in your games. As you incorporate these methods, remember that flexibility and creativity in your input systems can significantly contribute to the polish and enjoyment of your final product.

Continue Your Game Development Journey

The world of game development is vast and ever-evolving, and your quest for knowledge doesn’t stop here. If you’re inspired to take your Godot skills further, our Godot Game Development Mini-Degree is the perfect next step. Whether you’re just starting out or looking to solidify your game development prowess, this Mini-Degree offers an extensive curriculum tailored to guide you through building cross-platform games with Godot 4.

With a variety of topics like 2D and 3D game creation, GDScript programming, control flow, combat mechanics, UI systems, and more, you’ll have the opportunity to dive deep into many facets of game development. Each course is crafted to suit different experience levels, with flexibility to learn at your own pace and create a portfolio of real Godot projects.

Apart from the Mini-Degree, we have a broader range of Godot courses catering to specific interests and needs. As always, Zenva aims to provide high-quality content that can take you from beginner to a professional game developer. Embrace the journey ahead and enrich your skills to bring your game visions to life!


As we draw our tutorial to a close, remember that every great game offers a seamless blend of storytelling, art, and most importantly, an intuitive interface that connects the player to the digital world. By mastering the use of InputEventMouse in Godot 4, you become not just a coder, but a craftsman of experiences, capable of weaving the intricate web of interactions that define the modern gaming landscape.

Don’t stop here! Continue to refine and perfect your skills with our comprehensive Godot Game Development Mini-Degree. Each lesson is a step forward on your path to becoming an accomplished game developer. Join us, as we embark on this creative journey together—unleashing imagination, shaping dreams into reality, and coding the next generation of amazing games.

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Godot, Unreal, Python and more.