Windows Command Prompt Console ~

Get help with using AutoHotkey and its commands and hotkeys
anotherautohotkeyusr
Posts: 21
Joined: 27 Oct 2015, 18:45

Windows Command Prompt Console ~

21 Feb 2019, 11:56

After years of annoyance that the Windows Command Prompt console does not use tilde as Bash et cetera does I had enough and put together the below script. Please share any opportunities you see for improvement.

Code: Select all

#IfWinActive, ahk_exe cmd.exe
~::
    If ( A_ThisHotKey = "~" )
    {
        Key := "Tilde"
    }
    Loop, 1
    {
        Goto % Key . %Key%Index
        Tilde1:
                Send, {bs 1}`%USERPROFILE`%
                Break
        Tilde2:
                Length := StrLen("%USERPROFILE%")
                Send, {bs %Length%}%USERPROFILE%
                Break
        Tilde3:
                Length := StrLen(USERPROFILE)
                Send, {bs %Length%}~
                Break
        Tilde:
                Send, ~
    }
    If ( %Key%Index != 3 )
    {
        %Key%Index += 1
    }
    Else
    {
        %Key%Index :=
    }
Return
#IfWinActive
Thank you.

-rob-
User avatar
evilC
Posts: 4770
Joined: 27 Feb 2014, 12:30

Re: Windows Command Prompt Console ~

21 Feb 2019, 12:27

If ( A_ThisHotKey = "~" ) is always true

Goto % Key . %Key%Index WTF ?!?!
What exactly are you trying to achieve here? "Key" is literal, and undefined, so will never do anything. Same deal with "Index"
This code is syntactically identical to Goto % %Key%

I also don't see why you are using a convoluted combination of GoTo with nested labels (ewwww), along with double dereferencing, when a plain and simple if/else would be fine.
User avatar
evilC
Posts: 4770
Joined: 27 Feb 2014, 12:30

Re: Windows Command Prompt Console ~

21 Feb 2019, 12:31

Code: Select all

Presses := 1

#IfWinActive, ahk_exe cmd.exe
~::
	if (Presses == 1){
		Send, {bs 1}`%USERPROFILE`%
	} else if (Presses == 2){
		Length := StrLen("%USERPROFILE%")
		Send, {bs %Length%}%USERPROFILE%
	} else if (Presses == 3){
		Length := StrLen(USERPROFILE)
		Send, {bs %Length%}~
	} else {
		Send, ~
	}
	Presses++
	if (Presses > 3)
		Presses := 1
	return
#IfWinActive
anotherautohotkeyusr
Posts: 21
Joined: 27 Oct 2015, 18:45

Re: Windows Command Prompt Console ~

21 Feb 2019, 16:08

Hi evilC,

There are certain symbolic characters that are invalid in labels. I wanted the code to be generic, so that it can be functioned to handle any key. ~ is just an example. I wanted the labels to be dynamically generated based on the key. I do not wish to be contrary, however I would ask you to please consider and run the following example code:

Code: Select all

`::
~::
    DynamicLabelDemo( A_ThisHotKey )
Return

Grave1:
    OutputDebug, "This is Grave1!"
Return

Grave2:
    OutputDebug, "This is Grave2!"
Return

Grave3:
    OutputDebug, "This is Grave3!"
Return

Grave4:
    OutputDebug, "This is Grave4!"
Return

Grave5:
    OutputDebug, "This is Grave5!"
Return

Tilde1:
    OutputDebug, "This is Tilde1!"
Return

Tilde2:
    OutputDebug, "This is Tilde2!"
Return

Tilde3:
    OutputDebug, "This is Tilde3!"
Return

Tilde4:
    OutputDebug, "This is Tilde4!"
Return

Tilde5:
    OutputDebug, "This is Tilde5!"
Return

DynamicLabelDemo( Key )
{
    OutputDebug, % "Key: " . Key
    If ( Key = "``" )
        Key := "Grave"
    If ( Key = "~" )
        Key := "Tilde"
    LabelPrefix := Key
    OutputDebug, % "LabelPrefix: " . LabelPrefix
    Loop, 5
    {
        %Key%Index := A_Index
        LabelSuffix := % %Key%Index
        OutputDebug, % "LabelSuffix: " . LabelSuffix
        DynamicLabel := LabelPrefix . LabelSuffix
        OutputDebug, % "DynamicLabel: " . DynamicLabel
        Gosub, % DynamicLabel
    }
}
-rob-
User avatar
evilC
Posts: 4770
Joined: 27 Feb 2014, 12:30

Re: Windows Command Prompt Console ~

21 Feb 2019, 17:07

Ah, i see what you were trying to do now - yeah, makes a lot more sense now.
There are more elegant ways to achieve the same result tho, this problem is perfect for solving using a class

Code: Select all

#SingleInstance force

Tilde := new Cycler("~", Func("TildeFunc"), 3)
Grave := new Cycler("``", Func("GraveFunc"), 5)
return

TildeFunc(count){
	if (count == 1){
		;~ Send, {bs 1}`%USERPROFILE`%
		Tooltip 1
	} else if (count == 2){
		Length := StrLen("%USERPROFILE%")
		;~ Send, {bs %Length%}%USERPROFILE%
		Tooltip 2
	} else if (count == 3){
		Length := StrLen(USERPROFILE)
		;~ Send, {bs %Length%}~
		Tooltip 3
	} else {
		Tooltip 4
		;~ Send, ~
	}
}

GraveFunc(count){
	Tooltip % "Grave count: " count
}

class Cycler {
	__New(keyName, callback, max := 3){
		this.KeyName := keyName
		this.Callback := callback
		this.Max := max
		
		fn := this.KeyEvent.Bind(this)
		hotkey, % "$" keyName, % fn
	}
	
	KeyEvent(){
		static count := 1
		this.Callback.Call(count)
		count++
		if (count > this.Max)
			count := 1
	}
}

^Esc::
	ExitApp
anotherautohotkeyusr
Posts: 21
Joined: 27 Oct 2015, 18:45

Re: Windows Command Prompt Console ~

22 Feb 2019, 14:04

Classes in AHK!?! D'oh! Thank you very much for the guidance evilC, it is much appreciated. After seeing your example I am now suspecting that AHK might support arrays, in which case we could eliminate the key specific functions too. This is fun! I'm off to investigate arrays.

-rob-

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], tEdward, Wigi and 150 guests