I'm working on a personal project, and need the user to input an text, but, when i write an character, some times it write 2 or 3 insted of one, and of course, i tried to use sf::clock, it helps a litte bit, but dont solve the error, i'm trying to use the sf::window::setKeyTepeatEnabled(false), but seems to have no effect.
This is my Header file where i'm struggling with te problem :
#pragma once
#include <SFML/Graphics.hpp>
#include <string>
class TextBox {
public:
TextBox(int PosX, int PosY) {
Tbox.setSize(sf::Vector2f(500, 50)); Tbox.setOrigin(250, 25);
Tbox.setPosition(PosX, PosY);
Tbox.setOutlineThickness(1);
Tbox.setFillColor(sf::Color::Transparent); Tbox.setOutlineColor(sf::Color::White);
}
void Write(int MouseX, int MouseY, sf::Event& event) {
BebasNeue.loadFromFile("C:/Users/Felipe/source/repos/Render_Dog/Render_Dog/BebasNeue-Regular.otf");
int TPx = Tbox.getPosition().x;
int TPy = Tbox.getPosition().y;
Ui_text.setFont(BebasNeue);
Ui_text.setPosition(160, 577); Ui_text.setCharacterSize(35);
Ui_text.setString(User_Input);
Tbox.setOutlineColor(sf::Color::Red);
if (MouseX >= TPx - 250 && MouseY >= TPy - 25 && MouseX <= TPx + 250 && MouseY <= TPy + 25) {
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) Textbox_clicked = true;
}
else {
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) Textbox_clicked = false;
if (Textbox_clicked == true) Tbox.setOutlineColor(sf::Color::Red);
else Tbox.setOutlineColor(sf::Color::White);
}
if(Textbox_clicked == true) {
if (sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace) && Textbox_clicked
&& Text_Clock.getElapsedTime().asMilliseconds() > 70) {
std::string String_User_Input = (std::string)User_Input;
if (String_User_Input.size() == 0) {}
else {
User_Input.erase(String_User_Input.size() - 1, 1);
Text_Clock.restart();
}
}
if (event.type == sf::Event::TextEntered && !sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace)
&& Text_Clock.getElapsedTime().asMilliseconds() > 70 && Textbox_clicked ) {
User_Input += event.text.unicode;
Text_Clock.restart(); }
}
}
void Render(sf::RenderWindow& window) {
window.draw(Tbox);
window.draw(Ui_text);
}
private:
bool Textbox_clicked = false;
sf::Clock Text_Clock;
sf::RectangleShape Tbox;
sf::String User_Input;
sf::Text Ui_text; sf::Font BebasNeue;
};
And here is my main file :
#include <SFML/Graphics.hpp>
#include "Buttons.h"
#include "TextBox.h"
int main() {
// Render window
sf::RenderWindow window(sf::VideoMode(800, 800), "Render Dog"); window.setFramerateLimit(30.f);
// Not working
window.setKeyRepeatEnabled(false);
// Ui
Buttons B1(400, 400, sf::Vector2f(140, 70));
TextBox TB1(400, 600);
// BG gradient
sf::Vertex BG[4] = {
sf::Vertex(sf::Vector2f(0,-300), sf::Color::Black),
sf::Vertex(sf::Vector2f(800,-300), sf::Color::Black),
sf::Vertex(sf::Vector2f(800,9900), sf::Color::White),
sf::Vertex(sf::Vector2f(0,9900), sf::Color::White),
};
// Main loop
while (window.isOpen()) {
// Variaveis em Loop
sf::Event event;
int MouseX = sf::Mouse::getPosition(window).x;
int MouseY = sf::Mouse::getPosition(window).y;
// Loop de eventos
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) window.close();
}
// Funcoes
B1.colision(MouseX, MouseY);
TB1.Write(MouseX, MouseY, event);
// Renderizacao
window.draw(BG, 4 ,sf::Quads);
B1.render(window);
TB1.Render(window);
window.display();
window.clear();
}
return 0;
}