Errors in my code while converting from v1 to v2

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Errors in my code while converting from v1 to v2

Post by GameNtt » 06 May 2023, 04:33

I did the conversion of a script which has many hotkeys in one script and have found some errors which I have absolutely no clue as to why they are not working. I checked the docs and it seems to be the exact same command. https://imgur.com/a/JL5bZ6S All the errors I have encountered are here.

Code: Select all

^+!f::
{
 Time := FormatTime(, "dd/MM/yy")
 Send (Time "- To         ₹ () {ShiftDown}{Enter}{ShiftUp}                      From   ₹ ()")
 Return
}
This above function for some reason does not work in a file with other hotkeys, but it works when it is in an script all by itself. Why is this so?



This is the entire script

Code: Select all

#SingleInstance
SendMode "Input"
SetWorkingDir A_ScriptDir
#Include "Cursor Schemes v2.ahk"
CoordMode "Mouse","Screen"
SetTitleMatchMode 2
GroupAdd "xyzf", "ahk_class AutoHotkeyGUI"
GroupAdd "xyzf", "BlankScr.ahk"
global m := 1
mvmouse(m)
OutputDebug cMod(m,2)
global scrbrt := GetBrightness()
global scrbrt2 := GetBrightness()

#+!s::Suspend

#^!+s::SoundBeep

#!+e::Edit

#!+r::Reload

^F10::SetNumLockState !GetKeyState("NumLock", "T")

#!+q::Run "C:\Program Files\AutoHotkey\WindowSpy.ahk"

#!+f::Run "C:\Program Files (x86)\Everything\Everything.exe"

#^!c::SystemCursor("Toggle") 

#!+c::MouseMove 683, 359

^!+w::Send "web.archive.org/web/*/"

;!+6::MouseMove 840,480

>+^a::Send "awesome"

>+Up::MouseMove 0, -1, 0, "R"

>+Down::MouseMove 0, 1, 0, "R"

>+Left::MouseMove -1, 0, 0, "R"

>+Right::MouseMove 1, 0, 0, "R"

^F1::SetDefaultKeyboardLang(0x4009) ; english

^F2::SetDefaultKeyboardLang(0x0408) ; greek

^F3::SetDefaultKeyboardLang(0x0411) ; japanese

^F4::SetDefaultKeyboardLang(0x0419) ; russian 

^F5::SetDefaultKeyboardLang(0x10409) ; dvorak

+F1::SetInputLang(0x4009)

+F2::SetInputLang(0x0408)

+F3::SetInputLang(0x0411)

+F4::SetInputLang(0x0419)

+F5::SetInputLang(0x10409) ; dvorak

#c::Run "Calc.exe"

#!k::Run "C:\Program Files\Katana\Katana.exe"

^!b::Run "C:\Program Files\Bingo\Bingo.exe"

#^!g::Run "C:\Users\Redacted\Desktop\Gma.ahk"

#!+m::MsgBox m, "",  "T3"

#+!d::Run "C:\Users\Redacted\Desktop\Doad.ahk"

#+!b::Run "C:\Users\Redacted\Desktop\BlankScr.ahk"

#+g::Run "C:\Users\Redacted\Downloads\Music.mov"

#^!s::SendMessage 0x0112, 0xF140, 0,, "Program Manager"

^+!f::
{
 Time := FormatTime(, "dd/MM/yy")
 Send (Time "- To         ₹ () {ShiftDown}{Enter}{ShiftUp}                      From   ₹ ()")
 Return
}

^!+i::
{
 ChangeCursorScheme("Windows Inverted")
 SoundBeep
 Return
}

#!+w::
{
 MsgBox WinGetProcessPath("A")
 Return
}

^+s::
{
 Loop 10
 Click
 Return
}

