Macro to toggle key being held?

I got my keyboard yesterday and am trying to figure out if it’s possible (either through a normal key mapping or a smart macro) to toggle if a key is being held or not.

A more specific example being in games that use left shift as sprint, my pinky finger gets quite tired holding it down and not all games have sprint toggle. Being able to just tap the shift key and have it stay “held down” until I tap the key again would be great.

I tried to use this macro, but it seems to act as a normal shift key (only works while I hold the assigned key down).

ifNotShift{
    holdKey leftShift
    setVar shiftHeld 1
}
else {
    if $shiftHeld {
        releaseKey leftShift
        setVar shiftHeld 0
    }
}

There are multiple problems with the macro. The biggest one is that all keys are released when macro ends.

Here is fixed version:

ifNotShift {
    pressKey iLS
    setVar shiftHeld 1
    while $shiftHeld delayUntil 10
}
else {
    setVar shiftHeld 0
}

Also, you may like this variation (if briefly tapped, it locks, otherwise it acts as a normal shift):

holdKey iLS
ifNotShift ifNotInterrupted ifNotPlaytime 500 {
    pressKey iLS
    setVar shiftHeld 1
    while $shiftHeld delayUntil 10
}
else {
    setVar shiftHeld 0
}
2 Likes

Thanks for both options, I was going on my limited experience with autohotkey scripts.

If anyone else stumbles on this I’ll say the first option does exactly what I wanted (shift will toggle on / off)

The second one seems to behave in a way I didn’t know I wanted, but is pretty nice. If I am holding W already and hold shift it acts normal (shift held while you hold it, releases when you let go) but if you tap shift when no other keys are being pressed it will toggle it on / off and then you can sprint around with just the W key.

Thanks again for the fixes, seems like there’s a ton I need to learn about how the macros work.