wedi
wedi

Reputation: 1422

AutoHotKey v2.0.2: How to trigger HotKey on square bracket

I am trying to remap the opening square bracket ([) to a lower case u-umlaut (ü) and the opening curly bracket ({ / Shift+[) to the upper case U-umlaut (Ü) on a US keyboard layout using a AutoHotkey script.

The following works very well with ; to ö and : to Ö but the Variantes for ü and Ü don't. I suspect it's because a square bracket has a special meaning but there is no error about Syntax and AutoHotkey's escape Syntax with ` does not apply here.

I believe that the problem is the trigger $[:: because the first block with KeyWait alone without the second Up part does not prevent me typing [ while it does when I try that with ;.

Please explain what I am doing wrong.

#Requires AutoHotkey v2.0
#SingleInstance Force

LongPressDelay := 220

; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; O-Umlaut | ; => ö und : => Ö

$;::
{
    KeyWait ";"
    return
}

$; Up::
{
    If (A_PriorHotKey = "$;" AND A_TimeSincePriorHotkey < LongPressDelay)
        Send ";"
    else
        Send "ö"
    return
}

$+;::
{
    KeyWait ";"
    return
}

$+; Up::
{
    If (A_PriorHotKey = "$+;" AND A_TimeSincePriorHotkey < LongPressDelay)
        Send ":"
    else
        Send "Ö"
    return
}


; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; U-Umlaut| [ => ü und { => Ü

$[::
{
    KeyWait "["
    return
}

$[ Up::
{
    If (A_PriorHotKey = "$[" AND A_TimeSincePriorHotkey < LongPressDelay)
        Send "["
    else
        Send "ü"
    return
}

$+[::
{
    KeyWait "["
    return
}

$+[ Up::
{
    If (A_PriorHotKey = "$+[" AND A_TimeSincePriorHotkey < LongPressDelay)
        Send "{"
    else
        Send "Ü"
    return
}


; EDIT: My working solution per user3419297's answer
; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; U-Umlaut| [ => ü und { => Ü

$SC01A::
{
    KeyWait "SC01A"
    return
}

$SC01A Up::
{
    If (A_PriorHotKey = "$SC01A" AND A_TimeSincePriorHotkey < LongPressDelay)
        Send "["
    else
        Send "ü"
    return
}

$+SC01A::
{
    KeyWait "SC01A"
    return
}

$+SC01A Up::
{
    If (A_PriorHotKey = "$+SC01A" AND A_TimeSincePriorHotkey < LongPressDelay)
        SendText "{"
    else
        Send "Ü"
    return
}

Script based on https://www.autohotkey.com/board/topic/80697-long-keypress-hotkeys-wo-modifiers/?p=689354

Context:

As a Developer it's super helpful to code using the US keyboard layout because all the special characters are within easy reach. As a native German speaker I'd like to access our beloved Umlauts at their traditional spots because I'm used to it.

On MacOS I solved this with Karabiner and Umlauts on long press. I want to replicate this on Windows.

Upvotes: 0

Views: 1315

Answers (2)

Relax
Relax

Reputation: 10636

In the rare case where a key has no name or the standard code doesn't work, Hotkeys can be triggered by using the 3-digit hexadecimal scan code (SC) of a key.

The scan code of a key can be determined by following the steps at Special Keys:

  • Run a script with keyboard hook
  • open the AutoHotkey window (context menu in systray, Open item)
  • go to View > Key History and script info
  • type the key
  • hit F5 to see the code

Upvotes: 1

Kowai Monkey
Kowai Monkey

Reputation: 11

Trying the same right here, using a different code though.

https://www.autohotkey.com/boards/viewtopic.php?f=9&t=99809

I managed to migrate it to V2 and changed the keys to match the German Keyboard layout.

[ = ü
' = ä
; = ö
- = ß

Please feel free to try:

$[::
$+[::
$'::
$+'::
$;::
$+;::
$-::
{
    umlaut_pairs := Map("$[" , "ü", "$'" , "ä", "$;" , "ö", "$-" , "ß", "$+[" , "Ü", "$+'" , "Ä", "$+;" , "Ö")
    umlaut := umlaut_pairs[A_ThisHotkey]
    Send SubStr(A_ThisHotkey,2) ; input the letter first, otherwise input order might be wrong
    KeyWait Substr(A_ThisHotkey,-1), "T0.2" ; call only the specific character without "$" or "+" and show input of the pressed key
    if(A_TimeSinceThisHotkey>200) ; if pressed more than 200ms 
        {
        Send "{BackSpace}" ; backspace previous letter 
        Send umlaut_pairs[A_ThisHotkey] ; Replace acc. to the map
        KeyWait Substr(A_ThisHotkey,-1), "T0.2"
    ;   }
    }
}

Upvotes: 1

Related Questions