>+`::
{
  title := WinGetTitle("A")
  WinGetPos &x, &y, &wid, &hei, title
  MsgBox "WinHeight := " hei ", WinWidth := " wid
 Return
}

!^9::
{
 global m += 1
 mvmouse(m)
 OutputDebug "m := " m
 Return
}

^F12::
{
 Run "C:\Users\Redacted\Desktop\BlankScr.ahk"
 SystemCursor("Hide")
 scrbrt := GetBrightness()
 SoundBeep
 mvmouse(0)
 SetBrightness(0)
 WinWaitActive("ahk_group xyzf")
 SoundBeep
 WinWaitNotActive("ahk_group xyzf")
 SoundBeep
 mvmouse(m)
 SystemCursor("Show")
 SetBrightness(scrbrt)
}

!,::
{
  AdjustScreenBrightness(-10)
  Sleep 100
  scrbrt2 := GetBrightness()
  OutputDebug "Current Screen Brightness in `% is " scrbrt2
  Return
}

!.::
{
  AdjustScreenBrightness(10)
  Sleep 100
  scrbrt2 := GetBrightness()
  OutputDebug "Current Screen Brightness in `% is " scrbrt2
  Return
}

^+x::
{
    OutputDebug A_AhkVersion
}

GetBrightness()
{
    Return ComObjGet("winmgmts:\root\WMI").InstancesOf("WmiMonitorBrightness WHERE Active=TRUE").ItemIndex(0).CurrentBrightness
}

SetBrightness(val)
{
    service := "winmgmts:{impersonationLevel=impersonate}!\\.\root\WMI"
    monitors := ComObjGet(service).ExecQuery("SELECT * FROM WmiMonitorBrightness WHERE Active=TRUE")
    monMethods := ComObjGet(service).ExecQuery("SELECT * FROM wmiMonitorBrightNessMethods WHERE Active=TRUE")
    minBrightness := 0  ; level below this is identical to this

    if (val < minBrightness)  ; parenthesis is necessary here
        val := minBrightness
    toSet := val
    if (toSet > 100)
        Return
    if (toSet < minBrightness)
        toSet := minBrightness

    for i in monMethods {
        i.WmiSetBrightness(1, toSet)
        break
    }
}

AdjustScreenBrightness(step)
{
    service := "winmgmts:{impersonationLevel=impersonate}!\\.\root\WMI"
    monitors := ComObjGet(service).ExecQuery("SELECT * FROM WmiMonitorBrightness WHERE Active=TRUE")
    monMethods := ComObjGet(service).ExecQuery("SELECT * FROM wmiMonitorBrightNessMethods WHERE Active=TRUE")
    minBrightness := 5  ; level below this is identical to this

    for i in monitors {
        curt := i.CurrentBrightness
        break
    }
    if (curt < minBrightness)  ; parenthesis is necessary here
        curt := minBrightness
    toSet := curt + step
    if (toSet > 100)
        Return
    if (toSet < minBrightness)
        toSet := minBrightness
        
    

    for i in monMethods {
        i.WmiSetBrightness(1, toSet)
        break
    }
}

SystemCursor(cmd)  ; cmd = "Show|Hide|Toggle|Reload"
{
    static visible := true, c := []
    static sys_cursors := [32512, 32513, 32514, 32515, 32516, 32642
                         , 32643, 32644, 32645, 32646, 32648, 32649, 32650]
    if (cmd = "Reload" or !c.Count)  ; Reload when requested or at first call.
    {
        for i, id in sys_cursors
        {
            h_cursor  := DllCall("LoadCursor", "Ptr", 0, "Ptr", id)
            h_default := DllCall("CopyImage", "Ptr", h_cursor, "UInt", 2
                , "Int", 0, "Int", 0, "UInt", 0)
            h_blank   := DllCall("CreateCursor", "Ptr", 0, "Int", 0, "Int", 0
                , "Int", 32, "Int", 32
                , "Ptr", Buffer(32*4, 0xFF)
                , "Ptr", Buffer(32*4, 0))
            c[id] := {default: h_default, blank: h_blank}
        }
    }
    switch cmd
    {
        case "Show": visible := true
        case "Hide": visible := false
        case "Toggle": visible := !visible
        default: return
    }
    for id, handles in c
    {
        h_cursor := DllCall("CopyImage"
            , "Ptr", visible ? handles.default : handles.blank
            , "UInt", 2, "Int", 0, "Int", 0, "UInt", 0)
        DllCall("SetSystemCursor", "Ptr", h_cursor, "UInt", id)
    }
}

mvmouse(val)
{
    valm := mod(val, 2)
	if(valm = 0)
	    {
	        SetTimer MoveMouse, 250
            OutputDebug "Switched on"
	    }
    Else
    {
        SetTimer MoveMouse, 0
        OutputDebug "Switched off"
    }
    Return
}


SetDefaultKeyboardLang(LocaleID)
{
	Static SPI_SETDEFAULTINPUTLANG := 0x005A, SPIF_SENDWININICHANGE := 2	
	Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
    binaryLocaleID := "Buffer(4, 0)"
	NumPut("UPtr",LocaleID, binaryLocaleID)
	DllCall("SystemParametersInfo", "UInt", SPI_SETDEFAULTINPUTLANG, "UInt", 0, "UPtr", binaryLocaleID, "UInt", SPIF_SENDWININICHANGE)	
	owindows := WinGetList(,,,)
    Loop owindows.Length    
    	PostMessage 0x50, 0, % Lan, , % "ahk_id " windows%A_Index%
}

SetInputLang(Lang)
{
    WinExist("A")
    CtrlInFocus := ControlGetFocus()
    PostMessage(0x50, 0, Lang, CtrlInFocus)
}

cMod(dividend, divisor)
{
    if(divisor > 0)
    {
        if(dividend > 0)
        {
            while(dividend >= divisor)
                dividend -= divisor
            Return dividend

        }
        else if(dividend < 0)
        {
            while (dividend < 0)
                dividend += divisor
            Return dividend
        }
        else
        {
            Return
        }
    }
    else if(divisor < 0)
    {
        if(dividend > 0)
        {
            Return -cMod(-dividend, -divisor)
        }
        else if(dividend < 0)
        {
            Return cMod(-dividend, -divisor)
        }
        else
        {
            Return
        }
    }
    else
    Return
}

MoveMouse()
{
    If ( A_TimeIdle > 1000 && A_TimeIdle < 60001) 
        {
            MouseMove(1, 0,, "R")
            MouseMove(-1, 0,, "R")
            time := FormatTime(,"HH:mm:ss")
            OutputDebug "Moved at time " time
        }
    return
}

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

Re: Errors in my code while converting from v1 to v2

Post by mikeyww » 06 May 2023, 05:33

What happens when you run the script that you posted?

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Errors in my code while converting from v1 to v2

Post by GameNtt » 06 May 2023, 05:37

Quite a few errors. All of them are posted in that imgur link. There were too many to directly upload.

just me
Posts: 9453
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Errors in my code while converting from v1 to v2

Post by just me » 06 May 2023, 06:23

Looks like you are running the v2 code with AHK v1. Try to add

Code: Select all

#Requires AutoHotkey v2.0
at the top of the script.

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

Re: Errors in my code while converting from v1 to v2

Post by mikeyww » 06 May 2023, 06:24

You would need to go to each line with an error and translate it into a v2 statement. The AHK documentation shows the correct syntax for each.

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Errors in my code while converting from v1 to v2

Post by GameNtt » 06 May 2023, 06:57

The reason why I'm asking is because I am unable to find out why it is wrong. Some things in this are exactly the way they are supposed to be as shown in the docs.

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Errors in my code while converting from v1 to v2

Post by boiler » 06 May 2023, 07:11

It is clear from this thread and the other threads that you are not running v2. The error messages you have been showing are from v1. A couple times, you were asked to include to include the line #Requires AutoHotkey 2.0, and you apparently didn’t include that line, perhaps thinking it is not necessary. It is. If you did include it, you did not report what message AHK showed regarding that line.

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Errors in my code while converting from v1 to v2

Post by GameNtt » 06 May 2023, 08:22

I feel stupid now. For some reason, that file does not accept #Requires AutoHotkey v2. What I mean by that is that having that command on the top shows an error saying the interpreter is v1.1.36 but all other files with #Requires do not show that. I have no idea why that happens. :(

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Errors in my code while converting from v1 to v2

Post by boiler » 06 May 2023, 08:26

That means you don’t have v2 installed yet (or properly). Have you tried installing it yet? I recommend using the instructions for installing with v1.

GameNtt wrote: …all other files with #Requires do not show that.
Just #Requires? Or #Requires AutoHotkey v2? Or something else?

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Errors in my code while converting from v1 to v2

Post by GameNtt » 06 May 2023, 09:02

#Requires AutoHotkey v2 works on other scripts, just not on this one.

I have already uninstalled and reinstalled AHK once. I don't want to do that again. It was done on a clean install of Windows.

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Errors in my code while converting from v1 to v2

Post by boiler » 06 May 2023, 09:07

What does it show when you put that on this script? By the way, I should have said #Requires AutoHotkey v2.0. The .0 actually can make a difference in helping the launcher automatically run it using v2.

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Errors in my code while converting from v1 to v2

Post by GameNtt » 06 May 2023, 11:38

Earlier it was showing an error something similar to this
image.png
image.png (7.53 KiB) Viewed 572 times
Now, it does not show that error. The only thing I changed was deleting a shortcut of it from shell:startup. No idea why that should change a thing.

GameNtt
Posts: 154
Joined: 19 Aug 2022, 03:36

Re: Errors in my code while converting from v1 to v2

Post by GameNtt » 02 Jun 2023, 10:08

Code: Select all

; v1
SetDefaultKeyboard(LocaleID){
	Global
	SPI_SETDEFAULTINPUTLANG := 0x005A
	SPIF_SENDWININICHANGE := 2
	Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
	VarSetCapacity(Lan%LocaleID%, 4, 0)
	NumPut(LocaleID, Lan%LocaleID%)
	DllCall("SystemParametersInfo", "UInt", SPI_SETDEFAULTINPUTLANG, "UInt", 0, "UPtr", &Lan%LocaleID%, "UInt", SPIF_SENDWININICHANGE)
	WinGet, windows, List
	Loop %windows% {
		PostMessage 0x50, 0, %Lan%, , % "ahk_id " windows%A_Index%
	}
}
return

Code: Select all

; v2
SetDefaultKeyboardLang(LocaleID)
{
	Static SPI_SETDEFAULTINPUTLANG := 0x005A, SPIF_SENDWININICHANGE := 2	
	Lan := DllCall("LoadKeyboardLayout", "Str", Format("{:08x}", LocaleID), "Int", 0)
    binaryLocaleID := Buffer(4,0)
	NumPut("UPtr",LocaleID, binaryLocaleID)
	DllCall("SystemParametersInfo", "UInt", SPI_SETDEFAULTINPUTLANG, "UInt", 0, "UPtr", binaryLocaleID, "UInt", SPIF_SENDWININICHANGE)	
	owindows := WinGetList(,,,)
    Loop owindows.Length    
    	PostMessage 0x50, 0, Lan, , "ahk_id " windows%A_Index%
}
Why does code 1 work in v1, but code 2 not work in v2?
Is there some incorrect conversion? (There is an error saying this, but seeing the docs, I don't understand why there is an error [In the image it shows just "", but it is "UPtr"])
image.png
image.png (15.11 KiB) Viewed 497 times
image.png
image.png (14.42 KiB) Viewed 495 times
(After this, there are some number of errors saying that Variable not found for all elements in the array owindows)
image.png
image.png (12.83 KiB) Viewed 495 times

Post Reply

Return to “Ask for Help (v2)”