Link to home
Start Free TrialLog in
Avatar of learn
learn

asked on

speed of the mouse cursor

Hi,

I would like to set a scale for the speed of the mouse cursor.
I have searched in EE and found two ways to do that. One of them is commented as not very good: can only slow down 50% speed; the other one looks nobody put comments....but that is use SetCursorPos() API function within MouseMove event. I have found SetCursorPos() can raise the MouseMove event! That means infinite call.....

So,

Can you tell me how to set a scale for the speed of the mouse cursor?

Thank you in advance.
Avatar of tipi
tipi

Hi make a form and place a slider and command button there. With this code you can set 7 different mouse speeds, like in control panels mouse options.
When the form loads it reads the current speed and sets the slider marking it.

Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Private Const SPI_GETMOUSE = 3
Private Const SPI_SETMOUSE = 4
Private Const SPIF_SENDWININICHANGE = &H2
Private Const SPIF_UPDATEINIFILE = &H1
Dim m_speed(0 To 2) As Long

Private Sub Command1_Click()
SystemParametersInfo SPI_SETMOUSE, 0&, m_speed(0), SPIF_SENDWININICHANGE
End Sub

Private Sub Form_Load()
SystemParametersInfo SPI_GETMOUSE, 0&, m_speed(0), 0&
Dim sum As Long
sum = m_speed(0) + m_speed(1) + m_speed(2)
Select Case sum
Case 0
Slider1.Value = 0
Case 11
Slider1.Value = 1
Case 8
Slider1.Value = 2
Case 5
Slider1.Value = 3
Case 18
Slider1.Value = 4
Case 15
Slider1.Value = 5
Case 12
Slider1.Value = 6
End Select
End Sub

Private Sub Slider1_Scroll()
Select Case Slider1.Value
Case 0
m_speed(0) = 0
m_speed(1) = 0
m_speed(2) = 0
Case 1
m_speed(0) = 10
m_speed(1) = 0
m_speed(2) = 1
Case 2
m_speed(0) = 7
m_speed(1) = 0
m_speed(2) = 1
Case 3
m_speed(0) = 4
m_speed(1) = 0
m_speed(2) = 1
Case 4
m_speed(0) = 4
m_speed(1) = 12
m_speed(2) = 2
Case 5
m_speed(0) = 4
m_speed(1) = 9
m_speed(2) = 2
Case 6
m_speed(0) = 4
m_speed(1) = 6
m_speed(2) = 2
End Select
End Sub
Avatar of learn

ASKER

Hi tipi,

I have just tested your code....it can work but, as I said in my question, it can only slow down the speed by 50% (I think this is the one I found in EE).
Can you make the cursor slow down in any scale...well, can not any, but at lease 5 times slow than the normal? Can you do something in the m_speed() array? Whe you have set the speed, m_speed(2), to 0 but still has got 50% speed?

Thanks a lot anyway.
I will check the code and try to find out the logic of the three elements on the array. I'll be back later.(I can also find out answer by using direct x if U want to (and give the source too).If U are using direct x the mouse speed is only valid the time your app is running.
Avatar of learn

ASKER

Hi tipi,

I think your code can not get slower than 50% :-(
I have tried to set the slowest speed by hand in the window system and then run your code to check the setting in FormLoad event. m_speed(0),(1),(2) are 0, the same as your code when settimg the slowest speed!

So, we need to find something else....or we agree that there is no way to let the speed down further :-(
There is a direct way.
Use SystemParametersInfo function with first parameter = SPI_SETMOUSE

But before that you have to understand mouse concept. Read below. After that read how to use systemparametersinfo function from win32.hlp (I'm sure you can find this find in your delphi directory)

Relative mouse motion is subject to the effects of the mouse speed and the two mouse threshold values. In Windows NT, an end user sets these three values with the Mouse Tracking Speed slider of Control Panel's Mouse option; in Windows 95, an end user sets them with the Pointer Speed slider of the Control Panel's Mouse property sheet. An application obtains and sets these values with the SystemParametersInfo function.

The operating system applies two tests to the specified relative mouse motion. If the specified distance along either the x or y axis is greater than the first mouse threshold value, and the mouse speed is not zero, the operating system doubles the distance. If the specified distance along either the x or y axis is greater than the second mouse threshold value, and the mouse speed is equal to two, the operating system doubles the distance that resulted from applying the first threshold test. It is thus possible for the operating system to multiply relatively-specified mouse motion along the x or y axis by up to four times.

If I have time, I will show you some example.
Avatar of learn

ASKER

Hi Tussin,

Thank you for your document :-)

You should use the same time you spent on writng the document to show me some code :-)

I will give points (>100) to each experts who can show me different code that slows down the cursor speed to 4 times.

Cheers.
this is fast one.

  MouseInfoArray[0]:=4;
  MouseInfoArray[1]:=6;
  MouseInfoArray[2]:=2;
  if not SystemParametersInfo(SPI_SETMOUSE,0,@MouseInfoArray[0],0) then
    showmessage('Error');

this is medium one

  MouseInfoArray[0]:=4;
  MouseInfoArray[1]:=0;
  MouseInfoArray[2]:=1;
  if not SystemParametersInfo(SPI_SETMOUSE,0,@MouseInfoArray[0],0) then
    showmessage('Error');

and this is slow one

  MouseInfoArray[0]:=0;
  MouseInfoArray[1]:=0;
  MouseInfoArray[2]:=0;
  if not SystemParametersInfo(SPI_SETMOUSE,0,@MouseInfoArray[0],0) then
    showmessage('Error');
to make a scale just use progressbar to control these 3 variables.
sorry, I forgot to put the declaration for MouseInfoArray

var MouseInfoArray:array [0..2] of Integer;
Avatar of learn

ASKER

Hi Tussin,

What....Delphi.....Here is VB :-)
And, your MouseInfoArray[0..2] is m_speed(0 To 2) from tipi's code and all your fast, medium and slow ones have been included in tipi's code :-)

