Jump to content

questions about running a function multiple times


Recommended Posts

what happens if i have 2 keys activiting different functions and i first hit key1 and while the function is still running i hit key 2?

i know autoit doesnt support multiple functions at a time, so is there anyway i can make the other function wait till its another function is finished?

for example this simple script. al it does is displaying a message when i hit one of the numpads 1 - 4 , but i dont want to run the function of one of them till the other is finished else they start to overlap eachother.

#include <GUIConstants.au3>
#include <ComboConstants.au3>
GUICreate("Trade helper", 400,400,100,100)
$bericht1 = GUICtrlCreateInput ( "", 10,  5, 300, 20)
$bericht2 = GUICtrlCreateInput ( "", 10,  45, 300, 20)
$bericht3 = GUICtrlCreateInput ( "", 10,  85, 300, 20)
$bericht4 = GUICtrlCreateInput ( "", 10,  125, 300, 20)
GUICtrlCreateLabel("current messages",85,180)

$button1 = GuiCtrlCreateButton("Stop", 30, 150, 130, 20)
HotKeySet("{NUMPAD1}","trade1")
HotKeySet("{NUMPAD2}","trade2")
HotKeySet("{NUMPAD3}","trade3")
HotKeySet("{NUMPAD4}","trade4")
GUISetState ()





$msg = 0
While $msg <> $GUI_EVENT_CLOSE
$msg = GUIGetMsg()
$bericht11=GUICtrlRead ($bericht1)
$bericht22=GUICtrlRead ($bericht2)
$bericht33=GUICtrlRead ($bericht3)
$bericht44=GUICtrlRead ($bericht4)
WEnd

Func trade1()
Send("{ENTER}")
Send($bericht11)
Send("{ENTER}")
EndFunc

Func trade2()
Send("{ENTER}")
Send($bericht22)
Send("{ENTER}")
EndFunc
Func trade3()
Send("{ENTER}")
Send($bericht33)
Send("{ENTER}")
EndFunc
Func trade4()
Send("{ENTER}")
Send($bericht44)
Send("{ENTER}")
EndFunc
Link to comment
Share on other sites

The only way I could think of doing it was to buffer the keys in a variable.

I've set the Hotkeys to use one function and split it with a switch statement to get the correct hotkey

When the second/third etc hotkey is pressed it calls the buffer function while there is a current operation, then resends the keypress once the operation has finished

Global $keyQueue = ""

;set the number keys 1 to 5 as hotkeys
For $i = 1 to 5
HotkeySet("" & $i & "","MyFunc")
Next

While 1
    
    Sleep (100)
    
WEnd


Func MyFunc()

;set the hotkeys to call the buffer func
For $i = 1 to 5
    HotkeySet("" & $i & "","Queue")
Next

$pos = MouseGetPos()

Switch @HotKeyPressed
    
    Case "1"
        For $j = 1 to 10
            ToolTip("Hotkey 1 function",$pos[0],$pos[1])
            Sleep (200)
        Next
        ToolTip("")
    Case "2"
        For $j = 1 to 10
            ToolTip("Hotkey 2 function",$pos[0],$pos[1])
            Sleep (200)
        Next
        ToolTip("")
    Case "3"
        For $j = 1 to 10
            ToolTip("Hotkey 3 function",$pos[0],$pos[1])
            Sleep (200)
        Next
        ToolTip("")
    Case "4"
        For $j = 1 to 10
            ToolTip("Hotkey 4 function",$pos[0],$pos[1])
            Sleep (200)
        Next
        ToolTip("")
    Case "5"
        For $j = 1 to 10
            ToolTip("Hotkey 5 function",$pos[0],$pos[1])
            Sleep (200)
        Next
        ToolTip("")
    
        
EndSwitch

;set the hotkeys back to the normal function
For $i = 1 to 5
HotkeySet("" & $i & "","MyFunc")
Next

;send any hotkeys that were buffered
If $keyQueue <> "" then 
    $letter = StringLeft($keyQueue,1)
    $keyQueue = StringTrimLeft($keyQueue,1);trim the keybuffer
    Send($letter)
EndIf

    
EndFunc

Func Queue()
    $keyQueue &= @HotKeyPressed
EndFunc
Edited by ChrisL
Link to comment
Share on other sites

Try this

Global $keyQueue = ""

#include <GUIConstants.au3>
#include <ComboConstants.au3>
GUICreate("Trade helper", 400,400,100,100)
$bericht1 = GUICtrlCreateInput ( "", 10,  5, 300, 20)
$bericht2 = GUICtrlCreateInput ( "", 10,  45, 300, 20)
$bericht3 = GUICtrlCreateInput ( "", 10,  85, 300, 20)
$bericht4 = GUICtrlCreateInput ( "", 10,  125, 300, 20)
GUICtrlCreateLabel("current messages",85,180)

