Jump to content

Function intended to run once keeps looping


Recommended Posts

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?

Link to comment
Share on other sites

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.

 
Link to comment
Share on other sites

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]--------------- __________------------------------------ __________---------------

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

@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
Link to comment
Share on other sites

@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 ;)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...