Mistakamikaze
Mistakamikaze

Reputation: 442

How to represent "backspace" key in autohotkey

My keyboard is buggy right now and keys are double typing when they're not meant to. I found this autohotkey script online that ignores keystrokes if they're registered again in a very short timespan. The code works by listing all the broken keys contiguously in a string but I'm not sure how to list my backspace key. The documentation states the backspace is key is just backspace but it's not working for me. This is what I have so far.

;List all your broken keys between quotes below. I.e. if your broken keys are g and f then the line below shoud be 
;brokenKeys := "gf"
brokenKeys := "fosbaituwrvlk"

;timepan in which subsequent keystrokes should be ignored.
fixOffset := 80

;These are typical values for a starter AHK script
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

;This array will hold timers for each broken key
lastTimePressed := {}

;Create timer with current time for each broken key
Loop, Parse, brokenKeys
{
    lastTimePressed[A_LoopField] := A_TickCount
}

;lastTimePressed := A_TickCount

;Assign a hotkey handler for each broken key
Loop, Parse, brokenKeys
{    
    keyName := 
    Hotkey, $%A_LoopField%, HotKeyHandler 
}

HotKeyHandler:    
    pressedKey := SubStr(pressedKey,2)SubStr(A_ThisHotKey,2)    
    sinceLastPress := A_TickCount - lastTimePressed[pressedKey]
    if (sinceLastPress > fixOffset) { ;send the hijacked key only when sufficient time has passed
        lastTimePressed[pressedKey] := A_TickCount
        Send %pressedKey%
    }
return

EDIT: I've updated my code. Currently after I click backspace, the backspace key registers but after that all the brokenKeys no longer respond. From debugging, the pressedKey variable seems to have been affected by clicking backspace and so lastTimePressed[pressedKey] is not working in the hotKeyHandler function.

;List all your broken keys
brokenKeys := "f|o|s|b|a|i|t|u|w|r|v|l|k|'|backspace"

;timepan in which subsequent keystrokes should be ignored.
fixOffset := 80

;These are typical values for a starter AHK script
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

;This array will hold timers for each broken key
lastTimePressed := {}

;Create timer with current time for each broken key
Loop, Parse, brokenKeys, |
{
    lastTimePressed[A_LoopField] := A_TickCount
}

;lastTimePressed := A_TickCount

;Assign a hotkey handler for each broken key
Loop, Parse, brokenKeys, |
{    
    keyName := 
    Hotkey, $%A_LoopField%, HotKeyHandler 
}

HotKeyHandler:
    pressedKey := SubStr(pressedKey,2)SubStr(A_ThisHotKey,2)
    sinceLastPress := A_TickCount - lastTimePressed[pressedKey]
    if (sinceLastPress > fixOffset) { ;send the hijacked key only when sufficient time has passed
        lastTimePressed[pressedKey] := A_TickCount
        if (pressedKey == "backspace"){
            Send {backspace}
        } else {
            Send %pressedKey%
        }
    }
return

Upvotes: 2

Views: 2073

Answers (2)

Mistakamikaze
Mistakamikaze

Reputation: 442

Managed to get a solution from here

;List all your broken keys
brokenKeys := "f|o|s|b|a|i|t|u|w|r|v|l|k|Backspace"

;timepan in which subsequent keystrokes should be ignored.
fixOffset := 80

;These are typical values for a starter AHK script
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

;This array will hold timers for each broken key
lastTimePressed := {}

;Create timer with current time for each broken key
Loop, Parse, brokenKeys, |
    lastTimePressed[A_LoopField] := A_TickCount

;lastTimePressed := A_TickCount

;Assign a hotkey handler for each broken key
Loop, Parse, brokenKeys, |
{    
    keyName := 
    Hotkey, $%A_LoopField%, HotKeyHandler 
}
Return
HotKeyHandler:    
    pressedKey := SubStr(A_ThisHotKey,2) ;<<<< corrected  
    sinceLastPress := A_TickCount - lastTimePressed[pressedKey]
    if (sinceLastPress > fixOffset) { ;send the hijacked key only when sufficient time has passed
        lastTimePressed[pressedKey] := A_TickCount
        Send {%pressedKey%} ;<<<< corrected
    }
return

Upvotes: 2

T_Lube
T_Lube

Reputation: 331

The problem here is that the parsing loop can accept a delimiter to parse on, otherwise it will just parse individual characters in a string.

;List all your broken keys between quotes below. I.e. if your broken keys are g and f then the line below should be 
;brokenKeys := "gf"
;brokenKeys := "fosbaituwrvlk"
brokenKeys := "f|o|s|b|a|i|t|u|w|r|v|l|k|backspace"

;timepan in which subsequent keystrokes should be ignored.
fixOffset := 80 ; MILLISECONDS

;These are typical values for a starter AHK script
#NoEnv
SendMode Input
SetWorkingDir %A_ScriptDir%

;This array will hold timers for each broken key
lastTimePressed := {}

;Create timer with current time and hotkey for each broken key
Loop, Parse, brokenKeys, |
{
    lastTimePressed[A_LoopField] := A_TickCount
    Hotkey, ~$%A_LoopField%, HotKeyHandler
}
 ; YOUR VERSION IS PARSING INDIVIDUAL CHARACTERS
 ; THIS ONE IS USING "|" TO SEPARATE THE KEY NAMES
 ; NOW YOU CAN USE A KEY NAME COMPOSED OF MORE THAN ONE CHARACTER
 ; JUST SEPARATE EACH ONE WITH A "|" IN THE 'BROKENKEYS' VARIABLE ABOVE.
 ; I ALSO INCLUDED BOTH LOOP ACTIONS IN ONE LOOP, INSTEAD OF TWO.
 ; WHICH SAVES TIME IN LOADING, ALBEIT MARGINALLY.
Return
 
 HotKeyHandler:    
    pressedKey := SubStr(pressedKey,2)SubStr(A_ThisHotKey,2)    
    sinceLastPress := A_TickCount - lastTimePressed[pressedKey]
    if (sinceLastPress > fixOffset) { ;send the hijacked key only when sufficient time has passed
        lastTimePressed[pressedKey] := A_TickCount
        Send %pressedKey%
    }
return

Upvotes: 0

Related Questions