davidhsonic
davidhsonic

Reputation: 31

I want a simple program to freeze/unfreeze input when I press a key

This seems like such a simple thing, yet I can't find it anywhere. I want a simple program (like AutoHotkey, but I can't find a way to do it with AutoHotkey) that will freeze my keyboard and mouse (whatever I'm pressing at the time keeps being pressed, even if I release the actual key/button) when I press a certain key, and keep it frozen until I press that key again (with the chosen key never being considered pressed by other programs).

I just want this so that if a game expects me to hold down some buttons, I can press the buttons, press the designated key, let go, then press the key again when I'm supposed to release the buttons.

Upvotes: 1

Views: 509

Answers (1)

Eli
Eli

Reputation: 192

This works in text editors. Not sure how it will work in games though.

#NoEnv
#Warn
#SingleInstance Force
#UseHook
SetWorkingDir %A_ScriptDir%
SendMode Input

Global isHold := false ; Alternates between "true" and "false" at each "LShift" press.

; Hotkey below will only be active if the game's window is active
#IfWinActive "PutYourGameWindowTitleHere"

; Sends {LShift Down} if isHold == false, {LShift Up} if isHold == true
; Asterisk means it will work even if other keys are held at the same time.
*LShift::Send % (isHold := !isHold) ? "{LShift Down}" : "{LShift Up}"

#IfWinActive 

Update: Again, testing this in games you will have to do yourself.

#NoEnv
#Warn
#SingleInstance Force
#UseHook
SetWorkingDir %A_ScriptDir%
SendMode Input

Global aKeysToFreeze := { LShift: false, a: false }, isFreezeOn := false

`::fToggleFreeze()

^`:: ; You can use this to check the logical state of the key
If GetKeyState("a")
    msgbox Key is pressed.
else msgbox Key is not pressed.
return

fToggleFreeze() {
    Local
    Global aKeysToFreeze, isFreezeOn
    
    If isFreezeOn {                          ; If there are frozen keys,
        For sKeyName, isKeyFrozen in aKeysToFreeze {
            Send % "{" sKeyName " Up}"       ; send key up event for each key and
            aKeysToFreeze[sKeyName] := false ; set the each key's frozen state to false
        }
    } else {
        For sKeyName, isKeyFrozen in aKeysToFreeze
            If GetKeyState(sKeyName, "P")     ; If the key is physically pressed
                aKeysToFreeze[sKeyName] := true  ; set frozen state to true
    }
    isFreezeOn := !isFreezeOn    ; Frozen mode toggle
}

*a::Send {Blind}{a DownR}
*LShift:: Send {Blind}{LShift DownR}

#If !aKeysToFreeze["a"] ; If the key is frozen, the key up hotkey is blocked
*a Up::Send {Blind}{a Up}

#If !aKeysToFreeze["LShift"]
*LShift Up::Send {Blind}{LShift Up}
#If

Upvotes: 0

Related Questions