Change mouse speed or dpi on keyboard hold Topic is solved

Ask gaming related questions
Mailman

Change mouse speed or dpi on keyboard hold

05 Jul 2016, 13:20

Hi,

this problem is driving me crazy. Maybe someone can help.
I would like to have the following: While holding the right Alt Button, the mouse speed (windows speed or dpi. doesn't matter) should be at a certain (lower) level. When releasing the button, it should go back to regular speed.
I got used to this when playing quake (back in the day) where you could simply bind such a script within the game config. Now I'm playing Overwatch and am trying to find a method to do the same.

There is a "solution" that I found but which isn't adequate. Some people suggested using autohotkey in combination with a program called "mouse speed switcher". However, this is useless since moue speed switcher takes a couple of milliseconds to switch settings which is unacceptable for a fast paced shooter.
Another problem I ran into was that I would like to use the right Alt button (Alt Gr) for this. Autohotkey handles this key as activated on release. Of course I need it to be activated on press (and hold). It says to put in the "~prefix" but somehow that didn't change the keys behavior or I did it wrong.

I tried using autohotkey for a workaround: My mouse (Logi MX303) has such a dpi switching function but it can only be bound to mouse buttons. So I can use e.g. one of the thumb buttons to switch dpi while holding it. This works fine but I want (need) it on a keyboard key.
So, I thought: "Hey, easy. Bind the dpi switch onto mouse4 and create a Autohotkey script that binds Mouse4 to Alt Gr. Well... no... In Autohotkey Mouse 4 and 5 are called "Xbutton1" and "XButton2". When I try to bind them to Alt Gr only their regular function (forward/backward) is activated when pressing Alt Gr instead of the dpi switch that I bound to the mouse button in the Logitech Driver. Autohotkey doesn't seem to be able to actually mimic the real mouse button pressing.

So... any ideas? I'm going nuts here.

Thx
GeminiContractor
Posts: 15
Joined: 03 Jul 2016, 10:30

Re: Change mouse speed or dpi on keyboard hold  Topic is solved

05 Jul 2016, 16:46

I am not sure if it is what you want since it toggles, and you might have tried it, but there is a script to slow down the mouse here: https://autohotkey.com/board/topic/1353 ... entry90991

Code: Select all

; Autohotkey script "Toggle Mouse sensitivity"
;=================================================================================
SlowMouseSpeed      := 1
NormalMouseSpeed    := true ; State of Mouse pointer speed
UserMouseSpeed      := 0    ; Speed sensed before slow down
MouseThreshold1     := 6
MouseThreshold2     := 10
MouseEnhance        := 1

SPI_GETMOUSESPEED   := 0x70
SPI_SETMOUSESPEED   := 0x71
SPI_SETMOUSE        := 0x04 

;=================================================================================
*RAlt::   toggleMouseSpeed()

;=================================================================================
toggleMouseSpeed() {
    global
    ; SET LOW SPEED
    if( NormalMouseSpeed )
    {
        ; SENSE BEFORE
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,prevSpeed, UInt,0)

        ; Temporarily reduces the mouse cursor's speed.
        ; Retrieve the current speed so that it can be restored later
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,UserMouseSpeed, UInt,0)
        ; Slow down mouse speed
        DllCall("SystemParametersInfo", UInt,SPI_SETMOUSESPEED, UInt,0, UInt,SlowMouseSpeed, UInt,0)

        ; SENSE AFTER
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,currentSpeed, UInt,0)
        ToolTip, Mouse slow: %currentSpeed%/20

        ; REMEMBER CURRENT STATE
        NormalMouseSpeed := false
    }
    ; RESTORE SPEED
    else {
        ; SENSE BEFORE
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,prevSpeed, UInt,0)

        ; Restore the original speed.
        DllCall("SystemParametersInfo", UInt, SPI_SETMOUSESPEED, UInt,0, UInt,UserMouseSpeed, UInt,0)

        ; Restore the original speed acceleration thresholds and speed
        VarSetCapacity(MySet, 32, 0) 
        InsertInteger(MouseThreshold1, MySet, 0)
        InsertInteger(MouseThreshold2, MySet, 4)
        InsertInteger(MouseEnhance   , MySet, 8)
        DllCall("SystemParametersInfo", UInt,SPI_SETMOUSE, UInt,0, Str,MySet, UInt,1) 

        ; SENSE AFTER
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,currentSpeed, UInt,0)
        ToolTip, Mouse restored: %currentSpeed%/20

        ; REMEMBER CURRENT STATE
        NormalMouseSpeed := true
    }
    SetTimer, RemoveToolTip, 1000
}
;=================================================================================
InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4) {
    ; Copy each byte in the integer into the structure as raw binary data. 
    Loop %pSize%
    DllCall("RtlFillMemory", "UInt",&pDest + pOffset + A_Index-1, "UInt", 1, "UChar", pInteger >> 8*(A_Index-1) & 0xFF) 
}
;=================================================================================
RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return

