Skip to content
Lulezer edited this page Apr 2, 2025 · 5 revisions

Handling Input in Echlib 🎮

To make your game interactive, you need to handle keyboard and mouse input. Echlib provides functions to detect key presses, mouse clicks, and more.

  1. Checking for Key Presses To check if a key is pressed, use:

``IsKeyPressed()```

if (ech::IsKeyPressed(ech::KEY_W)) {
    // Move up
}

Note ech::IsKeyHeld returns true only while the key is held down.

IsKeyHeld→ Runs as long as the key is held.

IsKeyPressed → Only runs once when the key is first pressed.
  1. Checking for Mouse Input To check if a mouse button is pressed:
if (ech::IsMouseButtonPressed(ech::MOUSE_LEFT_BUTTON)) {
    // Left mouse button clicked
}

Note

Mouse buttons are:

ech::MOUSE_LEFT_BUTTON

ech::MOUSE_RIGHT_BUTTON

ech::MOUSE_MIDDLE_BUTTON
  1. Getting Mouse Position To get the mouse cursor's position:

ech::Vector2 mousePos = ech::GetMousePosition(); You can then access it by:

mousePos.x And mousePos.y

  1. Full Example: Basic Movement This example moves a rectangle using keyboard input.
#include "echlib.h"  

#include "echlib.h"  
#include <iostream>  // Include for std::cout

int main() {
    ech::MakeWindow(800, 600, "Input Example");
    ech::SetTargetFps(60);

    double rectX = 400, rectY = 300;
    double speed = 0.05;

    while (!ech::WindowShouldClose()) {
        // Move with arrow keys
        if (ech::IsKeyHeld(KEY_UP)) rectY += speed;
        if (ech::IsKeyHeld(KEY_DOWN)) rectY -= speed;
        if (ech::IsKeyHeld(KEY_LEFT)) rectX -= speed;
        if (ech::IsKeyHeld(KEY_RIGHT)) rectX += speed;


        ech::StartDrawing();
        ech::ClearBackground(ech::BLACK);

        // Draw the rectangle
        ech::DrawRectangle(rectX, rectY, 50, 50, ech::DARK_GREEN);

        ech::EndDrawing();
    }

    ech::CloseWindow();
    return 0;
}

This example: ✅ Moves a rectangle with arrow keys.

Now you know how to handle input in Echlib! 🚀

Yo

Clone this wiki locally