Intelligent Auto Close Bracket/Brace/Parenthesis

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pplupo
Posts: 7
Joined: 30 Jan 2023, 22:41

Intelligent Auto Close Bracket/Brace/Parenthesis

Post by pplupo » 30 Jan 2023, 22:45

Hi everyone!
I'm VERY new to AHK. I've written some 5 or 6 scripts in my first 4 days and I'm loving it.

I must say I found this pretty interesting: https://www.autohotkey.com/board/topic/44574-intelligent-auto-close-bracketbraceparen/

However, I cannot get it to work. Here is the error message:

Code: Select all

---------------------------
close_brackets.ahk
---------------------------
Error:  "~ " is not a valid key name.

	Line#
	029: GroupAdd,TextEditors,ahk_class XLMAIN
	030: GroupAdd,TextEditors,ahk_class OpusApp
	033: altkeys = AppsKey : LWin : RWin : LControl : LShift : RShift : LAlt : RAlt:PrintScreen : CtrlBreak : Pause : Break:Space : Tab : Enter : Escape : Delete : Insert : Home : End : PgUp : PgDn : Up : Down : Left : Right:ScrollLock : CapsLock : NumLock:Numpad0 : Numpad1 : Numpad2 : Numpad3 : Numpad4 : Numpad5 : Numpad6 : Numpad7 : Numpad8 : Numpad9:NumpadDown : NumpadLeft : NumpadRight : NumpadUp:NumpadIns : NumpadEnd : NumpadPgDn : NumpadClear : NumpadHome : NumpadPgUp:NumpadDot : NumpadDel :
	051: Loop,Parse,altkeys,:,%A_Space%
	052: Hotkey,"~" A_LoopField,EndcharIsTyped,ON
	055: Chars = -'@#$^&*_{+}:;"|/\,.?! `%	1234567890-=qwertyuiop\asdfghjkl;'zxcvbnm,./''
	056: Loop,Parse,Chars
--->	057: Hotkey,"~" A_LoopField,EndcharIsTyped,ON
	061: Return
	068: if (A_HotString = "(" )  
	069: Send,{del}
	070: if (A_HotString = "{" )  
	071: Send,{del}
	072: if (A_HotString = "[" )  
	073: Send,{del}

The current thread will exit.
---------------------------
OK   
---------------------------
Does anyone know how to fix it?

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

Re: Intelligent Auto Close Bracket/Brace/Parenthesis

Post by mikeyww » 30 Jan 2023, 23:41

Just one idea here: consider starting with something short & simple, also using the current version of AutoHotkey.

Code: Select all

; This script sends a paired brace, bracket, or parenthesis
#Requires AutoHotkey v2.0
paired := Map('(', ')', '[', ']', '{', '}')
~[::
~{::
~(::SendText(paired[SubStr(ThisHotkey, 2)]), Send('{Left}')

pplupo
Posts: 7
Joined: 30 Jan 2023, 22:41

Re: Intelligent Auto Close Bracket/Brace/Parenthesis

Post by pplupo » 31 Jan 2023, 00:29

Thank you, mikeyww!

Unfortunately, I cannot install 2.0 on my work laptop (I don't know why).

I know this seems a bit more complex, but the concept is pretty interesting, and it seems to be "almost ready."

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

Re: Intelligent Auto Close Bracket/Brace/Parenthesis

Post by mikeyww » 31 Jan 2023, 00:54

Hmm, strange, but here is v1.

Code: Select all

; This script sends a paired brace, bracket, or parenthesis
#Requires AutoHotkey v1.1.33
paired := {"(": ")", "{": "}", "[": "]"}
~[::
~{::
~(::
Send % "{Text}" paired[SubStr(A_ThisHotkey, 2)]
Send % "{Left}"
Return

pplupo
Posts: 7
Joined: 30 Jan 2023, 22:41

Re: Intelligent Auto Close Bracket/Brace/Parenthesis

Post by pplupo » 31 Jan 2023, 09:36

I don't want to diverge my own topic, but I'm not an admin, and only some things can be installed without asking for support. Apparently, AHK 1.x is allowed and 2.x is not. I believe it may be a matter of updating their policies, but I'm not sure. There may be more to it.

That other one is context-sensitive.

I found what the problem is.

Line 55, column 33. There's a space character there.
Chars=-'@#$^&*_{+}:;"|/\,.?! ```%`t1234567890-=qwertyuiop\asdfghjkl;'zxcvbnm,./''

If I remove the space, it works.
However, the space character should be represented there in that list, if I understand correctly how this works.
Any idea? I've tried A_Space, but the string just became -'@#$^&*_{+}:;"|/\,.?!A_Space```%`t1234567890-=qwertyuiop\asdfghjkl;'zxcvbnm,./'' :-(

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

Re: Intelligent Auto Close Bracket/Brace/Parenthesis

Post by mikeyww » 31 Jan 2023, 11:02

The Hotkey command wants the key name.

pplupo
Posts: 7
Joined: 30 Jan 2023, 22:41

Re: Intelligent Auto Close Bracket/Brace/Parenthesis

Post by pplupo » 31 Jan 2023, 13:00

Thank you! :-)

I ran the code below:

Code: Select all

	key  := A_Space
	name := GetKeyName(key)
	MsgBox, % Format("Name:`t{}", name)
The key name came as "Space".

I don't know how to join that with the Chars string.
I've tried to join it but it literally adds the word "Space" to it.

Obviously I'm missing on some fundamentals here.

Can you please explain to me how it works? Or, if you don't want to waste too much of your time, pls share with me the code to fix it and I'll try to understand the snippet on my own. Hopefully, I'll learn how to do this.

Thank you again!

pplupo
Posts: 7
Joined: 30 Jan 2023, 22:41

Admin, pls delete (and sorry)

Post by pplupo » 31 Jan 2023, 13:12

I didn't realize that the posts were waiting for approval. That's why I reposted them. I guess I didn't read the message with enough attention.
I won't resend posts again. I'm not impatient, just clueless. I'm sorry.

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

Re: Intelligent Auto Close Bracket/Brace/Parenthesis

Post by mikeyww » 31 Jan 2023, 14:08

Instead of using the A_LoopField in the Hotkey command, you would use the GetKeyName(A_LoopField).

pplupo
Posts: 7
Joined: 30 Jan 2023, 22:41

Re: Intelligent Auto Close Bracket/Brace/Parenthesis

Post by pplupo » 31 Jan 2023, 20:50

thank you! :-)
I really appreciate all the time you have spent on this.

Post Reply

Return to “Ask for Help (v1)”