;=================================================================================

Mailman

Re: Change mouse speed or dpi on keyboard hold

05 Jul 2016, 17:37

Not bad but... not quite. Apart from being a toggle, it doesn't work in Overwatch. Even though I put in a "#IfWinactive, Overwatch" (is that correct?). When I press Alt Gr in Overwatch, the script doesn't work but throws me into windows mode of Overwatch.

The script works as advertised in Windows though.
Any advice?
GeminiContractor
Posts: 15
Joined: 03 Jul 2016, 10:30

Re: Change mouse speed or dpi on keyboard hold

05 Jul 2016, 18:49

Mailman wrote:Not bad but... not quite. Apart from being a toggle, it doesn't work in Overwatch. Even though I put in a "#IfWinactive, Overwatch" (is that correct?). When I press Alt Gr in Overwatch, the script doesn't work but throws me into windows mode of Overwatch.

The script works as advertised in Windows though.
Any advice?
I do not think that you need a "," in front of "#IfWinActive". Maybe put it at the top though above "SlowMouseSpeed := 1".
Also, maybe Alt puts you into window mode?
Have you checked the controls in Overwatch to see if Alt is mapped to that function?
Last edited by GeminiContractor on 05 Jul 2016, 18:58, edited 1 time in total.
User avatar
Xtra
Posts: 1914
Joined: 02 Oct 2015, 12:15

Re: Change mouse speed or dpi on keyboard hold

05 Jul 2016, 18:53

You probably need to run the script as admin.

I made this for someone awhile back and its on old ahk forum:

Code: Select all

#NoEnv
SetBatchLines -1

; Usage: SniperMode(1,[speed]) = on  (speed optional: 1-20 range, default is 2)
;        SniperMode(0)         = off


XButton1::
    SniperMode(1) 
        KeyWait XButton1
    SniperMode(0)
return

XButton2::
    SniperMode(1,1) 
        KeyWait XButton2
    SniperMode(0)
return


SniperMode(m,s:=2){
   static b := 0, c := DllCall("SystemParametersInfo", UInt, 0x70, UInt, 0, UIntP, b, UInt, 0)
   DllCall("SystemParametersInfo", UInt, 0x71, UInt, 0, Ptr, m ? (s>0 && s<=20 ? s : 2) : b, UInt, 0)
}
Mailman

Re: Change mouse speed or dpi on keyboard hold

05 Jul 2016, 20:37

Ok... The following is working now. BUT I can't get it to work on the Alt Gr Key and more importantly, it doesn't work in Overwatch. Only in Windows.

Code: Select all

; GET INTIAL MOUSE SPEED:
; 0x70 (SPI_GETMOUSESPEED), third parameter is the speed (range is 1-20, 10 is default)
DllCall("SystemParametersInfo", UInt, 0x70, UInt, 0, UIntP, Mouse_Speed_Orig, UInt, 0)

Mouse_Speed_Slow := Mouse_Speed_Orig * 0.5
Mouse_Speed_Slow := Floor(Mouse_Speed_Slow)
; Or directly set the speed: Mouse_Speed_Slow =5
Return

space::
sendinput {f5}
loop, 1
{
KeyWait, space
sendinput {f5}
}
return

f5::
; 0x71 (SPI_SETMOUSESPEED), third parameter is the speed (range is 1-20, 10 is default)
DllCall("SystemParametersInfo", UInt, 0x70, UInt, 0, UIntP, Mouse_Speed_Now, UInt, 0)
If Mouse_Speed_Now =%Mouse_Speed_Orig%
DllCall("SystemParametersInfo", UInt, 0x71, UInt, 0, UInt, Mouse_Speed_Slow, UInt, 0)
Else
DllCall("SystemParametersInfo", UInt, 0x71, UInt, 0, UInt, Mouse_Speed_Orig, UInt, 0)
Return

~/::Suspend
+~/::Suspend
So close so close so close!
How do I get this to work in Overwatch?
GeminiContractor
Posts: 15
Joined: 03 Jul 2016, 10:30

Re: Change mouse speed or dpi on keyboard hold

06 Jul 2016, 03:23

Mailman wrote:it doesn't work in Overwatch
Now that I think about it, Blizzard is trying to make sure that no one cheats in that game, if I recall, even going so far as to ban people just because they lost connection (regardless of whether it was on purpose or not)
I know that the script that you want is not for cheating, but maybe they are using something to block AutoHotkey?
troynono

Re: Change mouse speed or dpi on keyboard hold

06 Nov 2016, 15:19

I think Overwatch take the raw input of your mouse, and what the script is changing is the windows sensitivity. So it's not working. Anyway since a few patch you can custom the aim speed while scoped with heroes like widow.
User avatar
evilC
Posts: 4783
Joined: 27 Feb 2014, 12:30

Re: Change mouse speed or dpi on keyboard hold

07 Nov 2016, 07:03

Games, in general, ignore windows sensitivity settings.

There is, however a way to do this.
What you need to do is by default have your mouse LESS sensitive than it needs to be (ie "Sniper mode" is the DEFAULT).
Then, you can make the mouse MORE sensitive when not in sniper mode by reading the mouse input from the RAWINPUT API, and sending fake extra movement using the mouse_event api.

ie you see a move of +1/+1 come in via RawInput, so you use the mouse_event API call to send a duplicate +1/+1 which in effect doubles your sensitivity - in other words you are not making the mouse less sensitive when in sniper mode - you are making the mouse more sensitive when not in sniper mode.

All the code needed to do this is here:
https://autohotkey.com/boards/viewtopic.php?t=10159

Note that you will have to filter what comes into RawInput because whatever you send as output via mouse_event will also be seen as input by RawInput.
However, luckily, any output sent by mouse_event will have a DeviceID of 0, whereas input from a real mouse will have a non-zero DeviceID - so simply do not repeat any input that has a DeviceID of 0.

This post here has the code that lets you tell whether the input is coming from a real mouse or mouse_event:
https://autohotkey.com/boards/viewtopic ... 50#p102050
User avatar
evilC
Posts: 4783
Joined: 27 Feb 2014, 12:30

Re: Change mouse speed or dpi on keyboard hold

07 Nov 2016, 07:14

Here you go - working DPI shifter set to double sensitivity when not in sniper mode.

F12 to toggle on / off

Code: Select all

#SingleInstance,Force
 
MacroOn := 1
new MouseDelta("MouseEvent")
 
return
 
GuiClose:
	ExitApp
 
F12::
	MacroOn := !MacroOn
	return
 
; Gets called when mouse moves or stops
; x and y are DELTA moves (Amount moved since last message), NOT coordinates.
MouseEvent(MouseID, x := 0, y := 0){
	global MacroOn
 
	if (!MacroOn)
		return

	if (MouseID){
		DllCall("mouse_event",uint,1,int, x ,int, y,uint,0,int,0)
	}
}
 
; Instantiate this class and pass it a func name or a Function Object
; The specified function will be called with the delta move for the X and Y axes
; Normally, there is no windows message "mouse stopped", so one is simulated.
; After 10ms of no mouse movement, the callback is called with 0 for X and Y
Class MouseDelta {
	__New(callback){
		static DevSize := 8 + A_PtrSize
		static RIDEV_INPUTSINK := 0x00000100
 
		this.TimeoutFn := this.TimeoutFunc.Bind(this)
 
		this.Callback := callback
		; Register mouse for WM_INPUT messages.
		VarSetCapacity(RAWINPUTDEVICE, DevSize)
		NumPut(1, RAWINPUTDEVICE, 0, "UShort")
		NumPut(2, RAWINPUTDEVICE, 2, "UShort")
		NumPut(RIDEV_INPUTSINK, RAWINPUTDEVICE, 4, "Uint")
		; WM_INPUT needs a hwnd to route to, so get the hwnd of the AHK Gui.
		; It doesn't matter if the GUI is showing, it still exists
		Gui +hwndhwnd
		NumPut(hwnd, RAWINPUTDEVICE, 8, "Uint")
 
		this.RAWINPUTDEVICE := RAWINPUTDEVICE
		DllCall("RegisterRawInputDevices", "Ptr", &RAWINPUTDEVICE, "UInt", 1, "UInt", DevSize )
		fn := this.MouseMoved.Bind(this)
		OnMessage(0x00FF, fn)
	}
 
	__Delete(){
		static RIDEV_REMOVE := 0x00000001
		static DevSize := 8 + A_PtrSize
		RAWINPUTDEVICE := this.RAWINPUTDEVICE
		NumPut(RIDEV_REMOVE, RAWINPUTDEVICE, 4, "Uint")
		DllCall("RegisterRawInputDevices", "Ptr", &RAWINPUTDEVICE, "UInt", 1, "UInt", DevSize )
	}
 
	; Called when the mouse moved.
	; Messages tend to contain small (+/- 1) movements, and happen frequently (~20ms)
	MouseMoved(wParam, lParam){
		Critical
		; RawInput statics
		static DeviceSize := 2 * A_PtrSize, iSize := 0, sz := 0, offsets := {x: (20+A_PtrSize*2), y: (24+A_PtrSize*2)}, uRawInput
 
		static axes := {x: 1, y: 2}
 
		VarSetCapacity(raw, 40, 0)
		If (!DllCall("GetRawInputData",uint,lParam,uint,0x10000003,uint,&raw,"uint*",40,uint, 16) or ErrorLevel)
			Return 0
		ThisMouse := NumGet(raw, 8)

		; Find size of rawinput data - only needs to be run the first time.
		if (!iSize){
			r := DllCall("GetRawInputData", "UInt", lParam, "UInt", 0x10000003, "Ptr", 0, "UInt*", iSize, "UInt", 8 + (A_PtrSize * 2))
			VarSetCapacity(uRawInput, iSize)
		}
		sz := iSize	; param gets overwritten with # of bytes output, so preserve iSize
		; Get RawInput data
		r := DllCall("GetRawInputData", "UInt", lParam, "UInt", 0x10000003, "Ptr", &uRawInput, "UInt*", sz, "UInt", 8 + (A_PtrSize * 2))
 
		x := NumGet(&uRawInput, offsets.x, "Int")
		y := NumGet(&uRawInput, offsets.y, "Int")
 
		this.Callback.(ThisMouse, x, y)
 
		; There is no message for "Stopped", so simulate one
		fn := this.TimeoutFn
		SetTimer, % fn, -50
	}
 
	TimeoutFunc(){
		this.Callback.(0, 0)
	}
 
}
User avatar
evilC
Posts: 4783
Joined: 27 Feb 2014, 12:30

Re: Change mouse speed or dpi on keyboard hold

07 Nov 2016, 11:56

Added a more configurable version of the above script to my MouseDelta thread:
https://autohotkey.com/boards/viewtopic ... 46#p115546

This one makes it easier to tweak some settings:
Hotkey used to shift DPI
Mode of hotkey (Press and hold, or toggle)
Scale Factor (How much the DPI is magnified when not in Sniper Mode)
dvamg

Re: Change mouse speed or dpi on keyboard hold

31 Dec 2016, 19:43

GeminiContractor wrote:I am not sure if it is what you want since it toggles, and you might have tried it, but there is a script to slow down the mouse here: https://autohotkey.com/board/topic/1353 ... entry90991

Code: Select all

; Autohotkey script "Toggle Mouse sensitivity"
;=================================================================================
SlowMouseSpeed      := 1
NormalMouseSpeed    := true ; State of Mouse pointer speed
UserMouseSpeed      := 0    ; Speed sensed before slow down
MouseThreshold1     := 6
MouseThreshold2     := 10
MouseEnhance        := 1

SPI_GETMOUSESPEED   := 0x70
SPI_SETMOUSESPEED   := 0x71
SPI_SETMOUSE        := 0x04 

;=================================================================================
*RAlt::   toggleMouseSpeed()

;=================================================================================
toggleMouseSpeed() {
    global
    ; SET LOW SPEED
    if( NormalMouseSpeed )
    {
        ; SENSE BEFORE
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,prevSpeed, UInt,0)

        ; Temporarily reduces the mouse cursor's speed.
        ; Retrieve the current speed so that it can be restored later
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,UserMouseSpeed, UInt,0)
        ; Slow down mouse speed
        DllCall("SystemParametersInfo", UInt,SPI_SETMOUSESPEED, UInt,0, UInt,SlowMouseSpeed, UInt,0)

        ; SENSE AFTER
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,currentSpeed, UInt,0)
        ToolTip, Mouse slow: %currentSpeed%/20

        ; REMEMBER CURRENT STATE
        NormalMouseSpeed := false
    }
    ; RESTORE SPEED
    else {
        ; SENSE BEFORE
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,prevSpeed, UInt,0)

        ; Restore the original speed.
        DllCall("SystemParametersInfo", UInt, SPI_SETMOUSESPEED, UInt,0, UInt,UserMouseSpeed, UInt,0)

        ; Restore the original speed acceleration thresholds and speed
        VarSetCapacity(MySet, 32, 0) 
        InsertInteger(MouseThreshold1, MySet, 0)
        InsertInteger(MouseThreshold2, MySet, 4)
        InsertInteger(MouseEnhance   , MySet, 8)
        DllCall("SystemParametersInfo", UInt,SPI_SETMOUSE, UInt,0, Str,MySet, UInt,1) 

        ; SENSE AFTER
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,currentSpeed, UInt,0)
        ToolTip, Mouse restored: %currentSpeed%/20

        ; REMEMBER CURRENT STATE
        NormalMouseSpeed := true
    }
    SetTimer, RemoveToolTip, 1000
}
;=================================================================================
InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4) {
    ; Copy each byte in the integer into the structure as raw binary data. 
    Loop %pSize%
    DllCall("RtlFillMemory", "UInt",&pDest + pOffset + A_Index-1, "UInt", 1, "UChar", pInteger >> 8*(A_Index-1) & 0xFF) 
}
;=================================================================================
RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return

;=================================================================================

This turns on "Enhance pointer precision" aka mouse acceleration ON when returning to default state. How to fix it?
wheelbase_belize
Posts: 1
Joined: 07 May 2017, 11:27

Re: Change mouse speed or dpi on keyboard hold

07 May 2017, 12:05

GeminiContractor wrote:I am not sure if it is what you want since it toggles, and you might have tried it, but there is a script to slow down the mouse here: https://autohotkey.com/board/topic/1353 ... entry90991

Code: Select all

; Autohotkey script "Toggle Mouse sensitivity"
;=================================================================================
SlowMouseSpeed      := 1
NormalMouseSpeed    := true ; State of Mouse pointer speed
UserMouseSpeed      := 0    ; Speed sensed before slow down
MouseThreshold1     := 6
MouseThreshold2     := 10
MouseEnhance        := 1

SPI_GETMOUSESPEED   := 0x70
SPI_SETMOUSESPEED   := 0x71
SPI_SETMOUSE        := 0x04 

;=================================================================================
*RAlt::   toggleMouseSpeed()

;=================================================================================
toggleMouseSpeed() {
    global
    ; SET LOW SPEED
    if( NormalMouseSpeed )
    {
        ; SENSE BEFORE
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,prevSpeed, UInt,0)

        ; Temporarily reduces the mouse cursor's speed.
        ; Retrieve the current speed so that it can be restored later
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,UserMouseSpeed, UInt,0)
        ; Slow down mouse speed
        DllCall("SystemParametersInfo", UInt,SPI_SETMOUSESPEED, UInt,0, UInt,SlowMouseSpeed, UInt,0)

        ; SENSE AFTER
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,currentSpeed, UInt,0)
        ToolTip, Mouse slow: %currentSpeed%/20

        ; REMEMBER CURRENT STATE
        NormalMouseSpeed := false
    }
    ; RESTORE SPEED
    else {
        ; SENSE BEFORE
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,prevSpeed, UInt,0)

        ; Restore the original speed.
        DllCall("SystemParametersInfo", UInt, SPI_SETMOUSESPEED, UInt,0, UInt,UserMouseSpeed, UInt,0)

        ; Restore the original speed acceleration thresholds and speed
        VarSetCapacity(MySet, 32, 0) 
        InsertInteger(MouseThreshold1, MySet, 0)
        InsertInteger(MouseThreshold2, MySet, 4)
        InsertInteger(MouseEnhance   , MySet, 8)
        DllCall("SystemParametersInfo", UInt,SPI_SETMOUSE, UInt,0, Str,MySet, UInt,1) 

        ; SENSE AFTER
        DllCall("SystemParametersInfo", UInt,SPI_GETMOUSESPEED, UInt,0, UIntP,currentSpeed, UInt,0)
        ToolTip, Mouse restored: %currentSpeed%/20

        ; REMEMBER CURRENT STATE
        NormalMouseSpeed := true
    }
    SetTimer, RemoveToolTip, 1000
}
;=================================================================================
InsertInteger(pInteger, ByRef pDest, pOffset = 0, pSize = 4) {
    ; Copy each byte in the integer into the structure as raw binary data. 
    Loop %pSize%
    DllCall("RtlFillMemory", "UInt",&pDest + pOffset + A_Index-1, "UInt", 1, "UChar", pInteger >> 8*(A_Index-1) & 0xFF) 
}
;=================================================================================
RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return

;=================================================================================

Ok I tried it on my rightmousebutton but it blocks my input is there a way around this because I want toggle speed and aiming ingame on my rightmousebutton

Return to “Gaming”

Who is online

Users browsing this forum: No registered users and 19 guests