Du hast zwei Whileschleifen. Einmal die des Fensters und die mit dem $On. Du bist in der ersten Schleife, also wird die zweite erst aufgerufen, wenn die erste verlassen wurde. Du musst beide Schleifen kombinieren.
Grund: Programmiersprachen laufen von oben nach unten durch (Sprünge/Funktionen,... mal abgesehen).
Generell solltest du keinen Code nach/zwischen Funktionen schreiben, das ist schlechter Stil, weil es sehr schnell unübersichtlich wird.
Tipp: Um ein boolean zu invertieren reicht $On=Not $On
Ich hab einen Timer hinzugefügt, damit während der 5 Sekunden warten das Fenster weiterhin reagiert.
AutoIt
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
Global $bIsOn = False,$iTimer
HotKeySet("s","StartStop")
HotKeySet("e", "_Exit")
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 220, 148, 258, 227)
$Label1 = GUICtrlCreateLabel("Start/Stop = s", 16, 8, 124, 29)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
$Label2 = GUICtrlCreateLabel("Exit = e", 16, 40, 69, 29)
GUICtrlSetFont(-1, 15, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
EndSwitch
If $bIsOn Then
If TimerDiff($iTimer)>5000 Then ;damit nur alle 5 Sekunden gesendet wird
;den zeitunterschied zwischen TimerInit und diesem Ausruf prüfen
Send(1)
$iTimer=TimerInit() ;Timer aktualisieren
EndIf
EndIf
WEnd
Func _Exit()
Exit
EndFunc
Func StartStop()
$bIsOn = Not $bIsOn
EndFunc
Alles anzeigen