Skip to content

Instantly share code, notes, and snippets.

@christophberger
Created February 5, 2015 21:47
Show Gist options
  • Save christophberger/0eb5f12e3d6638c5f32d to your computer and use it in GitHub Desktop.
Save christophberger/0eb5f12e3d6638c5f32d to your computer and use it in GitHub Desktop.
Switch Mouse Speed in Windows 7 (Go source)
/*A command-line tool for setting the mouse speed on Windows machines.
This comes in handy when using different mouses (e.g. at work and at home)
that require different speed settings.
Dependencies: sys package (go get golang.org/x/sys/...)
License:
Copyright (c) 2015, Christoph Berger <code@christophberger.com>
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
"fmt"
"os"
"strconv"
"unsafe"
"golang.org/x/sys/windows"
)
const (
spiGetMouseSpeed uint64 = 0x70
spiSetMouseSpeed uint64 = 0x71
spifSendChange uint64 = 2
defaultSpeed uint64 = 10
)
func main() {
var err error
var oldspeed uint64
newspeed := defaultSpeed
if len(os.Args) > 1 {
newspeed, err = strconv.ParseUint(os.Args[1], 0, 64)
}
if err != nil || newspeed < 1 || newspeed > 20 {
panic("Error reading parameter. Please pass an integer between 1 an 20.")
}
user32 := windows.MustLoadDLL("user32.dll")
spi := user32.MustFindProc("SystemParametersInfoA")
ret, _, err := spi.Call(uintptr(spiGetMouseSpeed), uintptr(0), uintptr(unsafe.Pointer(&oldspeed)), uintptr(0))
// err is always non-nil - check the return value instead.
if ret == 0 {
panic("Error calling SystemParametersInfo: " + err.Error())
}
ret, _, err = spi.Call(uintptr(spiSetMouseSpeed), uintptr(0), uintptr(newspeed), uintptr(spifSendChange))
if ret == 0 {
panic("Error calling SystemParametersInfo: " + err.Error())
}
fmt.Printf("Changed mouse speed from %d to %d", oldspeed, newspeed)
}
@christophberger
Copy link
Author

This Go application allows to change mouse speed on Windows 7 via command line. Useful if you use two different mice on the same machine (for example, if you have a laptop machine that you use at work and at home) whose sensors have different dpi settings. Use a hotkey tool (such as autohotkey) to assign two hotkeys to two different speed settings. Then you can quickly switch between two mice with a keypress!

To build:

  • Get the Go compiler
  • Download the file
  • Compile:
    go build winsetmousespeed.go

Execute the binary in command prompt (or from a hotkey tool):

winsetmousespeed 7

Valid values are between 1 and 20.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment