Queener Posted October 4, 2014 Posted October 4, 2014 (edited) I'm trying to create a hotkey to loop a function and stop the function if it's pressed again. Say I wanted to paste the word "Test" with a loop. If I press 1 then it kept typing test until I press 1 again. I'm just not sure how to accomblish that. I have the hotkey + loop function; just no the key press to start and stop. ex: hotkeyset("numpad1", "_Loop") hotkeyset("{ES}", "_Exit") while 1 sleep(1) wend Func _Exit() exit EndFunc Func _Loop() while 1 sleep(500) send("test") WEnd EndFunc EDIT: Tested this and it work, but the exitloop doesn't work if I assigned to the same key press as the hotkeyset. I would assume it tries to stop and then recall the same function again which kept the loop ongoing. Func Loop() while 1 sleep(1) If _IsPressed("09", $hDLL) Then ExitLoop DllClose($hDLL) Return EndIf send("Test") WEnd EndFunc Edited October 4, 2014 by asianqueen Msgbox(0, "Hate", "Just hate it when I post a question and find my own answer after a couple tries. But if I don't post the question, I can't seem to resolve it at all.")
kylomas Posted October 4, 2014 Posted October 4, 2014 Something like this... HotKeySet("p", "_Loop") hotkeyset("{ESC}", "_Exit") local $on = false while 1 sleep(99999) wend Func _Exit() exit EndFunc Func _Loop() $on = not $on while 1 if not $on then return sleep(500) send("_test") WEnd EndFunc Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill
Solution mikell Posted October 4, 2014 Solution Posted October 4, 2014 You can even continue an interrupted func (by using a static var for example) HotKeySet("p", "_Loop") hotkeyset("{ESC}", "_Exit") Global $on = false while 1 sleep(10) wend Func _Exit() exit EndFunc Func _Loop() Local Static $i = 10 $on = not $on while $on $i += 1 beep($i*40, 500) WEnd EndFunc jguinch 1
jguinch Posted October 4, 2014 Posted October 4, 2014 Very good example for Static use Mikell ! Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF
Queener Posted October 4, 2014 Author Posted October 4, 2014 Thanks Mikell and Kylosmas. This is a great example. Msgbox(0, "Hate", "Just hate it when I post a question and find my own answer after a couple tries. But if I don't post the question, I can't seem to resolve it at all.")
mikell Posted October 4, 2014 Posted October 4, 2014 @jguinch Thanks and meow Note that the toggle var can also be declared as local static, certainly a better way than global HotKeySet("p", "_Loop") hotkeyset("{ESC}", "_Exit") while 1 sleep(10) wend Func _Exit() exit EndFunc Func _Loop() Local Static $on = false, $i = 10 $on = not $on while $on $i += 1 beep($i*40, 500) WEnd EndFunc
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now