6

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?

iglvzx
  • 23,609
darren
  • 61
  • 1
  • 2

3 Answers3

11

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

iglvzx
  • 23,609
3

If I were presented the problem without seeing the answer above, I'd just add a few hotstrings per the example below.

:*?:[::
    SendInput, {Shift Down}`[{Shift up}
    return

:*?:]::
    SendInput, {Shift Down}`]{Shift Up}
    return

:*?:{::
    SendInput, `[
    return

:*?:}::
    SendInput, `]
    return
0

The first answer is bad solution if you use any shortcuts with braces keys, because it broke them. Shortcuts-compatible version here:

$[::Send {{ Down}
$[ Up::Send {{ Up}

+$[::Send {[ Down}
+$[ Up::Send {[ Up}

$]::Send {} Down}
$] Up::Send {} Up}

+$]::Send {] Down}
+$] Up::Send {] Up}

Actually, in programming languages round brackets are used twice as often as curly, so I offer this version:

#NoEnv
#NoTrayIcon
#SingleInstance force
#MaxHotkeysPerInterval 200

; Swap [] with ()
  [::Send {( Down}
  [ Up::Send {( Up}

  ]::Send {) Down}
  ] Up::Send {) Up}

  +9::Send {[ Down}
  +9 Up::Send {[ Up}

  +0::Send {] Down}
  +0 Up::Send {] Up}

; Swap " with '
   $'::Send "
  +$'::Send '
stealzy
  • 41