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

Author Topic: simulating mouseClick  (Read 2694 times)

0 Members and 1 Guest are viewing this topic.

andrei186

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
simulating mouseClick
« on: January 20, 2024, 07:48:53 pm »
I am trying to develop an application to work with the mouse coursor controlled by laser pointer (a webcam scanning the screen, a program scanning frames in search of laser dot, calculates its coordinated and simulates mouse click in these coordinates).

I am coding it in MSVS Community 2019 C++ and SFML.
One of the things I am trying to do is to place a sprite (just a red circle) into a certain place (x,y) which is different for every click using:

if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
                spriteSircle.setPosition(x, y); //x and y are calculated earlier in the program
        }

To simulate mouseClick the laser pointer is set to generate a short (50 msec) laser pulse.

With the standard mouse it works fine.
Yet with  the laser pointer two things do happen very often:
1. It misses laser clicks
2. It places the red circle at the coordinates of the PREVIOUS place. I.e. where it should have been places at the previous click

Where might be the error? It might be with the program which scans the frames and simulates the mouseclick, but I have no access to its code, and therefore need to make sure that the problem is not in my code.
« Last Edit: January 21, 2024, 09:31:45 pm by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: simulating mouseClick
« Reply #1 on: January 21, 2024, 09:38:38 pm »
How is the mouse simulated?

sf::Mouse::isButtonPressed does a hardware/system call to poll the mouse state and it will only do the check at the time the function is called, so if you render at 60fps, that's then once every 16ms.
As such there could be two potential failure modes. One the poll of the mouse state doesn't actually work for a simulated click. Or two, the "click" is too short and the window between mouse state fetching and mouse click keep missing each other most of the time.

I recommend to use events instead.

Whether that's the actually issue, I don't know, but we don't exactly have a lot to go on. :D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

andrei186

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: simulating mouseClick
« Reply #2 on: January 22, 2024, 10:41:39 am »
thank you very much for your comment.
I did not understand your question "How is the mouse simulated?"
I thought "mouse simulated" means using relevant function. In my case Mouse::isButtonPressed

From your question I suspect that apart from using  function like Mouse::isButtonPressed or event, mouse simulation requires more than just using function or event - is this the case?

PS. I tried to use C++ built-in function GetAsyncKeyState(). For some reason it even in the simplest test-code for Left Click like

int main()
{
   Sleep(100);
   if(GetAsyncKeyState(0x01))
        {
                cout<<"mouse"<<endl;
        }
}
 
it behave not as expected (no mater which integer I put inside  Sleep();
But for the Right button GetAsyncKeyState(0x02) works fine

 

anything