1

Why mouse speed is not changing after execution of the following program ?

Is it due to SPI_SETMOUSESPEED or due to unable to change the winini file by SPIF_UPDATEINIFILE , SPIF_SENDCHANGE and SPIF_SENDCHANGE parameters ?

Compiler : g++ , OS : Windows 8 .

#include <iostream>
#include <windows.h>
#include<winuser.h>
#pragma comment(lib, "user32.lib")

using namespace std ;

int main()
{
    int i = 0 , *MouseSpeed = &i ;

    bool x ;

//  Retrieving the mouse speed . 

    x = SystemParametersInfo( SPI_GETMOUSESPEED , 0 , MouseSpeed , 0 ) ;

    cout<<"\n\nPrevious Mouse Speed was : " << *MouseSpeed ;

    cout<<"\n\nSystemParametersInfo return status for SPI_GETMOUSESPEED : " << x ;

    if( x )
    {
        i = 20 ;

        MouseSpeed = &i ;

//  Changing the mouse speed .

        SystemParametersInfo( SPI_SETMOUSESPEED ,
                              0 ,
                              MouseSpeed ,
                              SPIF_UPDATEINIFILE ||
                              SPIF_SENDCHANGE ||
                              SPIF_SENDWININICHANGE ) ;

        cout<<"\n\nCurrent Mouse Speed is : " << *MouseSpeed ;

        cout<<"\n\nSystemParametersInfo return status for SPI_SETMOUSESPEED : " << x << "\n\n" ;
    }

    if( !x )        
        cout<< "Error Status : " << GetLastError() << "\n\n";

    return 0;
}
Biraj Bora
  • 888
  • 2
  • 12
  • 25
  • 1
    You're not alone, and it looks like someone likes my icon: http://stackoverflow.com/questions/9848423/c-systemparametersinfo-with-spi-setmouse-does-not-seem-to-change-cursor-spee?rq=1 – chris May 29 '13 at 12:23
  • 2
    What does SystemParametersInfo return (true or false) ? If it returns false, check the error number returned by GetLastError(), this may give you a clue. – Jabberwocky May 29 '13 at 12:24
  • I tried , but didn't worked. – Biraj Bora May 29 '13 at 12:59
  • @chris : Your SPI_SETMOUSE is working fine see... http://stackoverflow.com/a/16814574/2153564 – Biraj Bora May 30 '13 at 09:42
  • @birajbora, Huh. It was over a year ago and all I remember was trying something, seeing the numbers change, but not seeing the speed change. – chris May 30 '13 at 14:34

1 Answers1

7

You are passing the wrong value as pvParam for SPI_SETMOUSESPEED. From the documentation:

Sets the current mouse speed. The pvParam parameter is an integer between 1 (slowest) and 20 (fastest). A value of 10 is the default. This value is typically set using the mouse control panel application.

Compare that to the documentation for SPI_GETMOUSESPEED

Retrieves the current mouse speed. The mouse speed determines how far the pointer will move based on the distance the mouse moves. The pvParam parameter must point to an integer that receives a value which ranges between 1 (slowest) and 20 (fastest). A value of 10 is the default. The value can be set by an end-user using the mouse control panel application or by an application using SPI_SETMOUSESPEED.

So for SPI_GETMOUSESPEED you must pass an int* value as pvParam, but for SPI_SETMOUSESPEED you must pass in int value. You are passing an int* in both cases. Your call for SPI_SETMOUSESPED should be:

SystemParametersInfo(
    SPI_SETMOUSESPEED,
    0,
    (LPVOID) newMouseSpeed,
    SPIF_UPDATEINIFILE | SPIF_SENDCHANGE | SPIF_SENDWININICHANGE
);
Torbilicious
  • 467
  • 1
  • 4
  • 17
shf301
  • 31,086
  • 2
  • 52
  • 86
  • 1
    Don't use the `||` (LOGICAL OR) operator to combine flag bits. Use the `|` (BITWISE OR) operator instead. `SPIF_UPDATEINIFILE || SPIF_SENDCHANGE || SPIF_SENDWININICHANGE` evaluates as `true`, which is just `SPIF_UPDATEINIFILE` (0x01) by itself when converted to an integer. – Remy Lebeau Sep 13 '17 at 18:31