Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can i find mouse speed in c#
i need to have my mouse speed in C# please help me .
Posted
Updated 14-Feb-18 17:22pm
Comments
Richard MacCutchan 28-Feb-14 4:36am    
The mouse speed depends on the dexterity of the user.
SoMad 28-Feb-14 21:21pm    
:-D Good one.
BillWoodruff 28-Feb-14 9:11am    
Do you understand how mouse-speed is measured ?

"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."

http://msdn.microsoft.com/en-us/library/windows/desktop/ms724947(v=vs.85).aspx

You can find this in namespace System.Runtime.InteropServices. Below code is the demonstration to manipulate mouse speed in C#:
C#
using System;
using System.Runtime.InteropServices;

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

        [DllImport("User32.dll")]
        static extern Boolean SystemParametersInfo(
            UInt32 uiAction,
            UInt32 uiParam,
            UInt32 pvParam,
            UInt32 fWinIni);

        static void Main(string[] args)
        {
            SystemParametersInfo(
                SPI_SETMOUSESPEED,
                0,
                uint.Parse(args[0]),
                0);
        }
    }
}
 
Share this answer
 
Comments
neymar1107 28-Feb-14 4:48am    
can you help me how can i save it on int number .
BillWoodruff 28-Feb-14 9:09am    
+4 Great start to answering the OP's question. Just add showing how to use SPI_GETMOUSESPEED to get the current mouse speed setting, and you have my 5 :)
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1
{
    class Program
    {
        public const UInt32 SPI_GETMOUSESPEED = 0x0070;


        const UInt32 SPIF_UPDATEINIFILE = 0x01;
        const UInt32 SPIF_SENDWININICHANGE = 0x02;

        [DllImport("User32.dll")]
        static extern Boolean SystemParametersInfo(
            UInt32 uiAction,
            UInt32 uiParam,
            IntPtr pvParam,
            UInt32 fWinIni);
        
        static unsafe void Main(string[] args)
        {
         /*   int speed;
            SystemParametersInfo(SPI_GETMOUSESPEED, 0, new IntPtr(&speed), 0);
            Console.WriteLine(speed);

            */

           MouseOptions m = new MouseOptions();

            MouseOptions.GetDefaults();
            int speed;
            SystemParametersInfo(SPI_GETMOUSESPEED, 0, new IntPtr(&speed), 0);
            Console.WriteLine(speed);

            MouseOptions.SetDefaults();


            SystemParametersInfo(SPI_GETMOUSESPEED, 0, new IntPtr(&speed), 0);
            Console.WriteLine(speed);
            

            Console.ReadLine();
        }



        public class MouseOptions
        {
            [DllImport("user32.dll")]
            public static extern int SystemParametersInfo( int uAction, int uParam, IntPtr lpvParam, int fuWinIni );

            [DllImport("kernel32.dll")]
            public static extern int GetLastError();
     
            public const int SPI_GETMOUSESPEED = 112;
            public const int SPI_SETMOUSESPEED = 113;


            private static int intDefaulSpeed = 10;
            private static int intCurrentSpeed;
            private static int intNewSpeed;

            public static void GetDefaults()
            {
                intCurrentSpeed = GetMouseSpeed();
            }
            public static void SetDefaults()
            {
                if ( intCurrentSpeed == 20 )
                {
                    SetMouseSpeed(intDefaulSpeed); 
                }
                else if ( intCurrentSpeed < 10 )
                {
                    SetMouseSpeed(intDefaulSpeed);   
                }
            }

            public static int GetMouseSpeed()
            {
                int intSpeed = 0;
                IntPtr ptr;
                ptr = Marshal.AllocCoTaskMem(4);
                SystemParametersInfo(SPI_GETMOUSESPEED, 0, ptr, 0);
                intSpeed = Marshal.ReadInt32(ptr);
                Marshal.FreeCoTaskMem(ptr);

                return intSpeed;
            }

            public static void SetMouseSpeed( int intSpeed )
            {
                IntPtr ptr = new IntPtr(intSpeed);

                int b = SystemParametersInfo(SPI_SETMOUSESPEED, 0, ptr, 0);

                if (b == 0)
                {
                    Console.WriteLine("False");
                }
                else if ( b == 1 )
                {
                    Console.WriteLine("true");
                }

            }
            
        }


    }

}
C#

 
Share this answer
 
Comments
CHill60 15-Feb-18 5:15am    
Question was asked and solution accepted 4 years ago. If you are going to answer old posts (and I suggest that you don't) then make sure you are bringing something new to the thread (you're not) and that you explain your solution. Don't just dump code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900