Replace [ with { and ] with } using AutoHotkey

autohotkey

I found that in coding I rarely use the square braces [] but use the curly braces {} often. On most keyboards, these are inputted using Shift+[ or Shift+].

Now I tried to use AutoHotkey to remap these keys by using:

[::{{}

Adding {} is raw mode basically, but it didn't work. Next I tried

{[}::{{}

but that also didn't work.

Any help?

Best Answer

Using curly brackets for raw key interpretation is only for the Send commands. So, to map [ to { and ] to } you can use:

[::Send, {{}
]::Send, {}}

Note: Remapping a key with its Shift equivalent is troublesome, as most if not all keyboards send the same scancode each time, the only difference being the introduction of the Shift key (which has its own scancode).

For example, pressing [ on my keyboard sends the scancode 01A and yields a [. Pressing LShift+[ sends the scancodes 02A and 01A, yielding a {.


Update:

I have successfully overcome the scancode issue with some clever logic! Using the following format, you should be able to switch any key with its Shift pair. Key repetition should also work.

*$[::
    if (GetKeyState("Shift"))
        Send, {[}
    else
        Send, {{}  
    return

*$]::
    if (GetKeyState("Shift"))
        Send, {]}
    else
        Send, {}}
    return

Expanding on this idea, @Bob wrote a more robust version of the script:

*$[::
    if (GetKeyState("Shift"))
        SendInput, {[ Down}
    else
        SendInput, {{ Down}
    return

*$]::
    if (GetKeyState("Shift"))
        SendInput, {] Down}
    else
        SendInput, {} Down}
    return

*$[ Up::
    if (GetKeyState("Shift"))
        SendInput, {[ Up}
    else
        SendInput, {{ Up}
    return

*$] Up::
    if (GetKeyState("Shift"))
        SendInput, {] Up}
    else
        SendInput, {} Up}
    return
Related Question