So, you can only slow down the speed by 50%....:-(

Thenk you anyway.
http://msdn.microsoft.com/library/psdk/sysmgmt/sysinfo_4p67.htm

There is info about spi_setmousespeed and spi_getmousespeed parameters.(works only in 98 & 2000)
the constant of the spi_getmousespeed is 112 and
spi_setmousespeed is 113 (what a job to find,huh)
this accepts values from 1 to 20 , so it should be better than the earlier I
mentioned. Test it, let me know if it helped.
Ooppp!!,
       I answered in wrong language haha. Sorry about that, just seen that my code is exactly the same as tipi.
       The different between speed fastest and the slowest is 4 times I think.
      Sorry for mistake, Maybe because I answered too many topics and at very very late night.

Tussin.
Avatar of learn

ASKER

Hi tipi,

I have tried that in a Windows98 PC, but not word....I checked several times...getmousespeed=112 looks works but setmousespeed=113 not!
I hope that is not 113 :-)

mycode:

Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" _
(ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Private Const SPI_SETMOUSEspeed = 112 'Windows98
Private Const SPI_GETMOUSEspeed = 113
Private Const SPIF_SENDWININICHANGE = &H2
Private Const SPIF_UPDATEINIFILE = &H1

Private oldSpeed As Integer, currentSpeed As Integer

.........
SystemParametersInfo SPI_GETMOUSEspeed, 0&, oldSpeed, SPIF_SENDWININICHANGE
SystemParametersInfo SPI_SETMOUSEspeed, 0&, currentSpeed, SPIF_SENDWININICHANGE

Regards
I checked the code too and to setmouse is not working ! hmm, I red the sdk and verified that setmousespeed value is 113 and getmousespeed value is 112. With getmousespeed I always get 10,which is the default value.(it does not change even if I change the values in control panel, which indicates it is totally different function) I'll try to find out if there is some switch that has to be invoken also, before the  statement is validated. I try to find some extra info and return then to the subject.(We shall solve this problem, that is for sure)

"I'll be back"
Avatar of learn

ASKER

Hi tipi,

It said, the parameter is a pointer to an integer with 1 - 20....do you think we should not set that as an integer type? How to response a pointer in VB? I also tried to use "Dim currentSpeed(0) as Integer" and got the same result :-(
Avatar of learn

ASKER

Hi tipi,

If you still not come back, I think it is something wrong with microsoft :-)
ASKER CERTIFIED SOLUTION
Avatar of tipi
tipi

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
Avatar of learn

ASKER

Hi tipi,

Really? You found the solution? Sound correct....I will test it later when I got the Windows98 PC.....Hopefully it reduces down the speed at lease 4 times....Let we see....

Cheers.
More than 4 times, the cursor is so slow when setting the speed to 1.
Just remember to change the ByRef lpvParam to ByVal lpvParam !
(Using Microsoft apis you have to change their own declarations to make the function work...well I am use to that when programming with directx 7, but still it is very annoying )
If you have problems , just mail me, I'll send you some source code to play with the mouse speed
email: tipi@iobox.fi
Avatar of learn

ASKER

Hi tipi,

I have tested what you said and, yes, the cursor became very very slow with the setting = 1. That is what I need and your comment will become an excellent answer.
I am not happy with the fact that we need to change the declare and need to put Get and Set in seperate module....Is that the design from Microsoft? Or it is some bug but you play some clever job to overcome it?
It seems to me that the API plays something different from setting the speed by hand and the API looks more strong.

Cheers.