Search code examples
autohotkey

mac accents for windows breaks ctrl shortcuts


I'm running the ahk script attached below which I found here in order to get accents on my windows machine like that on a mac (meaning instead of pressing command + e , e on a mac to get é I can press alt + e , e on windows to get é). The problem is ctrl + e, ctrl + a etc. have stopped working as intended. There are a couple fixes for this on the bottom of the script for ctrl + a and ctrl + o but even these do not completely restore the function of these commands in all settings, and as far as all the other ctrl + keys (like ctrl + e, which I need), they're missing. I'm wondering if there is any way I can rewrite the code to just accept alt + key accents? I do not need any accents that may be accessible using ctrl as well. If I could do just make the code respond to the alt key and leave the ctrl key alone then hopefully all the usual functions that use the ctrl key wouldn't be affected.


; By Veil (https://www.autohotkey.com/board/topic/27801-special-characters-osx-style), cited by (https://www.aaron-gustafson.com/notebook/mac-like-special-characters-in-windows/)

#UseHook
!SC029::Return  ; grave -> the grave ` accent gave some probs, use the scancode instead (without virtual key VKC0 as in original, which read: !VKC0SC029)
;!`::Return          ; <-- grave doesn't work
!e::Return          ; acute
!i::Return          ; circumflex
!t::Return          ; tilde
!u::Return          ; umlaut

;                  1 2 3 4 5 6 7 8 9 1
;                                    0
;              r   g G a A c C t T u U
*a::diacritic("a","à,À,á,Á,â,Â,ã,Ã,ä,Ä")
*e::diacritic("e","è,È,é,É,ê,Ê,e,E,ë,Ë")
*i::diacritic("i","ì,Ì,í,Í,î,Î,i,I,ï,Ï")
*o::diacritic("o","ò,Ò,ó,Ó,ô,Ô,õ,Õ,ö,Ö")
*u::diacritic("u","ù,Ù,ú,Ú,û,Û,u,U,ü,Ü")
*n::diacritic("n","n,N,n,N,n,N,ñ,Ñ,n,N")
*y::diacritic("y","y,Y,y,Y,y,Y,y,Y,ÿ,Ÿ")  

diacritic(regular,accentedCharacters) {
    StringSplit, char, accentedCharacters, `,
    graveOption            := char1
    graveShiftOption       := char2
    acuteOption            := char3
    acuteShiftOption       := char4
    circumflexOption       := char5
    circumflexShiftOption  := char6
    tildeOption            := char7
    tildeShiftOption       := char8
    umlautOption           := char9
    umlautShiftOption      := char10
    
    if (A_PriorHotKey = "!SC029" && A_TimeSincePriorHotkey < 2000) {
        if (GetKeyState("Shift")) {
            SendInput % graveShiftOption
        } else {
            SendInput % graveOption
        }
    } else if (A_PriorHotKey = "!e" && A_TimeSincePriorHotkey < 2000) {
        if (GetKeyState("Shift")) {
            SendInput % acuteShiftOption
        } else {
            SendInput % acuteOption
        }
    } else if (A_PriorHotKey = "!i" && A_TimeSincePriorHotkey < 2000) {
        if (GetKeyState("Shift")) {
            SendInput % circumflexShiftOption
        } else {
            SendInput % circumflexOption
        }       
    } else if (A_PriorHotKey = "!t" && A_TimeSincePriorHotkey < 2000) {
        if (GetKeyState("Shift")) {
            SendInput % tildeShiftOption
        } else {
            SendInput % tildeOption
        }
    } else if (A_PriorHotKey = "!u" && A_TimeSincePriorHotkey < 2000) {
        if (GetKeyState("Shift")) {
            SendInput % umlautShiftOption
        } else {
            SendInput % umlautOption
        }
    } else {
        if (GetKeyState("Shift") or GetKeyState("Capslock","T")) {
            SendInput % "+" regular
        } else {
            SendInput % regular
        }
    }
}

; Fix for some CTRL + stuff that no longer works because of the script above:
; TODO - Add more as we find them
^a::Send ^{end}^+{home}  ;this makes ctrl + a select all again
^o::WinMenuSelectItem, A, , File, Open


I tried finding all instances of ctrl in the script (^ in ahk language) but there were none so I'm assuming it's the lines beginning with an asterisk which I've just learnt can accept any key before them like ctrl, alt, shift etc. As a total newbie to ahk language (this is the first I'm trying to run) I'm not sure what to do!


Solution

  • This works here:

    #UseHook
    !SC029::Return  ; grave -> the grave ` accent gave some probs, use the scancode instead (without virtual key VKC0 as in original, which read: !VKC0SC029)
    ;!`::Return          ; <-- grave doesn't work
    !e::Return          ; acute
    !i::Return          ; circumflex
    !t::Return          ; tilde
    !u::Return          ; umlaut
    
    ;                  1 2 3 4 5 6 7 8 9 1
    ;                                    0
    ;              r   g G a A c C t T u U
    *a::diacritic("a","à,À,á,Á,â,Â,ã,Ã,ä,Ä")
    *e::diacritic("e","è,È,é,É,ê,Ê,e,E,ë,Ë")
    *i::diacritic("i","ì,Ì,í,Í,î,Î,i,I,ï,Ï")
    *o::diacritic("o","ò,Ò,ó,Ó,ô,Ô,õ,Õ,ö,Ö")
    *u::diacritic("u","ù,Ù,ú,Ú,û,Û,u,U,ü,Ü")
    *n::diacritic("n","n,N,n,N,n,N,ñ,Ñ,n,N")
    *y::diacritic("y","y,Y,y,Y,y,Y,y,Y,ÿ,Ÿ")  
    
    diacritic(regular,accentedCharacters) {
        StringSplit, char, accentedCharacters, `,
        graveOption            := char1
        graveShiftOption       := char2
        acuteOption            := char3
        acuteShiftOption       := char4
        circumflexOption       := char5
        circumflexShiftOption  := char6
        tildeOption            := char7
        tildeShiftOption       := char8
        umlautOption           := char9
        umlautShiftOption      := char10
        
        if (GetKeyState("Ctrl")) {
            SendInput % "^" regular     
         } 
         else if (A_PriorHotKey = "!SC029" && A_TimeSincePriorHotkey < 2000) {
            if (GetKeyState("Shift")) {
                SendInput % graveShiftOption
            } else {
                SendInput % graveOption
            }
        } else if (A_PriorHotKey = "!e" && A_TimeSincePriorHotkey < 2000) {
            if (GetKeyState("Shift"))  {
                SendInput % acuteShiftOption
            } else {
                SendInput % acuteOption
            }
        } else if (A_PriorHotKey = "!i" && A_TimeSincePriorHotkey < 2000) {
            if (GetKeyState("Shift"))  {
                SendInput % circumflexShiftOption
            } else {
                SendInput % circumflexOption
            }       
        } else if (A_PriorHotKey = "!t" && A_TimeSincePriorHotkey < 2000) {
            if (GetKeyState("Shift"))  {
                SendInput % tildeShiftOption
            } else {
                SendInput % tildeOption
            }
        } else if (A_PriorHotKey = "!u" && A_TimeSincePriorHotkey < 2000) {
            if (GetKeyState("Shift"))  {
                SendInput % umlautShiftOption
            } else {
                SendInput % umlautOption
            }
        } else {
            if (GetKeyState("Shift")  or GetKeyState("Capslock","T")) 
            {
                SendInput % "+" regular     
            } 
            else 
            {
                SendInput % regular
            }
        }
    }