Jump to content


How to Interrupt a Toggle?


  • Please log in to reply
5 replies to this topic

#1 AHKHopeful

AHKHopeful
  • Guests

Posted 18 May 2012 - 05:55 PM

I want to create some (hopefully) simple behavior involving two keyboard keys: let's call them ONE and TWO. When ONE is pressed and released, I want it to behave as though the key is constantly being pressed (i.e., key is down), and then when it is pressed again I want it to behave as though ONE is not being pressed (i.e., key is up). So this is just a toggle, and I'm using this code I got from somewhere to do that successfully:

$ONE::
    while GetKeyState(%A_ThisHotkey%,"P")
    {
        sleep 10
    }
    if GetKeyState("ONE")
    {
        send {ONE up}
    } else {
        send {ONE down}
    }
return
Now, 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.

#2 LazyMan

LazyMan
  • Members
  • 450 posts

Posted 18 May 2012 - 07:17 PM

AHKHopeful":3c2k5w4h">

I'm using this code I got from somewhere to do that successfully:

I don't think your code does what you said it does.

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

AHKHopeful
  • Guests

Posted 18 May 2012 - 11:55 PM

Thanks for the response and code! It does work partially, it's pretty close, but there is that problem you mentioned. 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. Anyhow, you've given me some good insight into solving the problem, thanks again.

#4 Alpha Bravo

Alpha Bravo
  • Members
  • 873 posts

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 LazyMan

LazyMan
  • Members
  • 450 posts

Posted 21 May 2012 - 11:14 PM

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.

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:

#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

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.

Does this happen if you use these hotkeys in an editor like Notepad?

#6 Guests

  • 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
return

Very cool, thanks for posting! I will study this one as well.

Does this happen if you use these hotkeys in an editor like Notepad?

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!