Changing Mouse Pointer Speed in C#

Posted on: 09 October 2008

I've just got myself a fancy new mouse (OK - not really all that fancy, but I like it). There's just one small hitch - it's much more sensitive than the touchpad on my laptop, so whenever I switch between the two, I have to fire up control panel, choose the mouse panel, choose the right tab, slide the slider, click the "OK" button, and it all just seems not worth all the effort.

There has to be an easier way than this, and indeed there is. All the control panel applet does is call the Win32 API, so why don't I?

A quick search found the SystemParametersInfo API call:

BOOL WINAPI SystemParametersInfo(
  __in     UINT uiAction,
  __in     UINT uiParam,
  __inout  PVOID pvParam,
  __in     UINT fWinIni

and another quick search found the values for the uiAction constants:

public const uint SPI_SETMOUSESPEED = 0x0071;

A little bit of C# to glue it all together:

using System;
using System.Runtime.InteropServices;

namespace MouseSpeedSwitcher
    class Program
        public const UInt32 SPI_SETMOUSESPEED = 0x0071;

        static extern Boolean SystemParametersInfo(
            UInt32 uiAction, 
            UInt32 uiParam, 
            UInt32 pvParam,
            UInt32 fWinIni);

        static void Main(string[] args)

and a couple of tasks on my start menu to set it to the right speed for each device. Happy mousing once again!