Sign in to follow this  
Followers 0
Queener

Key press

6 posts in this topic

#1 ·  Posted (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 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.")

Share this post


Link to post
Share on other sites



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

Share this post


Link to post
Share on other sites

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
1 person likes this

Share this post


Link to post
Share on other sites

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.")

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0