mikke89/RmlUi

Backspace button is not working for the input field.

Closed this issue · 2 comments

//main.cpp file

#include <iostream>
#include <string>
#include <RmlUi/Core.h>
#include <RmlUi/Debugger.h>
#include "ADDITONAL/RmlUi_Backend.h"

// Global/static variable to track reload requests
static bool reload_requested = false;

// KeyDownCallback for handling F5
bool HandleKeyDown(Rml::Context* context, Rml::Input::KeyIdentifier key, int /*modifier*/, float /*native_dp_ratio*/, bool priority) {
    if (key == Rml::Input::KI_F5 && priority) {
        reload_requested = true;
        return true; // Block further processing of F5
    }
    return false;
}

void ReloadDocument(Rml::Context* context, Rml::ElementDocument*& document, const std::string& path) {
    if (document) {
        document->Close(); // Close the current document
    }

    // Clear cached stylesheets to force reload
    Rml::Factory::ClearStyleSheetCache(); // <-- Add this line

    document = context->LoadDocument(path); // Reload from disk
    if (document) {
        document->Show();
    }
    else {
        Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to reload document!");
    }
}

struct MyData
{
    std::string title = "Hello World!";
    std::string animal = "dog";
    bool show_text = true;

} MyData;

bool SetupDataBinding(Rml::Context* context, Rml::DataModelHandle& my_model)
{
    Rml::DataModelConstructor constructor = context->CreateDataModel("my_model");
    if (!constructor)
    {
        return false;
    }
    constructor.Bind("title", &MyData.title);
    constructor.Bind("animal", &MyData.animal);
    constructor.Bind("show_text", &MyData.show_text);

    my_model = constructor.GetModelHandle();
    return true;
}

int main() {
    // Initialize backend first
    std::cout << "Initializing backend" << std::endl;
    if (!Backend::Initialize("RmlUi Demo", 1280, 720, true)) {
        std::cout << "Backend initialization failed" << std::endl;
        return 1;
    }

    // Set up core interfaces BEFORE initializing RmlUi
    Rml::SetSystemInterface(Backend::GetSystemInterface());
    Rml::SetRenderInterface(Backend::GetRenderInterface());

    // Initialize RmlUi
    if (!Rml::Initialise()) {
        return 1;
    }

    // Load fonts
    if (!Rml::LoadFontFace("assets/LatoLatin-Regular.ttf")) {
        Rml::Log::Message(Rml::Log::LT_ERROR, "Failed to load regular font!");
    }
    if (!Rml::LoadFontFace("assets/LatoLatin-Bold.ttf")) {
        Rml::Log::Message(Rml::Log::LT_WARNING, "Failed to load bold font!");
    }
    if (!Rml::LoadFontFace("assets/LatoLatin-Italic.ttf")) {
        Rml::Log::Message(Rml::Log::LT_WARNING, "Failed to load italic font!");
    }

    // Create context with explicit render dimensions
    Rml::Context* context = Rml::CreateContext("main", Rml::Vector2i(1280, 720));
    if (!context) {
        Rml::Log::Message(Rml::Log::LT_ERROR, "Context creation failed!");
        return 1;
    }

    // Initialize debugger AFTER context creation
    Rml::Debugger::Initialise(context);
    Rml::Debugger::SetVisible(true);  // Start with debugger visible for testing

    // Setup Data Binding
    Rml::DataModelHandle modelHandle;
    if (!SetupDataBinding(context, modelHandle))
    {
        std::cout << "Data model setup failed!" << std::endl;
        return 1;
    }

    // Load document
    std::cout << "About to load document" << std::endl;
    Rml::ElementDocument* document = context->LoadDocument("assets/demo.rml");
    if (!document) {
        std::cout << "Document load failed" << std::endl;
        Rml::Log::Message(Rml::Log::LT_ERROR, "Document load failed!");
        return 1;
    }
    document->Show();
    std::cout << "Document loaded successfully" << std::endl;

    // Main loop with proper error handling
    try {
        while (Backend::ProcessEvents(context, HandleKeyDown, true)) {
            context->Update();

            // Check if F5 was pressed
            if (reload_requested) {
                ReloadDocument(context, document, "assets/demo.rml");
                std::cout << 1 << std::endl;
                reload_requested = false;
            }

            std::cout << MyData.animal << std::endl;

            Backend::BeginFrame();
            context->Render();
            Backend::PresentFrame();
        }
    }
    catch (const std::exception& e) {
        Rml::Log::Message(Rml::Log::LT_ERROR, "Runtime error: %s", e.what());
    }

    // Cleanup
    Rml::Shutdown();
    Backend::Shutdown();

    return 0;
}

//demo.rml

<rml>
	<head>
		<title>My RmlUi Document</title>
		<link type="text/rcss" href="demo.rcss"/>
		<link type="text/template" href="template_demo.rml"/>
	</head>
	<body>
		<p>Hello World!</p>
		<div>
			<template src="basic">
				Another Paragraph!
			</template>
		</div>
		<h1>Simple data binding example</h1>
		<div data-model="my_model" class="class1">
			<h2>{{title}}</h2>
			<p data-if="show_text">The quick brown fox jumps over the lazy {{animal}}.</p>
			<input type="text" data-value="animal" data-input="update"/>
		</div>
	</body>
</rml>

##ISSUE##
The issue is that in the input field when ever i click the backspace the character is not erasing. And also whenever i click arrow keys also the cursor is not moving. Other than this all other keys(numbers, alphabets and special characters) are working. Please to help me to solve this issue.
I have also added a video so that u can understand what's the issue. At first i tried pressing the backspace its not working then i press some characters its working.

2025-03-26.16-58-46.mp4

Hey, and apologies for the late response, it's been some busy times for me.

I have been testing the code you posted, it was missing the style sheets and templates, but I managed to recreate the described issue eventually.

It turns out, the issue is in the HandleKeyDown function you posted. The values of the returned booleans are flipped. So this function was always blocking all keydown events (except F5). The solution is to return true from this function, which here indicates to propagate the event up to the next handler.

Let us know how that goes, hopefully this resolves the issue for you.

Closing this, as I believe it's been resolved, and due to inactivity.