Link to home
Start Free TrialLog in
Avatar of Troudeloup
Troudeloup

asked on

move mouse toward right side for 1 unit

I use this function to move mouse cursor to toward right side.

yet on different systems this produces varied mouse move speed, which is a problem.
how do I make the mouse cursor to travel at uniform speed regardless of system delays?


void mousemove_horizontal_line( HWND hwnd1, int x1, int x2, int y )
{
      POINT p;
      p.x = x1;
      p.y = y;
      ClientToScreen( hwnd1, &p );
      SetCursorPos( p.x, p.y );

      GetCursorPos(&p);
      ScreenToClient( hwnd1, &p );
      
      //cout << p.x << endl;      
      
      while ( p.x < x2 )
      {
            GetCursorPos(&p);
            ScreenToClient( hwnd1, &p );
            
            mouse_event( MOUSEEVENTF_MOVE, 1, 0, 0, 0 );
      
            //Sleep ( 10 );
            if ( ( p.x % 5 ) == 0 )
            {
                  Sleep ( 1 );
            }
      }
      
}
Avatar of ovule
ovule

I'm not a Windows programmer so probably can't help you.  What do you mean by different systems though?
Avatar of Troudeloup

ASKER

different computer, box.

I have several computers and they have different speed, and I don't want something as trivial as simulating mouse movement dependent on system speed, which makes no sense.
or maybe it doesn't have to be something that use windows to help time things efficiently,

maybe something else in generic c++?

i just need to wait a little bit of time between things
given this


            if ( ( p.x % 5 ) == 0 )
            {
                  Sleep ( 1 );
            }
 



part is what I use to optional sleep ( right now it sleeps for 1milisecond when the x value is divisible by 5)



what if I use another method to do the same thing as sleep(1)

to rephrase myself,

I use an for loop to move mouse position toward right until target x has been reached
and to slow it down so that it fits my need, I use sleep every time when x value's modulo is 0.

for some reason on a slower machine, this part of code of mouse movement  proves to run very slowly.

why does it do that? i expect sleep (1) to be the same on all processors, but this is not the case.
is it the way i used it? or should I use something else to to move mouse movement go slower?

            mouse_event( MOUSEEVENTF_MOVE, 1, 0, 0, 0 );
     
            //Sleep ( 10 );
            if ( ( p.x % 5 ) == 0 )
            {
                  Sleep ( 1 );
            }
Why to do all this?
Just use the WM_MOUSEMOVE event and wait the operating system to tell you when mouse moves, then continue process the message as you want based on mouse position.
ok, so I wait until system reports that the mouse has indeed moved a tick, for instance.


what if I want the mouse to move right for 150 pixels, yet i want to control how fast it goes?

mouse_event( MOUSEEVENTF_MOVE, 1, 0, 0, 0 );


or rather, instead of moving 1 pixel at a time and manually use some sort of a way to slow it down,

is there a way to do

 mouse_event( MOUSEEVENTF_MOVE, 150, 0, 0, 0 );

and then tell it how fast to move in the first place?
Avatar of jkr
Um,

POINT pt;

GetCursorPos(&pt);

pt.x++; // one pixel to the right

SetCursorPos(pt.x,pt.y);

is probably what you are looking for.
i ought to use that other way or equivalent, because it's not to move the mouse to a point, it's to move across a line.
Well, to move it over a line, you could

GetCursorPos(&pt);

for (int i = 0; i < 100; ++i) { // move 100 pixels to the right

    SetCursorPos(pt.x + i,pt.y);
}
Forget all that.

If you want uniform movement on any computer, you'll need to add a time element.

Movement would be X pixels over Y time...

So set up a timer (WM_TIMER; you can find this anywhere) and then have the timer proc move your cursor.

You enable/disable the timer whenever you need the mouse cursor moved.

Pseudo code is:

Get my mouse Current XY
Determine my desination XY

How long do I want that to take?   10 seconds? 2 seconds?

Delta X = DestX - CurrentX
Delta Y = DestY - CurrentY

Timer XY = Divide delta XY by the elapsed time....

set up the timer

Add TimerXY to the currentXY of the mouse
when you get to you destination, turn off the timer event.

-j
can you show me a working sample?

I have no idea how to syntaxtually use wm_timer and how to implement it for this instance.
>>Forget all that.

Really?

GetCursorPos(&pt);

for (int i = 0; i < 100; ++i) { // move 100 pixels to the right

    Sleep(100);
    SetCursorPos(pt.x + i,pt.y);
}
yes seriously, it's just one of those programming must thing i guess

the game doesn't like instant moves, it takes only movement


how do I use windows timer?

i think it's more accurate than sleep()
The windows timer and all the details around it are in that link I gave you.

Have you checked it out?
If you are using a console application, you cannot use timers, you need a message loop for that to work. And no, timers are not more accurate than 'Sleep()'.
The multimedia timers are more accurate than sleep();  If I remember right, sleep() was accurate to 16hz, and the Multimedia Timers are accurate to 1000Hz.



damn, no wm_timer with console application.

ok, i have to look into this one.
This is a console app?

Is this .Net?  

DOS based?

How are you writing this?

-j
i am using mingw to write a windows console app.

Ok, cool.

Your console application can still have a message loop; what you don't have is a WinMain.

I see references to RegisterSysMsgHandler()... see if it's in your headers.

-john
>>Your console application can still have a message loop;

Err, no. The message loop for a console app is "hidden" by the OS.
Yeah... but the message pump I'm referring to isn't the standard one....

RegisterSysMsgHandler() allows a console app to install a message callback.
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Maybe.

I'm reading... the call i was talking about was published in the WinCE api... but I understood that it made it into the standard win32 api as well.

This might be the same thing... the "usage" and the signature of the method look right

-j