Search code examples
autohotkeydarkmode

Switching dark mode using AutoHotKey Error: Parameter #2 of RegWrite is invalid


Using AutoHotKey v2 and the following post I am trying to switch my dark mode on my windows machine.

AutoHotKey introduced a new syntax in v2 which I am trying to translate. Resulting in the following script:

^!r::
{
    valueType := "REG_DWORD"
    keyName := "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
    valueName := "AppsUseLightTheme"
    appMode := RegRead(keyName, valueName)

    ; MsgBox(appMode)
    If (appMode = 0) ; dark
        RegWrite(valueType, keyName, valueName, 1)
    else   ; If (appMode = 1) light 
        RegWrite(valueType, keyName, valueName, 0)
}

However an error message on the keyName when writing the value to the registry:

Error: Parameter #2 of RegWrite is invalid.

Specifically: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize

    006: appMode := RegRead(keyName, valueName)
    008: If (appMode = 0)
▶   009: RegWrite(valueType, keyName, valueName, 1)
    010: Else
    011: RegWrite(valueType, keyName, valueName, 0)

which would suggest the keyName is invalid. It does work however on the read.

What am I missing?


Solution

  • The right syntax for RegWrite is

    RegWrite Value, ValueType, KeyName , ValueName

    #Requires AutoHotkey v2.0
    
    ^!r::
    {
        valueType := "REG_DWORD"
        keyName := "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
        valueName := "AppsUseLightTheme"
        appMode := RegRead(keyName, valueName)
    
        ; MsgBox(appMode)
    
        If (appMode = 0) ; dark
            RegWrite 1, ValueType, KeyName , ValueName
        else   ; If (appMode = 1) light 
            RegWrite 0, ValueType, KeyName , ValueName
    }