Software/AutoHotKey

[AutoHotKey] 도스창에서 출력하기

rudals.kim 2024. 7. 25. 09:43
반응형
AutoHotkey 1.1.37.02 버전에서 테스트 되었습니다.


변수값을 확인하고 싶은데 매번 MsgBox를 사용했습니다. 도스창에서 변수값을 간단히 확인 할 수 있는 방법이 있지는 않을까하여 검색을 좀 해 보니 일반적인 printf 함수처럼 사용할 수 있는 방법이 있어서 스크랩 해 놓습니다.

 

아래 AutoHotKey 포럼에서 발견하였습니다.

 

printf() - AutoHotkey Community

Report this post @ Quote 05 Dec 2018, 07:49 Are you sure this works? DllCall("AllocConsole") stdin := FileOpen(DllCall("GetStdHandle", "int", -10, "ptr"), "h `n") stdout := FileOpen(DllCall("GetStdHandle", "int", -11, "ptr"), "h `n") dllcall("msvcrt.dll\pr

www.autohotkey.com

 

아래 ConsolePrint함수를 사용하면 도스창에서 값을 출력할 수 있습니다.

ConsolePrint(ByRef vText:="", RW:="")
{
    static vIsReady := 0, oStdOut
    if !vIsReady
    {
        DllCall("kernel32\AllocConsole")
        oStdOut := FileOpen("*", "w `n")
        vIsReady := 1
    }
    if !RW 
    {
        oStdOut.Write(vText)
        oStdOut.Read(0) ;flush the write buffer
    } 
    else
        return oStdOut.ReadAll(), RW:=""
}

ConsolePrint("1 " A_Now "`n")
ConsolePrint("2 " A_Now "`n")


그런데 한가지 문제가 변수값을 확인하기 전에 도스창이 바로 사라집니다.
이것도 AutoHotKey 포럼에서 발견하였는데 약간의 제약사항이 있습니다.

 

 

AutoHotkey Community - share scripts and functions, get answers to your programming questions

The official AutoHotkey community forums - share your scripts or functions, find answers to your questions and discuss topics with other members

www.autohotkey.com

 

  제약조건  

아래 사용된 코드의 AnyKeyWait함수를 보면 idle시간(사용자의 입력이 없는 대기시간)을 체크하기 때문에 마우스를 이동하거나 키보드를 누르는등의 이벤트가 발생되면 바로 대기 상태에서 빠져 버립니다. 특정 제약조건하에서만 사용될 수 있겠습니다.


아래 사용된 코드의 AnyKeyWait함수를 보면 idle시간(사용자의 입력이 없는 대기시간)을 체크하기 때문에 마우스를 이동하거나 키보드를 누르는등의 이벤트가 발생되면 바로 대기 상태에서 빠져 버립니다. 특정 제약조건하에서만 사용될 수 있겠습니다.

전체 테스트 코드입니다.

ConsolePrint(ByRef vText:="", RW:="")
{
    static vIsReady := 0, oStdOut
    if !vIsReady
    {
        DllCall("kernel32\AllocConsole")
        oStdOut := FileOpen("*", "w `n")
        vIsReady := 1
    }
    if !RW 
    {
        oStdOut.Write(vText)
        oStdOut.Read(0) ;flush the write buffer
    } 
    else
        return oStdOut.ReadAll(), RW:=""
}

AnyKeyWait() {
    T := A_TimeIdle
    Loop {
        If (A_TimeIdle - T < 0)
            Break
    }
}

ConsolePrint("1 " A_Now "`n")
ConsolePrint("2 " A_Now "`n")
ConsolePrint("Press any key to continue...")
AnyKeyWait()


실행결과입니다

반응형