title: Why does the expression: If (A:=!A) evaluate differently each time a hotkey is pressed?
body: <p>In an Autohotkey script, once a user presses the <em>CapsLock</em> key, a <em>SplashText</em> should pop up indicating that it is ON, or out when it is OFF, the problem here is not about the code that gets it to work but how much simpler it can get.</p>
  
  <p>There are different ways that do it, some need about 25 lines, some use <em>SetTimer</em> and <em>GetKeyState</em> internal functions and several loops to get it to run, some just about five and no loops.</p>
  
  <p>Simplest:</p>
  
  <pre><code>#SingleInstance Force
  SetCapsLockState, Off
  
  
  ~CapsLock::
  If (Tog:=!Tog)
  SplashTextOn ,350 ,35 , Wanted !, [CapsLock] Activated.
  else
  SplashTextOff
  
  return
  </code></pre>
  
  <p>How does: <strong>If (Tog:=!Tog)</strong> get this code running so easily? <strong>Tog</strong> is just a variable not initiated, and each time the <em>CapsLock</em> is pressed it is continuously changing its value from 1 to 0</p>
  
  <p>It seems to be acting as a <em>Flag</em> in the code? what am I missing in this line:</p>
  
  <pre><code>If (Tog:=!Tog)
  </code></pre>
  
  <p>What makes it evaluate different each time?</p>
  
  <p>This is one below is another approach with <strong>A = 0</strong> working as a switch, I made this one and it doesn't quite get as simple as I'd like it to be but it does the job.</p>
  
  <p>Longer code:  </p>
  
  <pre><code>#SingleInstance Force 
  SetCapsLockState, Off
  
  ~CapsLock::
  
      If (A = 0) 
      {
      SplashTextOn ,350 ,35 , Wanted !, [CapsLock] Activated.
      A=1
          }else
          {
          SplashTextOff
          A=0
          }
  
  return 
  </code></pre>
  
  <p>Longest code: </p>
  
  <p>This last one gets the same result but checks the actual physical state of the key It wouldn't rely on <strong>SetCapsLockState, Off</strong> at the top of the script to make sure a switch will do the rest of the work in just one line as the simpler one.</p>
  
  <pre><code>#SingleInstance Force
  #Persistent
  
  SetTimer, StateCapsLockON_Timer, 100
  Return
  
  ~CapsLock::
  
  StateCapsLockON_Timer
      If GetKeyState("CapsLock", "T")
      {
      SetTimer, StateCapsLockOFF_Timer, 100
      SplashTextOn ,350 ,35 , Wanted !, [CapsLock] Activated.
      SetTimer, StateCapsLockON_Timer, Off
      }
   Return
  
  StateCapsLockOFF_Timer:
      If !GetKeyState("CapsLock", "T")
      {
      SetTimer, StateCapsLockOFF_Timer, Off
      SplashTextOff
      SetTimer, StateCapsLockON_Timer, On
      }
  Return
  </code></pre>
  
  <p>Any good ideas about the way <strong>If (Tog:=!Tog)</strong> works in the simplest of these three examples?</p>