Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Mouse detected when using sf::Event::TextEntered  (Read 3456 times)

0 Members and 1 Guest are viewing this topic.

nickklis

  • Newbie
  • *
  • Posts: 4
    • View Profile
Mouse detected when using sf::Event::TextEntered
« on: November 06, 2023, 08:13:42 am »
Hello people.

I am having issues I am trying to create an in game console which works but every time the mouse crosses the screen it activates and repeats letters that are held down
here's the code

if (event.type == sf::Event::TextEntered)
{
        if (test_ == false)
        {
                std::cout << "pass";           
        }
        test_ = true;
}
else
{
        test_ = false;
}

So it supposed to detect the letter once but if you hold down the letter and move the mouse it repeats the letter which is sub optimal

What I need is for sf::Event::TextEntered to ignore the mouse
« Last Edit: November 06, 2023, 09:03:22 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Mouse detected when using sf::Event::TextEntered
« Reply #1 on: November 06, 2023, 09:04:54 am »
Make sure you only process events within the event loop and to always correctly check the event type.

Since your code doesn't show how you display letters, nor shows the general structure of the code, I can only guess what went wrong.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

nickklis

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Mouse detected when using sf::Event::TextEntered
« Reply #2 on: November 06, 2023, 02:54:49 pm »
#include <iostream>
#include <SFML\Graphics.hpp>
#include <sstream>
#include <conio.h>


int screen_width = 640, screen_height = 480;

sf::RenderWindow window(sf::VideoMode(screen_width, screen_height), "SFML works!", sf::Style::Default);





sf::Event event;
bool  test_ = false;
void GETCH()
{

       

        if (event.type == sf::Event::TextEntered)
        {
                if (test_ == false)
                {
                        std::cout << "pass";
                       
                }
                test_ = true;
        }
        else
        {
                test_ = false;
        }


       

}



int main()
{
       
        std::stringstream ss;
       

        while (window.isOpen())
        {


                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();



                       
                }
                window.clear(sf::Color(0, 0, 0, 255));
               

                GETCH();
               
                window.display();
               

        }

        return 0;
}


There you go friend copy paste.
As I said before I'm if you hold down a key and move the mouse over the screen it starts to skip the function and repeat the letters.

1 Type the code into a compiler
2 compile
3 hold down any key to so that "pass" shows in the compiler only once
4 now here's the tricky part GRAB THE MOUSE AND MOVE IT ACCROSS THE SCREEN
5 pass will show up REPEATEDLY while you are holding the button
6 It's super simple, it's a glitch, and the problem is that if I want to have both keyboard and mouse I either have to cash the entire library and find a different one, or modify the library and delete the mouse function and use a third party mouse detection but probably the former.
« Last Edit: November 06, 2023, 03:49:42 pm by eXpl0it3r »

nickklis

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Mouse detected when using sf::Event::TextEntered
« Reply #3 on: November 06, 2023, 03:02:15 pm »
If I move getch into the while pollevent it seems to work holycrap
anywho Im late for work if
I'll confirm later if any problems arrive

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Mouse detected when using sf::Event::TextEntered
« Reply #4 on: November 07, 2023, 09:51:36 am »
Events objects are only really valid inside the event loop or more specifically after you've called pollEvent, so you need to make sure to always use events just within the event loop
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Mouse detected when using sf::Event::TextEntered
« Reply #5 on: November 07, 2023, 12:24:51 pm »
Separate from your issue but move your sf::RenderWindow into the main function.

It's a heavy SFML resource and they often don't work well being global (for technical reasons) so I think it's worth doing that now rather than having to try to work out why you're getting difficult to find issues later on.

- Simplest way to do that is move the object into the main function and pass it to each function that uses it by either reference or points.
- You could also have inside a class but that class object should also be created within main (or a function called from main)
- Another way you could do is to use a global pointer (similar to how you have now but a pointer) and then create the object from within main function.
I'd recommend the first (simple) way.



Aside from the suggestion, I'm glad you got your code working. The reason it was 'being weird' is that the sf::Event that is getting tested inside GETCH during the normal loop (even when there are no events) is no longer valid; that is, it always still has the contents of the previous event so your code thinks it is constantly 'being pressed'. A mouse event would change its contents causing GETCH to set "test_" to false meaning that it would then able to reset the "test_" to true and output "pass". Note also that - even when pressing a key that can create an sf::TextEntered event - an event other than sf::TextEntered can be created (an sf::KeyPressed event usually accompanies an sf::TextEntered event, for example).
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything