Adjusting mouse sensitivity with hotkeys Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Adjusting mouse sensitivity with hotkeys

Post by imkira3 » 01 Feb 2023, 05:20

I have been working on how to implement a much requested (and rarely seen) feature for AHK scripts, as far as I know this has never been done in v2 yet and I'm having some issues. This original code was for v1 but it was easy to convert to v2 I think, just needed a few spaces and quotes. I have read multiple examples, I'm sure my code is correct, so what am I doing wrong? The goal is do adjust mouse DPI from 1-20 through binds to improve gaming experience, here is a small scale test I'm working on, should set DPI to 5 making the mouse move quite slow when you press Ctrl+Shift+Numpad1:

Code: Select all

A_DPIAdjusted := 5
A_DPIOriginal := 0
<^<+Numpad1::{
if A_DPIOriginal := 0{
; This line should retrieve the original mouse speed for later use
DllCall("SystemParametersInfo", "UInt", "SPI_GETMOUSESPEED", "UInt", 0, "UIntP", A_DPIOriginal, "UInt", 0)
}
; This line is supposed to adjust the current mouse speed
DllCall("SystemParametersInfo", "UInt", "SPI_SETMOUSESPEED", "UInt", 0, "UInt", A_DPIAdjusted, "UInt", 0)
}
As you can see the code looks simple enough, I read the DllCall page and it looks good too, I'm not getting any errors either, and I'm running AHK elevated.

User avatar
mikeyww
Posts: 26600
Joined: 09 Sep 2014, 18:38

Re: Adjusting mouse sensitivity with hotkeys

Post by mikeyww » 01 Feb 2023, 06:54

Hello,

I did not run your script, but the strings in the third parameter would not work within AHK. These should actually be numbers (0x70, 0x71). You can use a variable (unquoted) if you define it in advance. One clue about this is the preceding parameter, which is UInt, indicating an unsigned integer. Another clue will be the error message that appears, "Expected a Number but got a String".

To prevent confusion, I suggest that A_ should be avoided as leading text of a new variable name, because AHK uses this for its built-in variables.

:= is always assignment, never comparison.

Code: Select all

#Requires AutoHotkey v2.0
A_DPIOriginal := 5
If A_DPIOriginal := 0
     MsgBox "It's neither zero nor null"
Else MsgBox "It's zero or null"
This original code was for v1
Your script was not translated (directly and thoroughly and accurately) from a working v1 script, as this same approach would also not work in v1.

Although the numpad can be tricky to use, when I think my hotkey is not working, I run a simple test.

Code: Select all

#Requires AutoHotkey v2.0
<^NumpadEnd::MsgBox 'Shifted 1'
I'm sure my code is correct
This statement has no basis. You can believe that you are always right, but it won't help you debug any of your scripts.

ntepa
Posts: 406
Joined: 19 Oct 2022, 20:52

Re: Adjusting mouse sensitivity with hotkeys

Post by ntepa » 01 Feb 2023, 10:04

<^<+Numpad1 won't work as a hotkey. Shift + Numpad1 is NumpadEnd.
SPI_GETMOUSESPEED and SPI_SETMOUSESPEED are not strings.
When a DllCall argtype has a P or * suffix, you need to use a VarRef by putting & in front of the variable.

Code: Select all

<^NumpadEnd::
{
    static toggle := 0
    static DPIOriginal := 0
    static DPIAdjusted := 5
    static SPI_GETMOUSESPEED := 0x70
    static SPI_SETMOUSESPEED := 0x71

    if DPIOriginal = 0 {
        DllCall("SystemParametersInfo", "UInt", SPI_GETMOUSESPEED, "UInt", 0, "UIntP", &DPIOriginal, "UInt", 0)
    }

    if toggle := !toggle {
        DllCall("SystemParametersInfo", "UInt", SPI_SETMOUSESPEED, "UInt", 0, "UInt", DPIAdjusted, "UInt", 0)
    } else {
        DllCall("SystemParametersInfo", "UInt", SPI_SETMOUSESPEED, "UInt", 0, "UInt", DPIOriginal, "UInt", 0)
    }
}

imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Adjusting mouse sensitivity with hotkeys

Post by imkira3 » 01 Feb 2023, 17:17

ntepa wrote:
01 Feb 2023, 10:04
<^<+Numpad1 won't work as a hotkey. Shift + Numpad1 is NumpadEnd.
SPI_GETMOUSESPEED and SPI_SETMOUSESPEED are not strings.
When a DllCall argtype has a P or * suffix, you need to use a VarRef by putting & in front of the variable.

Code: Select all

<^NumpadEnd::
{
    static toggle := 0
    static DPIOriginal := 0
    static DPIAdjusted := 5
    static SPI_GETMOUSESPEED := 0x70
    static SPI_SETMOUSESPEED := 0x71

    if DPIOriginal = 0 {
        DllCall("SystemParametersInfo", "UInt", SPI_GETMOUSESPEED, "UInt", 0, "UIntP", &DPIOriginal, "UInt", 0)
    }

    if toggle := !toggle {
        DllCall("SystemParametersInfo", "UInt", SPI_SETMOUSESPEED, "UInt", 0, "UInt", DPIAdjusted, "UInt", 0)
    } else {
        DllCall("SystemParametersInfo", "UInt", SPI_SETMOUSESPEED, "UInt", 0, "UInt", DPIOriginal, "UInt", 0)
    }
}
Okay, that was very helpful, but I still have 1 problem, it's only letting me call local variables, and this is no good. The variable that stores the original DPI has to be global, since this won't be the only button that adjusts DPI. This works:

Code: Select all

^1::{
A_DPIOriginal := 0
A_DPIAdjusted := 5
A_GetMouseSpeed := 0x70
A_SetMouseSpeed := 0x71
if A_DPIOriginal = 0 {
DllCall("SystemParametersInfo", "UInt", A_GetMouseSpeed, "UInt", 0, "UIntP", &A_DPIOriginal, "UInt", 0)
}
DllCall("SystemParametersInfo", "UInt", A_SetMouseSpeed, "UInt", 0, "UInt", A_DPIAdjusted, "UInt", 0)
}
But this doesn't:

Code: Select all

A_DPIOriginal := 0

^1::{
A_DPIAdjusted := 5
A_GetMouseSpeed := 0x70
A_SetMouseSpeed := 0x71
if A_DPIOriginal = 0 {
DllCall("SystemParametersInfo", "UInt", A_GetMouseSpeed, "UInt", 0, "UIntP", &A_DPIOriginal, "UInt", 0)
}
DllCall("SystemParametersInfo", "UInt", A_SetMouseSpeed, "UInt", 0, "UInt", A_DPIAdjusted, "UInt", 0)
}

imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Adjusting mouse sensitivity with hotkeys

Post by imkira3 » 01 Feb 2023, 17:35

Nvm I figured it out! I was using = instead of :=, oops lol

Code: Select all

A_DPIOriginal := 0
A_GetMouseSpeed := 0x70
A_SetMouseSpeed := 0x71

^1::{
A_DPIAdjusted := 5
if A_DPIOriginal := 0{
DllCall("SystemParametersInfo", "UInt", A_GetMouseSpeed, "UInt", 0, "UIntP", &A_DPIOriginal, "UInt", 0)
}
DllCall("SystemParametersInfo", "UInt", A_SetMouseSpeed, "UInt", 0, "UInt", A_DPIAdjusted, "UInt", 0)
}

ntepa
Posts: 406
Joined: 19 Oct 2022, 20:52

Re: Adjusting mouse sensitivity with hotkeys  Topic is solved

Post by ntepa » 01 Feb 2023, 18:11

A_DPIOriginal needs to be declared global in order to change the value.
It should be = not := because you're comparing.

Code: Select all

A_DPIOriginal := 0
A_GetMouseSpeed := 0x70
A_SetMouseSpeed := 0x71

^1::{
    global A_DPIOriginal
    static A_DPIAdjusted := 5
    if A_DPIOriginal = 0 {
        DllCall("SystemParametersInfo", "UInt", A_GetMouseSpeed, "UInt", 0, "UIntP", &A_DPIOriginal, "UInt", 0)
    }
    DllCall("SystemParametersInfo", "UInt", A_SetMouseSpeed, "UInt", 0, "UInt", A_DPIAdjusted, "UInt", 0)
}

imkira3
Posts: 84
Joined: 10 Jan 2023, 13:57
Contact:

Re: Adjusting mouse sensitivity with hotkeys

Post by imkira3 » 01 Feb 2023, 18:47

ntepa wrote:
01 Feb 2023, 18:11
A_DPIOriginal needs to be declared global in order to change the value.
It should be = not := because you're comparing.
Ah, I see now, that makes sense. Perfect, now I can make a numpad script that will change DPI from 1-20! Check out the imkira3 Keyboard Design v3 or newer for more information, it should be released in a few days I think. I wanna think more about features to add first.

ntepa
Posts: 406
Joined: 19 Oct 2022, 20:52

Re: Adjusting mouse sensitivity with hotkeys

Post by ntepa » 01 Feb 2023, 19:08

In a hotkey or function, if you're just reading a global variable, it doesn't need to be declared global. You can still declare it global for clarity.
If you're assigning a global variable or changing its value inside a hotkey or function, it needs to be declared global.
It doesn't matter if you declare the global variables in the sub-brackets as long as it's inside the hotkey brackets.

robinson
Posts: 189
Joined: 12 Sep 2019, 20:28

Re: Adjusting mouse sensitivity with hotkeys

Post by robinson » 13 Mar 2023, 22:05

@ntepa

Hi,
Could you help me re-write this
into something that increases sensitivity when a key is held,
and puts it back to normal when you release?
Thanks!

ntepa
Posts: 406
Joined: 19 Oct 2022, 20:52

Re: Adjusting mouse sensitivity with hotkeys

Post by ntepa » 13 Mar 2023, 23:07

@robinson
hold x to increase sensitivity.
You can change the speed by using the newSpeed parameter. 1 is slowest, 20 is fastest, 10 is usually default.

Code: Select all

x::MouseSpeedToggleHold(20) ; change the speed here

MouseSpeedToggleHold(newSpeed) {
    static GetMouseSpeed := 0x70
    static SetMouseSpeed := 0x71
    DllCall("SystemParametersInfo", "UInt", GetMouseSpeed, "UInt", 0, "UIntP", &originalSpeed:=0, "UInt", 0) ; Get the original speed
    DllCall("SystemParametersInfo", "UInt", SetMouseSpeed, "UInt", 0, "UInt", newSpeed, "UInt", 0)           ; set the new speed
    KeyWait RegExReplace(A_ThisHotkey, "^.*?(\w+)$", "$1")                                                    ; wait for this hotkey
    DllCall("SystemParametersInfo", "UInt", SetMouseSpeed, "UInt", 0, "UInt", originalSpeed, "UInt", 0)      ; restore the speed
}
Last edited by ntepa on 17 Mar 2024, 14:37, edited 1 time in total.

robinson
Posts: 189
Joined: 12 Sep 2019, 20:28

Re: Adjusting mouse sensitivity with hotkeys

Post by robinson » 14 Mar 2023, 22:40

@ntepa

That's amazing. Thank-you so much.

Fomenosh
Posts: 1
Joined: 17 Mar 2024, 10:29

Re: Adjusting mouse sensitivity with hotkeys

Post by Fomenosh » 17 Mar 2024, 10:32

@ntepa

How do I use LButton as a hotkey in this case, without losing LButton's default function? (sorry im brazilian and my english is not that good)

ntepa
Posts: 406
Joined: 19 Oct 2022, 20:52

Re: Adjusting mouse sensitivity with hotkeys

Post by ntepa » 17 Mar 2024, 14:37

Fomenosh wrote:
17 Mar 2024, 10:32
How do I use LButton as a hotkey in this case, without losing LButton's default function?
Put ~ in front of the hotkey:

Code: Select all

~LButton::MouseSpeedToggleHold(3) ; change the speed here

MouseSpeedToggleHold(newSpeed) {
    static GetMouseSpeed := 0x70
    static SetMouseSpeed := 0x71
    DllCall("SystemParametersInfo", "UInt", GetMouseSpeed, "UInt", 0, "UIntP", &originalSpeed:=0, "UInt", 0) ; Get the original speed
    DllCall("SystemParametersInfo", "UInt", SetMouseSpeed, "UInt", 0, "UInt", newSpeed, "UInt", 0)           ; set the new speed
    KeyWait RegExReplace(A_ThisHotkey, "^.*?(\w+)$", "$1")                                                   ; wait for this hotkey
    DllCall("SystemParametersInfo", "UInt", SetMouseSpeed, "UInt", 0, "UInt", originalSpeed, "UInt", 0)      ; restore the speed
}

Post Reply

Return to “Ask for Help (v2)”