$ONE::
while GetKeyState(%A_ThisHotkey%,"P")
{
sleep 10
}
if GetKeyState("ONE")
{
send {ONE up}
} else {
send {ONE down}
}
returnNow, TWO is just a regular keyboard key that I'm not assigning any special behavior to. But if TWO is down then I want it to temporarily toggle ONE off if ONE is currently toggled on, but TWO should just behave normally if ONE is toggled off. In other words, if the user holds down TWO manually on the keyboard then it should interrupt ONE if ONE is toggled on, and when the user releases TWO then, if ONE was interrupted, it should be toggled back on. Thanks for any help you can provide.
How to Interrupt a Toggle?
#1
AHKHopeful
Posted 18 May 2012 - 05:55 PM
#2
Posted 18 May 2012 - 07:17 PM
AHKHopeful":3c2k5w4h">I don't think your code does what you said it does.I'm using this code I got from somewhere to do that successfully:
I think this code does what you want:
#MaxThreadsPerHotkey 2
$1::
KeyWait 1
Toggle := (! Toggle)
While ( Toggle && ! TogglePaused )
{
Send 1
Sleep 100
}
Return
$2::
If ( Toggle )
{
TogglePaused := True
KeyWait 2
TogglePaused := False
}
Else
Send 2
Return
:shock: Caveat :!:: If I hold down key 1 until my autorepeat begins, I am unable to toggle off the stream of 1s using key 1. I don't yet understand why. :oops:
#3
AHKHopeful
Posted 18 May 2012 - 11:55 PM
#4
Posted 20 May 2012 - 03:47 AM
Hotkey, $1, One
return
One:
if Toggle1 := !Toggle1
{
KeyWait 1
SetTimer, Press1, 30
return
}
SetTimer, Press1, off
Return
Press1:
send, 1
return
2::
if Toggle2 := !Toggle2
Hotkey, $1, One, Off
else
Hotkey, $1, One, On
return
#5
Posted 21 May 2012 - 11:14 PM
Leef_me explained the issue. When I changed the Sleep time from 100 to 1 the code behaved as expected, even when key 1 was held down longer than the repeat delay:Caveat :!:: If I hold down key 1 until my autorepeat begins, I am unable to toggle off the stream of 1s using key 1. I don't yet understand why.
#MaxThreadsPerHotkey 2
$1::
KeyWait 1
Toggle := (! Toggle)
While ( Toggle && ! TogglePaused )
{
Send 1
[color=#FF0000]Sleep 1[/color]
}
Return
$2::
If ( Toggle )
{
TogglePaused := True
KeyWait 2
TogglePaused := False
}
Else
Send 2
Return
Does this happen if you use these hotkeys in an editor like Notepad?Also, if 1 has not been toggled on and I hit 2, something weird happens and a bunch of keypresses seem to occur in rapid succession.
#6
Guests
Posted 25 May 2012 - 04:20 AM
Hotkey, $1, One
return
One:
if Toggle1 := !Toggle1
{
KeyWait 1
SetTimer, Press1, 30
return
}
SetTimer, Press1, off
Return
Press1:
send, 1
return
2::
if Toggle2 := !Toggle2
Hotkey, $1, One, Off
else
Hotkey, $1, One, On
returnVery cool, thanks for posting! I will study this one as well.Actually it was an issue with some existing application hotkeys; it had nothing to do with your script. Anyway, thanks for updating the script, it works great!Does this happen if you use these hotkeys in an editor like Notepad?