$button1 = GuiCtrlCreateButton("Stop", 30, 150, 130, 20)
For $i = 1 to 4
    HotKeySet("{NUMPAD" & $i & "}","trade")
Next

GUISetState ()





$msg = 0
While $msg <> $GUI_EVENT_CLOSE
$msg = GUIGetMsg()
$bericht11=GUICtrlRead ($bericht1)
$bericht22=GUICtrlRead ($bericht2)
$bericht33=GUICtrlRead ($bericht3)
$bericht44=GUICtrlRead ($bericht4)
WEnd

Func trade()
    
    For $i = 1 to 4
        HotKeySet("{NUMPAD" & $i & "}","Queue")
    Next

Switch @HotKeyPressed
    
    Case "{NUMPAD1}"
        Send("{ENTER}")
        Send($bericht11)
        Send("{ENTER}")
        Sleep (5000)
    Case "{NUMPAD2}"
        Send("{ENTER}")
        Send($bericht22)
        Send("{ENTER}")
        Sleep (5000)
    Case "{NUMPAD3}"
        Send("{ENTER}")
        Send($bericht33)
        Send("{ENTER}")
        Sleep (5000)
    Case "{NUMPAD4}"
        Send("{ENTER}")
        Send($bericht44)
        Send("{ENTER}")
        Sleep (5000)

EndSwitch
    
For $i = 1 to 4
    HotKeySet("{NUMPAD" & $i & "}","trade")
Next

If $keyQueue <> "" then
    $keyLen = StringInStr($keyQueue,"|")
    $Key = StringLeft($keyQueue,$keyLen -1)
    $keyQueue = StringTrimLeft($keyQueue,$keyLen)
    ConsoleWrite("Sending " & $Key & @crlf);Debug to Scite
    Send($Key)
EndIf

EndFunc



Func Queue()
    $keyQueue &= @HotKeyPressed & "|"
    ConsoleWrite("Buffer " & $keyQueue & @crlf);Debug to Scite
EndFunc
Edited by ChrisL
Link to comment
Share on other sites

thx for the help im going to test it.

edit:

