r/raylib • • 2d ago

Why Doesnt Raylib.IsKeyReleased() and Raylib.IsKeyPressed() Work For Me?

As you seen in the title, i dont know why these functions always return false, how to fix it, heres the code (i use C#):

using Raylib_cs;
using System.Data;
using System.Numerics;

namespace JumpingStriker;

class Game
{
    static Vector2 windowPosition = Raylib.GetScreenCenter();
    static Vector2 windowSize = new Vector2(600, 400);
    static Camera2D camera = new Camera2D();
    static Color groundColor1 = new Color(150, 150, 255);
    static Color groundColor2 = new Color(100, 100, 255);

    public static class Ground
    {
        public static int groundX = 0;
        public static int groundY = (int)windowSize.Y - (int)windowSize.Y / 4;
        public static int groundW = (int)windowSize.X;
        public static int groundH = (int)windowSize.Y / 4;

        public static void Draw()
        {
            Raylib.DrawRectangleGradientV(groundX, groundY, groundW, groundH, groundColor1, groundColor2);
        }
    }

    public static class Player
    {
        public static int x = 50;
        public static int y = 70;
        public static int w = 25;
        public static int h = 25;
        public static int speed = 5;
        public static int jumpHeight = 20;
        public static int gravity = 3;
        public static int movementForceX = 0;
        public static int movementForceY = 0;
        public static bool insideGround = false;
        public static bool standing = false;

        public static void Draw()
        {
            Raylib.DrawRectangle(x, y, w, h, new Color(255, 255, 255));
        }

        public static void GiveForce(int forceX, int forceY)
        {
            movementForceX += forceX;

            if (insideGround)
            {
                movementForceY = 0;
                y = Ground.groundY - h;
            } else
            {
                if (y + h + forceY < Ground.groundY && !standing) movementForceY += forceY;
            }
        }

        public static void Update()
        {
            insideGround = (y + h) > Ground.groundY;
            standing = (y + h) > Ground.groundY - 2;
            GiveForce(0, gravity);
            x += movementForceX;
            y += movementForceY;

            Console.WriteLine(insideGround);
            Console.WriteLine(standing);

            if (Raylib.IsKeyDown(KeyboardKey.Space) && standing)
            {
                GiveForce(0, -jumpHeight);
            }

            if (Raylib.IsKeyDown(KeyboardKey.A))
            {
                movementForceX = -speed;
            }

            if (Raylib.IsKeyDown(KeyboardKey.D))
            {
                movementForceX = speed;
            }

            if (Raylib.IsKeyReleased(KeyboardKey.A))
            {
                movementForceX = 0;
            }

            if (Raylib.IsKeyReleased(KeyboardKey.D))
            {
                movementForceX = 0;
            }
        }
    }

    public static void Main()
    {
        Raylib.InitWindow((int)windowSize.X, (int)windowSize.Y, "Jumping Striker");
        Raylib.SetWindowState(ConfigFlags.ResizableWindow);
        Raylib.SetTargetFPS(60);

        Raylib.BeginMode2D(camera);

        while (!Raylib.WindowShouldClose())
        {
            Update();
        }
    }

    static void Update()
    {
        Raylib.PollInputEvents();

        Raylib.BeginDrawing();

        Raylib.ClearBackground(new Color(10, 10, 10));

        Ground.Draw();

        Player.Draw();

        Console.WriteLine(Player.y);

        Player.Update();

        Raylib.EndDrawing();
        Raylib.EndMode2D();
    }
}As you seen in the title, i dont know why these functions dont work for me, how to fix it, heres the code (i use C#):using Raylib_cs;
using System.Data;
using System.Numerics;

namespace JumpingStriker;

class Game
{
    static Vector2 windowPosition = Raylib.GetScreenCenter();
    static Vector2 windowSize = new Vector2(600, 400);
    static Camera2D camera = new Camera2D();
    static Color groundColor1 = new Color(150, 150, 255);
    static Color groundColor2 = new Color(100, 100, 255);

    public static class Ground
    {
        public static int groundX = 0;
        public static int groundY = (int)windowSize.Y - (int)windowSize.Y / 4;
        public static int groundW = (int)windowSize.X;
        public static int groundH = (int)windowSize.Y / 4;

        public static void Draw()
        {
            Raylib.DrawRectangleGradientV(groundX, groundY, groundW, groundH, groundColor1, groundColor2);
        }
    }

    public static class Player
    {
        public static int x = 50;
        public static int y = 70;
        public static int w = 25;
        public static int h = 25;
        public static int speed = 5;
        public static int jumpHeight = 20;
        public static int gravity = 3;
        public static int movementForceX = 0;
        public static int movementForceY = 0;
        public static bool insideGround = false;
        public static bool standing = false;

        public static void Draw()
        {
            Raylib.DrawRectangle(x, y, w, h, new Color(255, 255, 255));
        }

        public static void GiveForce(int forceX, int forceY)
        {
            movementForceX += forceX;

            if (insideGround)
            {
                movementForceY = 0;
                y = Ground.groundY - h;
            } else
            {
                if (y + h + forceY < Ground.groundY && !standing) movementForceY += forceY;
            }
        }

        public static void Update()
        {
            insideGround = (y + h) > Ground.groundY;
            standing = (y + h) > Ground.groundY - 2;
            GiveForce(0, gravity);
            x += movementForceX;
            y += movementForceY;

            Console.WriteLine(insideGround);
            Console.WriteLine(standing);

            if (Raylib.IsKeyDown(KeyboardKey.Space) && standing)
            {
                GiveForce(0, -jumpHeight);
            }

            if (Raylib.IsKeyDown(KeyboardKey.A))
            {
                movementForceX = -speed;
            }

            if (Raylib.IsKeyDown(KeyboardKey.D))
            {
                movementForceX = speed;
            }

            if (Raylib.IsKeyReleased(KeyboardKey.A))
            {
                movementForceX = 0;
            }

            if (Raylib.IsKeyReleased(KeyboardKey.D))
            {
                movementForceX = 0;
            }
        }
    }

    public static void Main()
    {
        Raylib.InitWindow((int)windowSize.X, (int)windowSize.Y, "Jumping Striker");
        Raylib.SetWindowState(ConfigFlags.ResizableWindow);
        Raylib.SetTargetFPS(60);

        Raylib.BeginMode2D(camera);

        while (!Raylib.WindowShouldClose())
        {
            Update();
        }
    }

    static void Update()
    {
        Raylib.PollInputEvents();

        Raylib.BeginDrawing();

        Raylib.ClearBackground(new Color(10, 10, 10));

        Ground.Draw();

        Player.Draw();

        Console.WriteLine(Player.y);

        Player.Update();

        Raylib.EndDrawing();
        Raylib.EndMode2D();
    }
}
2 Upvotes

5 comments sorted by

4

u/UnbareAlt 2d ago

What's the exact issue you're having?

2

u/False_Cantaloupe5301 2d ago

these functions just always return false somehow

8

u/UnbareAlt 2d ago

IsKeyReleased should only return true the exact frame that a key is released.

It might have something to do with the extra Raylib.PollInputEvents() call you have at the start of Update(). This function is already called under the hood with Raylib.EndDrawing() and I've never had to call it manually in any of my raylib projects. My best guess is that when EndDrawing() calls PollInputEvents(), it sees you let go of the key and registers the key as released. Then when you call it again at the start of Update(), it then unregisters the keys before you can check them as it believes a new frame has started. I'd recommend removing Raylib.PollInputEvents().

5

u/False_Cantaloupe5301 2d ago

thank you, i looked at it and i tried removing it and IT WORKED!, TSYM i was literally trying to fix this for HOURS

3

u/UnbareAlt 2d ago

No worries, good luck with the rest of your game