2
votes

folks,

I want to create a layer based keyboard using AutoHotkey. Basicly, I want to achieve what shift already does: modify each key when a modifier is used.

I want to improve regular shift in the following:

  • press modifier once: only change layer for next character
  • hold modifier: change layer as long as modifier is down
  • press modifier twice: enter layer mode, like capslock. (end by another press)

Modifiers: LAlt, RAlt, LControl, RControl (CapsLock, Shift)

How cas I accomplish this?


what I found so far on stackoverflow: This code allows for shift to be pressed and released for the next character

$*LShift:: 
    SendInput, {LShift Down}      ; press shift
    Input, Key, L1 M V            ; wait for input character
    If GetKeyState("LShift", "P") ; if shift still pressed, wait for release
        KeyWait, LShift
    SendInput, {LShift Up}        ; send input with shift down, the shift up
Return

this code turns a double shift press into CapsLock

LShift::
   KeyWait, CapsLock                      ; wait to be released
   KeyWait, CapsLock, D T0.2              ; and pressed again within 0.2 seconds
   if ErrorLevel
      return
   else if (A_PriorKey = "CapsLock")
      SetCapsLockState, % GetKeyState("CapsLock","T") ? "Off" : "On"
   return

#If, GetKeyState("CapsLock", "P")         ; hotkeys go below
    a::b
#If

But I am not experienced enough with AHK to bring this together. My goal is to have something like

Modifier::
    ; code that makes the modifier behave like expected: single press, hold, double press
Return

#If, GetKeyState("Modifier", "P")  ; List of key remaps in specific layer
#If  

I hope this is specific enough and that you can help me out here.

thanks!

1

1 Answers

1
votes

Assign the corresponding Booleam values (true or false) to the variables "Double_LAlt" and "Double_LAlt_holding" in order to create context-sensitive hotkeys depended on their values:

LAlt::
    ToolTip,,,, 3
    ToolTip,,,, 4
    Double_LAlt := false
    ; Press twice or press twice and hold LAlt within 0,2 seconds
    If (A_PriorHotKey = "~LAlt Up" AND A_TimeSincePriorHotkey < 200)
    {
        Sleep, 200      
        If GetKeyState("LAlt","P")
        {               
            ToolTip,,,, 4
            ToolTip, Double_LAlt_holding,,, 2
            Double_LAlt_holding := true
        }
        else
        {   
            ToolTip,,,, 4
            ToolTip, Double_LAlt,,, 3   
            Double_LAlt := true             
        }
    }
    If !((Double_LAlt_holding) || (Double_LAlt))    ; "!" means "NOT" and "||" means "OR"
        ToolTip, LAlt_holding,,, 1
return

~LAlt Up::
    ToolTip,,,, 1
    ToolTip,,,, 2
    Double_LAlt_holding := false
    Sleep, 100
    If (A_TimeIdlePhysical > 100)
        Tooltip, PriorHotKey = LAlt Up,,, 4
    SetTimer, RemoveTooltip, 1000
return


#If (Double_LAlt_holding)   ; If  this variable has the value "true"

    <!a:: MsgBox, a while Double_LAlt_holding       ; "<!" means "LAlt"
    <!1:: MsgBox, 1 while Double_LAlt_holding

#If (Double_LAlt)

    a:: MsgBox, a after Double_LAlt
    1:: MsgBox, 1 after Double_LAlt

    ; Press a key within 2 seconds after releasing LAlt:
#If (A_PriorHotKey = "~LAlt Up" AND A_TimeSincePriorHotkey < 2000)

    a:: MsgBox, a after LAlt Up
    1:: MsgBox, 1 after LAlt Up

#If GetKeyState("LAlt","P")

    a:: MsgBox, a while LAlt_holding
    1:: MsgBox, 1 while LAlt_holding

#If 

RemoveTooltip:
    If (A_TimeSincePriorHotkey > 2000) ;  2 seconds
        ToolTip,,,, 4
return