Jump to content
Sign in to follow this  
Surma

Function intended to run once keeps looping

Recommended Posts

Surma

Hello.

I'm kinda new to autoit and this is probably some small mistake, but anyway.

I am making a script which is supposed to run a function once when a key is pressed. Something like this:

HotKeySet("g","_func")

Func _func ()
    ; Code
EndFunc

While 1
    Sleep(50)
WEnd

The problem is that when g is pressed, ; Code just keeps getting executed over and over again. I only want it to run once. How do I achieve this?

Share this post


Link to post
Share on other sites
bogQ

The problem is that when g is pressed, ; Code just keeps getting executed over and over again. I only want it to run once. How do I achieve this?

fast way to do it is to block it untill the key is up

#include <Misc.au3>

HotKeySet("g","_func")

While 1
    Sleep(10)
WEnd

Func _func ()
    ; Code
    Do
        Sleep(10)
    Until Not _IsPressed("47");G
EndFunc

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)



460px-Thief-4-temp-banner.jpg
There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

 

Share this post


Link to post
Share on other sites
DrKovra

Well, if you ONLY want to run it once, then this will do.

HotKeySet("g","_func")

Func _func ()
    ; Code
    Exit
EndFunc

While 1
    Sleep(50)
WEnd

Add the "Exit" command after your code :)


[s][font="Impact"]░▒▓▓►DrKovra◄▓▓▒░[/font][/s]The only thing I [sup]know [/sup]is that I don't know [sub]nothing[/sub]--------------- __________------------------------------ __________---------------

Share this post


Link to post
Share on other sites
Surma

Well, if you ONLY want to run it once, then this will do.

HotKeySet("g","_func")

Func _func ()
    ; Code
    Exit
EndFunc

While 1
    Sleep(50)
WEnd

Add the "Exit" command after your code :)

Exit would cause the whole script to exit, wouldn't it? Or is it just the function that exists?

Anyway, I'm not sure that I completely understand why it behaves as it does. I'd really like to understand that and why your suggestions fixes it.

Share this post


Link to post
Share on other sites
Jos

You haven't specified what code is in the _Func() that could cause this behaviour.

The posted code will work fine and only execute the Func one time when the HotKey is pressed.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
PsaltyDS

@Surma: This typically happens because something you are doing in _Func() is sending a synthetic "g" keystroke, which fires the hotkey again in a loop. You didn't answer to forumer100's post above showing how to avoid that.

:)

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites
Surma

@Surma: This typically happens because something you are doing in _Func() is sending a synthetic "g" keystroke, which fires the hotkey again in a loop. You didn't answer to forumer100's post above showing how to avoid that.

:)

Sorry, must've scrolled past him.

Uh, yes indeed I am using send, among other things, in the function. I feel stupid...

Thank you all a million, you've made my day ;)

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  

×