it doesnt work, but i dont see the mistake:(

i dont really understand the last part:

For $i = 1 to 4
    HotKeySet("{NUMPAD" & $i & "}","trade")
Next

If $keyQueue <> "" then
    $keyLen = StringInStr($keyQueue,"|")
    $Key = StringLeft($keyQueue,$keyLen -1)
    $keyQueue = StringTrimLeft($keyQueue,$keyLen)
    ConsoleWrite("Sending " & $Key & @crlf);Debug to Scite
    Send($Key)
EndIf

EndFunc



Func Queue()
    $keyQueue &= @HotKeyPressed & "|"
    ConsoleWrite("Buffer " & $keyQueue & @crlf);Debug to Scite
EndFunc

i never used these kind of things before. what does it exactly do?

Edited by cageman
Link to comment
Share on other sites

Do you have/Use Scite for writing your scripts?

If so check out the output in the bottom pain, it does work with the functions but maybe the actions are incorrect but it deffinately buffers the hotkeys.

Run the script and press Numpad 1234321 in quick succession and check out the debug info in the Scite pain

Global $keyQueue = ""

#include <GUIConstants.au3>
#include <ComboConstants.au3>
GUICreate("Trade helper", 400,400,100,100)
$bericht1 = GUICtrlCreateInput ( "", 10,  5, 300, 20)
$bericht2 = GUICtrlCreateInput ( "", 10,  45, 300, 20)
$bericht3 = GUICtrlCreateInput ( "", 10,  85, 300, 20)
$bericht4 = GUICtrlCreateInput ( "", 10,  125, 300, 20)
GUICtrlCreateLabel("current messages",85,180)

$button1 = GuiCtrlCreateButton("Stop", 30, 150, 130, 20)
For $i = 1 to 4
    HotKeySet("{NUMPAD" & $i & "}","trade")
Next

GUISetState ()





$msg = 0
While $msg <> $GUI_EVENT_CLOSE
$msg = GUIGetMsg()
$bericht11=GUICtrlRead ($bericht1)
$bericht22=GUICtrlRead ($bericht2)
$bericht33=GUICtrlRead ($bericht3)
$bericht44=GUICtrlRead ($bericht4)
WEnd

Func trade()
    
    For $i = 1 to 4
        HotKeySet("{NUMPAD" & $i & "}","Queue")
    Next

Switch @HotKeyPressed
    
    Case "{NUMPAD1}"
        ConsoleWrite("Numpad1 is started" & @crlf)
        Send("{ENTER}")
        Send($bericht11)
        Send("{ENTER}")
        Sleep (5000)
        ConsoleWrite("Numpad1 is Finished" & @crlf)
    Case "{NUMPAD2}"
        ConsoleWrite("Numpad2 is started" & @crlf)
        Send("{ENTER}")
        Send($bericht22)
        Send("{ENTER}")
        Sleep (5000)
        ConsoleWrite("Numpad2 is Finished" & @crlf)
    Case "{NUMPAD3}"
        ConsoleWrite("Numpad3 is started" & @crlf)
        Send("{ENTER}")
        Send($bericht33)
        Send("{ENTER}")
        Sleep (5000)
        ConsoleWrite("Numpad3 is Finished" & @crlf)
    Case "{NUMPAD4}"
        ConsoleWrite("Numpad4 is started" & @crlf)
        Send("{ENTER}")
        Send($bericht44)
        Send("{ENTER}")
        Sleep (5000)
        ConsoleWrite("Numpad4 is Finished" & @crlf)

EndSwitch
    
For $i = 1 to 4
    HotKeySet("{NUMPAD" & $i & "}","trade")
Next

If $keyQueue <> "" then
    $keyLen = StringInStr($keyQueue,"|")
    $Key = StringLeft($keyQueue,$keyLen -1)
    $keyQueue = StringTrimLeft($keyQueue,$keyLen)
    ConsoleWrite("Sending " & $Key & @crlf);Debug to Scite
    Send($Key)
EndIf

EndFunc



Func Queue()
    $keyQueue &= @HotKeyPressed & "|"
    ConsoleWrite("Buffer " & $keyQueue & @crlf);Debug to Scite
EndFunc

This bit

For $i = 1 to 4
    HotKeySet("{NUMPAD" & $i & "}","trade")
Next

Is the same as

HotkeySet("{NUMPAD1"}","trade")
HotkeySet("{NUMPAD2"}","trade")
HotkeySet("{NUMPAD3"}","trade")
HotkeySet("{NUMPAD4"}","trade")

I just put them in a loop, it was easy to use a counting loop for the 1 to 4 part of NumpadX

When the trade function is ran it changes all of the hotkeys to use the Queue function instead of trade, this allows the function to complete before starting the next function. All this function does is keeps track of any hotkeys pressed while the current trade function part is running. The Buffer is a global variable declared at the top of the script called $KeyQueue

Func Queue()
    $keyQueue &= @HotKeyPressed & "|"
    ConsoleWrite("Buffer " & $keyQueue & @crlf);Debug to Scite
EndFunc

Once the currently running part of the function completes the original hotkeys are reinstated to the trade function and if any of the Hotkeys were pressed they are stored in the buffer variable $KeyQueue

What we now do with the original hotkey sets now reinstated is to work from the left hand side of the $KeyQueue buffer variable and resend the hotkeys, when we send the next hotkey the buffer is reactivated to queue any more hotkey presses that may happen

If $keyQueue <> "" then
    $keyLen = StringInStr($keyQueue,"|")
    $Key = StringLeft($keyQueue,$keyLen -1)
    $keyQueue = StringTrimLeft($keyQueue,$keyLen)
    ConsoleWrite("Sending " & $Key & @crlf);Debug to Scite
    Send($Key)
EndIf

Edit:

Looking at your code this part

Send("{ENTER}")

Send($bericht11)

Send("{ENTER}")

Doesn't really make much sense where is it sending this information?

I don't fully understand what your program is for if you explain we may be able to help

Edited by ChrisL
Link to comment
Share on other sites

its for a game so i dont have to type a whole message each time. cause sometimes i need to repeat something alot of times and no its not spam:)

i press enter so i can enter chat

then type the message i filled in in the GUI and then press enter again to send it.

so easy, but if i would do 2 things at the same time it would press enter enter again and maybe again and then some keys can open my inventory for example and some strange things could happen

thats why i want to make sure only 1 function runs.

edit:

oh and i use scite yes

Edited by cageman
Link to comment
Share on other sites

its for a game so i dont have to type a whole message each time. cause sometimes i need to repeat something alot of times and no its not spam:)

i press enter so i can enter chat

then type the message i filled in in the GUI and then press enter again to send it.

so easy, but if i would do 2 things at the same time it would press enter enter again and maybe again and then some keys can open my inventory for example and some strange things could happen

thats why i want to make sure only 1 function runs.

edit:

oh and i use scite yes

I think you need to look at controlsend to send the text to a specific window/control, Send is going to send it to the active window which is your own app isn't it?

Having said that, If I open up notepad and make sure the Notepad window is active and I type on the Number pad 1234 it works fine and writes the text in to notepad.

I still think that you need to look at ControlSend though!

Link to comment
Share on other sites

I think you need to look at controlsend to send the text to a specific window/control, Send is going to send it to the active window which is your own app isn't it?

Having said that, If I open up notepad and make sure the Notepad window is active and I type on the Number pad 1234 it works fine and writes the text in to notepad.

I still think that you need to look at ControlSend though!

oke thx :whistle: works perfect.
Link to comment
Share on other sites

Cool, you could always just use WinActivate before sending the keys, it's a little crude but might help!

im sure i always have an active window, so i dont need that;)

again thx for the help i would never have come up with your buffer function

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